#!/bin/sh

# PalaceServer for UNIX install script.

# Amend PATH variable
PATH=$PATH:/bin:/usr/bin:/sbin

# ****Following Section sets default variables for install****

# The name of the palace server (hub) executable:
PSERVER="pserver"
# The name of the palace server (frontend) executable:
PSFRONT="psfront"
# The location where palace files will be installed:
ROOT="/usr/local/palace"
# Default value for individual palace files
INSTANCE="palace"
# The name of the default license file
LICENSEFILE="license.txt"
# The name of the tar containing files shared by all installed servers:
SHARED_TAR="frame.tar"
# The name of the tar file contining the version specific UNIX binaries:
BINFILES_TAR="arch.tar"
# The name of the tar file contining files specific to each server install:
INSTANCE_TAR="instance.tar"
# Default state for the directory name check variable
FOOBAR_CHARACTER="true"
# Set the working directory as a variable for unpacking tar files.
DIR=`pwd`

# ****This Section is the *actual* installer!**** 

# Introduction text and license agreement information
echo
echo "Welcome to the PalaceServer installer, which will guide you"
echo "through the process of installing the PalaceServer for UNIX."
echo 
echo 
echo "LICENSE AGREEMENT"
echo
echo "Please read the following license agreement.  You must accept the"
echo "agreement to continue installing the PalaceServer."
echo
echo "Press ENTER to view the License Agreement."
echo
read inputstring

# Show the user the license:
${PAGER:-more} $LICENSEFILE

# Now confirm that the user agrees to the license:
echo
echo 'Please type "accept" to indicate your acceptance of this'
echo 'license agreement.'
read inputstring

if [ "$inputstring" != "accept" ]
then
  echo
  echo 'YOU DID NOT ACCEPT THE LICENSE AGREEMENT.  INSTALLATION TERMINATED.'
  echo
  exit 1
fi

# The following routine ensures that the user selects an appropriate
# directory name.  If the name isn't appropriate, the user will be prompted
# again for a better name.

while [ $FOOBAR_CHARACTER = "true" ]
do

  # Ask the user where to install the palace server:
  echo
  echo "In what directory would you like to install the PalaceServer?"
  echo "Default location (press return) is: [$ROOT]"
  read inputstring
  if [ "$inputstring" ]
  then
    ROOT=$inputstring
    ROOT=`echo $ROOT | sed -e's!/$!!'`
  fi
  
  # Check for silly chars in $ROOT
  
  # Check for any characters in the install path that may interfere with any
  # future scripting or that may cause general confusion after install.  Only 
  # capital and lowercase letters, numerals, _, and - are allowed as directory
  # names.  
    
  if echo $ROOT | egrep '[^A-Za-z/0-9_-]' > /dev/null 
  then  
    echo
    echo "THIS INSTALLATION SCRIPT WILL NOT WORK IF THE PATH YOU SPECIFY FOR" 
    echo "YOUR PALACE SERVER INSTALL CONTAINS ANY CHARACTERS THAT ARE"
    echo "NOT ALPHANUMERIC, /, _, or -. PLEASE REPLACE THE ILLEGAL PATH"
    echo "WITH A VALID ONE."
  else FOOBAR_CHARACTER="false"

  fi
done

export ROOT
export DIR
export INSTANCE

# Create $ROOT if it does not exist
if [ -d $ROOT ]
then
  echo 
  echo "PREVIOUS VERSION WARNING"
  echo 
  echo "Directory $ROOT exists. Do you wish to upgrade your current"
  echo "Palace install? NOTE: This will require overwriting your"
  echo "PalaceServer install.  Your original PalaceServer directory WILL"
  echo "be backed up.  Instructions for migrating to the new installation"
  echo "will be provided at the end of the install"
  echo
  echo " Do you wish to upgrade your current Palace install? <y/N> "
  read inputstring
  case "$inputstring" in
      y|Y|yes|Yes|YES)

      # If the user chooses to upgrade their current install, their dirctory
      # structure will be moved to a new location (essentially backing it up, 
      # and a directory of the same name created.

      # Back up old palace directory to a safe location
      mv $ROOT $ROOT.$$  
     
      # Creating new PalaceDirectory    
      mkdir $ROOT
      mkdir $ROOT/$INSTANCE
    ;;
  *)
    echo
    echo YOU CHOSE NOT TO OVERWRITE YOUR EXISTING PALACE DIRECTORY.
    echo INSTALLATION ABORTED WITH NO CHANGES MADE.
    echo
    exit 1
    ;;
  esac
else
  echo
  echo "Directory $ROOT does not exist.  Creating it..."
  mkdir $ROOT
  mkdir $ROOT/$INSTANCE
fi

# Make sure $ROOT is a directory
if [ -d $ROOT ]
then
  echo Directory $ROOT now exists.
  chmod 755 $ROOT > /dev/null 
  chmod 755 $ROOT/$INSTANCE > /dev/null
else
  echo $ROOT is not a directory!
  echo
  echo INSTALL EXITED PREMATURELY.  FIX ABOVE PROBLEMS AND RUN INSTALL AGAIN.
  echo
  exit 1
fi

# Change to the new directory and unpack/install tarred files
umask 022
echo "Unpacking files...."
echo
cd $ROOT
tar -xf $DIR/$SHARED_TAR
cd bin
tar -xf $DIR/$BINFILES_TAR
cd ../palace
tar -xf $DIR/$INSTANCE_TAR
cd $DIR

# Run the configuration script to configure installed PalaceServer
./setup

# import useful variables from setup
cd $DIR
. ./variables.out
rm variables.out

# Set rights on bin dir
cd $ROOT
chmod -R 750 bin

# Start servers now if desired

echo
echo "The server is now installed. Would you like to start it now?"
read inputstring
case "$inputstring" in
  y|Y|yes|Yes|YES)

# Start the Palace Server running
    cd bin
    ./start

# Check to make sure that the PalaceServer started Successfully and 
# display appropriate message.

    if [ -f $ROOT/$INSTANCE/logs/pserver.pid ] && [ -f $ROOT/logs/httpd.pid ] \
      && kill -0 `cat $ROOT/$INSTANCE/logs/pserver.pid` && kill -0 `cat $ROOT/logs/httpd.pid` 
    then
      echo
      echo "You're done!"
      echo
      echo "The server is now running.  To start or stop your"
      echo "server, cd to $ROOT/bin and run the start-palace"
      echo "or stop-palace scripts as you need to."
      echo ""
      echo "Also, please view the RELNOTES files for additional"
      echo "information.  These files will help you with configuring your"
      echo "PalaceServer and integrating it with your website for optimal"
      echo "performance."
      echo
    else
      echo 
      echo "Uh-Oh!"
      echo 
      echo "Unfortunately, your PalaceServer did not start properly.  Please"
      echo "check the RELNOTES file for information on troubleshooting your"
      echo "PalaceServer installation."
    fi
    ;;
  *)
    echo
    echo "You're done!"
    echo
    echo "To start your server, cd to $ROOT/bin and run the start-palace"
    echo "script."
    echo ""
    echo "Also, please view the RELNOTES files for additional"
    echo "information.  These files will help you with configuring your"
    echo "PalaceServer and integrating it with your website for optimal"
    echo "performance."
    ;;
esac

# Display Release Notes if desired.
echo
echo "View Release Notes now?"
read inputstring
case "$inputstring" in
  y|Y|yes|Yes|YES)
    cd $ROOT
    ${PAGER:-more} RELNOTES
    cd $DIR;;
esac
cd $DIR
exit 0
