#!/bin/sh
#
# Programmer: Brian L. Evans
# Version: @(#)startServer	1.12	02/01/98
#
# Usage: startServer userName
#
# This is a simple Unix shell script to start the TCP/IP WEDS Server.
# Please modify the following settings according to your system.

# BEGIN user configuration of settings

# 1. Get a process table in extra-wide format that includes the user names
#    The 'ps' program takes different arguments for the BSD and System V
#    Unix implementations.  For BSD versions of ps, the command would be
#    "ps -auxwww".  On System V, the command would be "ps -ef".  Set
#    PROCTABLE accordingly.
PROCTABBSD="/usr/ucb/ps -auxwww"
PROCTABSYSV="/bin/ps -ef"
PROCTABLE=$PROCTABSYSV
# 2. Get the process id number from a line of data in a process table---
#    the process id number is the second word at character locations 9-14
#    because user names are up to eight characters long.  The 'cut' program
#    is not always available.  A possible alternative is awk.
GETSECONDWORD="/bin/cut -c9-14"
# 3. Update the path to include where the cd, cut, grep, kill, mv,
#    ps, and sleep programs are installed.  Note that the default path
#    for crontab jobs is PATH=/usr/bin.  The correct version of the
#    Java Developer's Kit (JDK) must be on your path for the WEDS server
#    to run.  WEDS 1.0.X uses JDK 1.0.2.
PATH=$PATH:/usr/local/bin:/usr/local/packages/java-1.0.2/bin
export PATH
# 4. Set the name of version 1.0.2 of the 'java' program in the
#    Java Developer's Kit.  If the 'java' program is not on the
#    path, then specify a full pathname.
JAVA102=java

# END user configuration of settings

# Check to make sure that we have received exactly one argument
if [ $# != 1 ]
then
  echo "Usage: $0 userName"
  exit 1
fi

userName=$1

# Kill the current server if it is still running
procId=`$PROCTABLE | grep -w java | grep $userName | grep -v grep | $GETSECONDWORD`
if [ "$procId" != "" ]
then
  kill $procId
  echo "Killed the old WEDS TCP/IP Server started by $userName"
  # Sleep for fifteen seconds to give time for the process to die
  sleep 15
fi

# Determine where the startServer script is installed
if [ "$0" = "startServer" ]
then
  thisDirectory="."
else
  thisDirectory=`echo $0 | sed -e 's+/startServer++'`
fi

# Determine the machine architecture to run the right simulators
ptarchScript="$thisDirectory/ptarch"
simArch=`$ptarchScript`

# Pass the location of the simulator programs to the server
# relative to the directory in which the server is installed
serverDirectory="$thisDirectory/../classes"

# Figure out where to write the output from stdout and stderr
if [ -d $serverDirectory/outfiles -a -w $serverDirectory/outfiles ]
then
  outputDirectory=$serverDirectory/outfiles
  relOutputDirectory=outfiles
elif [ -d $serverDirectory -a -w $serverDirectory ]
then
  outputDirectory=$serverDirectory
  relOutputDirectory=.
else
  outputDirectory=/tmp
  relOutputDirectory=/tmp
fi

# Backup the last server output file if it exists
if [ -r $outputDirectory/Server.out ]
then
  mv $outputDirectory/Server.out $outputDirectory/Server.out~
fi

# Names of the relative pathnames of possible simulator programs
C30SIM="../../simulator/tms320c30/bin.$simArch/c30sim"
M68SIM="../../simulator/m68hc11/bin.$simArch/m68hc11sim"
M568DEB="../../simulator/ads568/bin.$simArch/ads56800"
M568SIM="../../simulator/clas568/bin.$simArch/sim56800"
simProgNames="$C30SIM $M68SIM $M568DEB $M568SIM"

# Kill any old simulator processes that are still running
for sim in $simProgNames
do
  simProcId=`$PROCTABLE | grep $sim | grep $userName | grep -v grep | $GETSECONDWORD`
  if [ "$simProcId" != "" ]
  then
    kill $simProcId
    echo "Killed old $sim processes"
    # Sleep for fifteen seconds to give time for the process to die
    sleep 15
  fi
done

# Start the server and direct stdout and stderr to the file Server.out
cd $serverDirectory; \
  $JAVA102 -Denv.C30SIM=$C30SIM -Denv.MC6811=$M68SIM \
	   -Denv.ADS568=$M568DEB -Denv.SIM568=$M568SIM \
           Server > $relOutputDirectory/Server.out 2>&1 &
echo "Started a new WEDS TCP/IP Server for $userName"
echo "Output from stdout and stderr will be in "
echo "$serverDirectory/$relOutputDirectory/Server.out"
