#!/bin/sh

# PalaceServer for UNIX configuration script.  This script is
# called from the end of the main installer.  After the main 
# installer untars the palace directories, this script obtains 
# pertinent information from the user and configures the 
# corresponding PalaceServer files accordingly.

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

# Serial number to look for
SERIALNUMBER=""
# The name of the configuration file:
PSERVERCONF="pserver.conf"
# Set default URL to null
SERVER_HOSTNAME=""
# Default Palace port.
PORT=9998
# Default HTTP port.
HTTP_PORT=9990

# ****This section is the actual configurator****

# let the user enter their registration number
echo
echo "PalaceServer License Number"
echo
echo "The license number you enter defines the capacity of your installation."
echo
echo "Please enter your license number now."
echo
echo "You may upgrade your license number at any time by editing"
echo "$ROOT/palace/psdata/$PSERVERCONF."
echo
echo "License number: "
read inputstring
     
# Check to see if serial number was entered or if the default was chosen.
  if [ -n "$inputstring" ]
  then SERIALNUMBER=$inputstring
  fi      

# Query for the appropriate hostname for the server:
BAD_CHARACTER="true" 
while [ "$BAD_CHARACTER" = "true" ]	
  do
  while [ "$SERVER_HOSTNAME" = "" ]
    do
      echo
      echo "Setting your World Wide Web Hostname"
      echo
      echo "To properly set up your PalaceServer, you must specify" 
      echo "the hostname that is used to identify this machine" 
      echo "on the internet."
      echo 
      echo "Please enter the correct WWW hostname for your server"
      echo "(e.g. yourserver.domain.com):"
      read inputstring
      SERVER_HOSTNAME=$inputstring
  done
    #Check for bad characters in the SERVER_HOSTNAME
    if echo $SERVER_HOSTNAME | egrep '[^A-Za-z/0-9._-]'>/dev/null 
      then  
        echo
        echo "THIS INSTALLATION SCRIPT WILL NOT WORK IF THE HOSTNAME YOU" 
        echo "SPECIFY FOR YOUR PALACE SERVER CONTAINS ANY CHARACTERS THAT ARE"
        echo "NOT ALPHANUMERIC, _, or -. PLEASE REPLACE THE ILLEGAL HOSTNAME"
        echo "WITH A VALID ONE."
        SERVER_HOSTNAME=""
      else BAD_CHARACTER="false" 
    fi
done

# Query for Palace port number
BAD_CHARACTER="true" 
while [ "$BAD_CHARACTER" = "true" ]	
  do
    echo
    echo "Setting your Palace Server port number"
    echo
    echo "What port number would you like your server to listen for user" 
    echo "connections on?" 
    echo "Default Palace Server port (press return) is: [$PORT]"
    read inputstring
    if [ "$inputstring" ]
    then
      testport=$inputstring
    else
      testport=$PORT
    fi

    #Check for bad characters in the port number
    if echo $testport | egrep '[^0-9]'>/dev/null 
    then  
      echo
      echo "THE PORT MUST BE GIVEN AS A DECIMAL NUMBER. PLEASE SPECIFY" 
      echo "A VALID PORT NUMBER."
    else
      BAD_CHARACTER="false"
      PORT=$testport
    fi
done

# Query for HTTP port number
BAD_CHARACTER="true" 
while [ "$BAD_CHARACTER" = "true" ]	
  do
    echo
    echo "Setting your HTTP Server port number"
    echo
    echo "Your Palace Server will direct clients to your HTTP server to obtain"
    echo "artwork and other resources. What port number should Palace clients"
    echo "connect to in order to reach your HTTP server?"
    echo
    echo "Default HTTP Server port (press return) is: [$HTTP_PORT]"
    read inputstring
    if [ "$inputstring" ]
    then
      testport=$inputstring
    else
      testport=$HTTP_PORT
    fi

    #Check for bad characters in the port number
    if echo $testport | egrep '[^0-9]'>/dev/null 
    then  
      echo
      echo "THE PORT MUST BE GIVEN AS A DECIMAL NUMBER. PLEASE SPECIFY" 
      echo "A VALID PORT NUMBER."
    else
      BAD_CHARACTER="false"
      HTTP_PORT=$testport
    fi
done


# Prompt for Servername. 
echo
echo "Naming Your PalaceServer"
echo
echo "Please enter the name you want to use for your PalaceServer" 
echo "(e.g. My Company's PalaceServer).  This name will be displayed"
echo "to users who connect to your site."
echo
echo "PalaceServer Name:" 
read inputstring
SERVER_NAME=$inputstring

# Make sure Server name is less than 31 characters.
while [ `echo $SERVER_NAME | wc -c` -ge 31 ]
  do
    echo
    echo "The name you have chosen for your PalaceServer is more than 30"
    echo "characters long.  Please choose a shorter name."
    echo
    echo "PalaceServer Name:"
    echo
    read inputstring
    SERVER_NAME=$inputstring
done

# Take problem characters out of the Server name.
SERVER_NAME=`echo $SERVER_NAME | tr '"!' "' " ` 
 
# Look for the correct group name to use and set variable appropriately.
if egrep '^nogroup' /etc/group > /dev/null
  then NOGROUP=nogroup
  else NOGROUP=nobody
fi  

# Set all parameters as given by user 
substitutevars() {
  TEMPLATE=$1
sed -e "s!%SERVER_NAME%!$SERVER_NAME!"\
    -e "s!%SERIALNUMBER%!$SERIALNUMBER!"\
    -e "s!%HOSTNAME%!$SERVER_HOSTNAME!"\
    -e "s!%ROOT%!$ROOT!"\
    -e "s!%PORT%!$PORT!"\
    -e "s!%HTTP_PORT%!$HTTP_PORT!"\
    -e "s!%INSTANCE%!$INSTANCE!"\
    -e "s!%NOGROUP%!$NOGROUP!"\
    < $TEMPLATE > `echo $TEMPLATE | sed -e 's/\.tmpl//'`
	rm $TEMPLATE
}

umask 027
find $ROOT \( -name \*.tmpl -o -name .\*.tmpl \) -print | while read FILENAME
do
  substitutevars $FILENAME
done

# Export neccessary variables back to install.
cd $DIR
echo "SERVER_HOSTNAME=$SERVER_HOSTNAME">variables.out
echo "export SERVER_HOSTNAME">>variables.out
echo "HTTP_PORT=$HTTP_PORT">>variables.out
echo "export HTTP_PORT">>variables.out
chmod 777 variables.out

# Return to install.
