#!/bin/sh
#
# This shell program is designed to quickly get your Daylight software
# started.  To use it, simply type "faststart".
#
# Prerequisites:
#  Daylight software must be installed
#  You must have 64 megabytes of main memory.
#  You must be running X11
#  You must have one of:
#      SunOS release 5.5 ("Solaris 2.5") or later
#      SGI release 6.2 or later
#      RedHat Linux release 6.1 or later
#
# You should be able to run this script as soon as you've installed
# the distribution, added "thor" and "merlin" services
# to /etc/services, and installed a valid license file obtained from
# Daylight.
#
# This program may ask a couple of questions at the start if it can't
# figure out where things are.
#


######################################################################
# Figure out where DY_ROOT is.  If it isn't defined, we get it by
# assuming that this script lives in $DY_ROOT/bin.
#

if [ -z "$DY_ROOT" ] ; then
  dir=`dirname $0`
  ( cd $dir
    bin=`pwd`
    bin=`basename $bin`
        )
  if [ "$dir" = "" ] ; then dir="." ; fi
  cd $dir/..
  DY_ROOT=`pwd`; export DY_ROOT
  tmp_root=$DY_ROOT
else
  echo "\$DY_ROOT is currently defined as:"
  echo "  \$DY_ROOT = $DY_ROOT"
  echo "Press [Return] to use current value, or enter new directory:"
  read tmp_root
  if [ $? != 0 ] ; then exit 1; fi
fi
if [ -z "$tmp_root" ] ; then
  tmp_root=$DY_ROOT
fi
if [ -d $tmp_root/bin ] ; then
  bin=bin
fi

tmp_root=`echo "echo $tmp_root" | sh`
while [ ! -d "$tmp_root" -o ! -d "$tmp_root/$bin" ] ; do
  echo "ERROR: \"$tmp_root\" doesn't appear to be correct for DY_ROOT."
  echo "  DY_ROOT should be something like one of these:"
  echo "    /daylight/v462"
  echo "    /usr/local/daylight/v462"
  echo "    $HOME/daylight/v462"
  echo "Enter directory for DY_ROOT:"
  read tmp_root
  if [ $? != 0 ] ; then exit 1; fi
  tmp_root=`echo "echo $tmp_root" | sh`
  bin=bin
done
DY_ROOT=$tmp_root

export DY_ROOT
echo "\$DY_ROOT        = $DY_ROOT"


#######################################################################
# See if this is a version of the OS that we support.
#

osversion=`$DY_ROOT/$bin/osversion`
if [   0 = `echo $osversion | egrep -c "SGI 6.[2-9]"` \
    -a 0 = `echo $osversion | egrep -c "Linux 2.2.*"` \
    -a 0 = `echo $osversion | egrep -c "solaris 5.[2-9]"` \
    ] ; then
  echo "Warning: you may not be running the right operating system."
  echo "Daylight Software requires:"
  echo "     Sun/Solaris version 5.2 or later",
  echo "     SGI IRIX 6.2 or later"
  echo "     RedHat Linux 6.1 or later"
  echo "This version calls itself \"$osversion\".  Proceed anyway?"
  echo "(enter \"n\" to quit):"
  read line
  if [ $? != 0 ] ; then exit 1; fi
  if [ "$line" = "n" ] ; then
    echo "quitting"
    exit 1
  fi
fi

is_sun=FALSE
is_sgi=FALSE
is_linux=FALSE

case $osversion in
  solaris*) is_sun=TRUE
    ;;
  Sun*) is_sun=TRUE
    ;;
  SGI*) is_sgi=TRUE
    ;;
  Linux*)  is_linux=TRUE
    ;;
esac



#######################################################################
# Make sure we can find the X display.  If Sun system, we also need 
# OPENWINHOME and LD_LIBRARY_PATH so the dynamic linker can work.
#

host=`hostname`
if [ \( $is_sun = TRUE -a -z "$OPENWINHOME" \) -o -z "$DISPLAY" ] ; then

  echo ""
  echo "\$OPENWINHOME or \$DISPLAY is not defined; you must be running"
  echo "X Windows. If you have X Windows on one machine and Daylight"
  echo "software on another (e.g. you used rlogin(1) to get to the"
  echo "Daylight software), you probably need to set the following:"
  echo ""
  if [ $SHELL != "/bin/csh" ] ; then
    echo "  DISPLAY=$host:0.0; export DISPLAY"
    if [ $is_sun = TRUE ] ; then
      echo "  OPENWINHOME=/usr/openwin; export OPENWINHOME"
      echo "  LD_LIBRARY_PATH=$OPENWINHOME/lib:/usr/lib; export LD_LIBRARY_PATH"
    fi
  else
    echo "  setenv DISPLAY $host:0.0"
    if [ $is_sun = TRUE ] ; then
      echo "  setenv OPENWINHOME /usr/openwin"
      echo "  setenv LD_LIBRARY_PATH \$OPENWINHOME/lib:/usr/lib"
    fi
  fi
  echo ""
  echo "(If your X server is not \"$host\", replace \"$host\" with the name"
  echo "of your X server.)"
  echo ""

  exit 1
fi

######################################################################
# Make sure /etc/services is set up correctly.
#

if [   0 = `grep -c thor </etc/services` \
    -a 0 = `grep -c merlin </etc/services` ] ; then
  echo "No entries for thor or merlin in your services file; can't"
  echo "run the servers.  You need the following lines added to the"
  echo "file /etc/services:"
  echo ""
  echo "    thor   5555/tcp"
  echo "    merlin 5556/tcp"
  echo ""
  echo "Only the super-user (root) can edit the services file.  See"
  echo "your UNIX system administrator for help."
  exit
else
  echo "/etc/services looks ok..."
fi


#######################################################################
# Set up HELPPATH for XView's "Help" key (usually "F1").
#

if [ -z "$HELPPATH" ] ; then
  HELPPATH=$DY_ROOT/help:$OPENWINHOME/lib/help
elif [ 0 = `echo $HELPPATH | grep -c $DY_ROOT` ] ; then
  HELPPATH=$DY_ROOT/help:$HELPPATH
fi
export HELPPATH


#######################################################################
# If DY_THORDB is defined, try to use that; otherwise, ask for it.
#

cd $DY_ROOT/../
if [ -z "$DY_THORDB" ] ; then
  if [ -w thordb ] ; then
    DY_THORDB=`pwd`/thordb
  else
    DY_THORDB=""
  fi
fi

line=$DY_THORDB
while [ "$line" = "" -o ! -w "$line" ] ; do
  echo "Enter location for THOR databases:"
  read line
  if [ $? != 0 ] ; then exit 1; fi
  line=`echo "echo $line" | sh`
  if [ "$line" = "" -o ! -w "$line" ] ; then
    echo "\"$line\" is not a writeable directory"
    line=""
  fi
done
DY_THORDB=$line
export DY_THORDB
echo "\$DY_THORDB      = $DY_THORDB"


######################################################################
# Check for lock files in $DY_THORDB.  Their existence usually
# spells trouble.
#

lockfiles=`ls $DY_THORDB/*.LCK 2>/dev/null`
if [ ! -z "$lockfiles" ] ; then
  echo "--------------------------------------------------------------------"
  echo "WARNING: The following databases are currently \"locked\""
  echo "by another THOR server:"
  echo ""
  for f in $lockfiles ; do
    echo "   $f" | sed -e 's/\.LCK/.THOR/'
  done
  echo ""
  echo "Locked databases can be due to either a THOR server currently"
  echo "running, or by a \"crashed\" THOR server.  If you are SURE that"
  echo "another THOR server is not running (i.e. the locks are no longer"
  echo "legitimate), the locks can be removed."
  echo "--------------------------------------------------------------------"
  echo ""
  echo "Do you want to remove the locks? (type \"yes\" to confirm):"
  read line
  if [ $? != 0 ] ; then exit 1; fi
  if [ "$line" != "yes" -a "$line" != "y" ] ; then
    echo "Locks NOT removed."
    echo "NOTE: You may not be able to access the databases listed above."
    echo "Do you wish to proceed anyway? (yes):"
    read line
    if [ $? != 0 ] ; then exit 1; fi
    case $line in
      [nN]*) exit 1;;
    esac
  else
    rm -i $lockfiles
    echo "Locks removed."
  fi
fi


#######################################################################
# Try to find the license file.
#

if [ -z "$DY_LICENSEDATA" ] ; then
  DY_LICENSEDATA=$DY_ROOT/etc/dy_license.dat  # initial guess
fi
$DY_ROOT/bin/testlicense -p any
until [ $? = 0 ] ; do
  echo ""
  echo "Tried \"$DY_LICENSEDATA\", invalid license."
  echo "Enter file that contains the Daylight license:"
  read line
  if [ $? != 0 ] ; then exit 1; fi
  DY_LICENSEDATA=$line
  export DY_LICENSEDATA
  $DY_ROOT/bin/testlicense -p any
done
echo "\$DY_LICENSEDATA = $DY_LICENSEDATA"

#######################################################################
# Location for the THOR and Merlin servers' log files.  The servers
# log connections, failed logins, database openings, and so forth,
# to this file.
#

: ${DY_MERLIN_LOG_FILE:=/tmp/merlinserver.log}
: ${DY_THOR_LOG_FILE:=/tmp/thorserver.log}
export DY_MERLIN_LOG_FILE DY_THOR_LOG_FILE

echo "\$DY_MERLIN_LOG_FILE = $DY_MERLIN_LOG_FILE"
echo "\$DY_THOR_LOG_FILE   = $DY_THOR_LOG_FILE"
echo ""

#######################################################################
# For non-Sun systems, merge the XView options in into the user's X
# resources database.  This uses the .Xdefaults supplied as part
# of $DY_ROOT/contrib.
#

if [ $is_sun = FALSE ] ; then
  echo ""
  echo "SGI and Linux machines usually don't have OpenWindows parameters defined"
  echo "in their X server resources database.  If you answer \"yes\" here, the"
  echo "OpenWindows parameters will be defined for you.  If you answer \"no\""
  echo "the X programs will work, but they may default to a poor-looking fonts"
  echo "and colors with poor contrast".
  echo ""
  echo "Do you want to define the OpenWindows parameters? (default \"yes\"):"
  read line
  if [ $? != 0 ] ; then exit 1; fi
  if [ "$line" != "no" -a "$line" != "n" ] ; then
    case $osversion in
      SGI*)  xrdb $DY_ROOT/thorhome/sgi/.Xdefaults
  ;;
      Linux*)   xrdb $DY_ROOT/thorhome/linux/.Xdefaults
  ;;
    esac
    if [ $? != 0 ] ; then
      echo "WARNING: xrdb returned an error -- parameters may not be set!"
    else
      echo "OpenWindows parameters defined."
    fi
  fi

  # define EXTRAS_MENU for non-SUN systems
  EXTRASMENU=$DY_ROOT/etc/.text_extras_menu
  export EXTRASMENU

fi

#######################################################################
# Find demo dbs for merlinloading.
# 

DEMODBS=""
files=`ls $DY_THORDB/*.THOR | grep demo | grep -v _ `
for file in $files ; do
  file=`basename $file`
  file=`echo $file | sed -e 's/\.THOR$//`
  DEMODBS="$DEMODBS $file"
done

#######################################################################
# Start the servers.  Check each license before trying to start the
# corresponding program.  Write a script
# "faststop" that can be used to kill everything.
#

echo "Creating script \"$DY_ROOT/bin/faststop\"..."
(echo "#!/bin/sh"
 echo "# faststop - stop the processes started by faststart."
 echo "# This script is re-created each time you run faststart."
) >$DY_ROOT/bin/faststop
chmod 744 $DY_ROOT/bin/faststop

$DY_ROOT/bin/thorping 2>&1 >/dev/null
thorstatus=$?
echo "DEBUG: thorstatus = $thorstatus"
if [ $thorstatus -eq 0 ] ; then
  echo "The thorserver is already running and will not be restarted..."
else
  $DY_ROOT/bin/testlicense -s thor
  if [ $? = 0 -a -x $DY_ROOT/bin/thorserver ] ; then
    if [ -f $DY_THOR_LOG_FILE -a ! -w $DY_THOR_LOG_FILE ] ; then
      echo "ERROR: $DY_THOR_LOG_FILE is not writeable!"
      echo "       You must remove $DY_THOR_LOG_FILE."
      exit 1
    fi
    echo "Starting thorserver..."
    $DY_ROOT/bin/thorserver &
    server_ids="$!"
    sleep 3
    $DY_ROOT/bin/thorping
    thorstatus=$?
    $DY_ROOT/bin/thorserv_monitor
  fi
fi


$DY_ROOT/bin/merlinping 2>&1 >/dev/null
merlinstatus=$?
if [ $merlinstatus -eq 0 ] ; then
  echo "The merlinserver is already running and will not be restarted..."
else
  $DY_ROOT/bin/testlicense -s merlin
  if [ $? = 0 -a -x $DY_ROOT/bin/merlinserver ] ; then
    if [ -f $DY_MERLIN_LOG_FILE -a ! -w $DY_MERLIN_LOG_FILE ] ; then
      echo "ERROR: $DY_MERLIN_LOG_FILE is not writeable!"
      echo "       You must remove $DY_MERLIN_LOG_FILE."
      exit 1
    fi
    echo "Starting merlinserver..."
    $DY_ROOT/bin/merlinserver $DEMODBS &
    server_ids="$server_ids $!"
    sleep 2
    echo ""
    echo "-----------------------------------------------------------"
    echo "Loading: $DEMODBS..."
    echo "This may take several minutes..."
    echo "-----------------------------------------------------------"
    echo ""
    $DY_ROOT/bin/merserv_monitor
  fi
fi

# 'ping' the server until it is ready.
# If we get a "busy loading database..." error, try again.
# Any other error is a problem.

DY_SECURE_PASSWORDS=FALSE
sleep 5  
while true ; do
  $DY_ROOT/bin/merlinping 2>&1 > /dev/null
  merlinstatus=$?
  echo "loading databases..."
  if [ $merlinstatus -eq 0 ] ; then
    echo ""
    echo "done loading databases"
    echo ""
    break    # done loading pool
  else
    sleep 3
  fi
done


if [ $thorstatus -ne 0 -o $merlinstatus -ne 0 ] ; then
  echo "--------------------------------------------------------------"
  echo "              ***** OUCH! PROBLEM! ****"
  echo "One or both of the Thor and Merlin servers failed to start."
  echo ""
  echo "This can be due to: lack of execute permission, no permission"
  echo "to open the $DY_ROOT/etc/dy_passwords.dat file,"
  echo "trying to run as \"root\", to not enough memory on your"
  echo "system, or other limitations."
  echo ""
  echo "Check for error messages above this message.  If you can't"
  echo "determine the problem's cause, contact Daylight at"
  echo "(USA) 505-989-1000 or e-mail to support@daylight.com."
  echo "--------------------------------------------------------------"
  exit 1
fi

$DY_ROOT/bin/testlicense -p thor
if [ $? = 0 -a -x $DY_ROOT/bin/xvthor ] ; then
  echo "Starting xvthor..."
  $DY_ROOT/bin/xvthor &
  process_ids="$!"
fi

# On SGI using 4dwm, have to start xvmerlin without -iconic, as 
# otherwise the window manager likes to iconify the keypad every time
# you use it.  Yuck.

$DY_ROOT/bin/testlicense -p merlin
if [ $? = 0 -a -x $DY_ROOT/bin/xvmerlin ] ; then
  echo "Starting xvmerlin..."
  $DY_ROOT/bin/xvmerlin -PROFILE "" &
  process_ids="$process_ids $!"
fi

$DY_ROOT/bin/testlicense -p pcmodels
if [ $? = 0 -a -x $DY_ROOT/bin/xvpcmodels ] ; then
  echo "Starting xvpcmodels..."
  $DY_ROOT/bin/xvpcmodels &
  process_ids="$process_ids $!"
fi


$DY_ROOT/bin/testlicense -p thormanager
if [ $? = 0 -a -x $DY_ROOT/bin/sthorman ] ; then
  echo "Starting sthorman..."
  xterm -bg grey30 -fg white -cr white -rw -sb -sl 1000 \
  -name sthorman -title sthorman \
  -e $DY_ROOT/bin/sthorman &
fi

(echo "echo \"killing X programs...\""
 echo "kill $process_ids"
 echo "sleep 10"
 echo "echo \"killing server programs...\""
 echo "kill $server_ids"
 echo "sleep 2"
 echo "echo \"removing \$DY_ROOT/bin/faststop...\""
 echo "rm $DY_ROOT/bin/faststop"
 echo "echo \"\$0: done.\""
) >>$DY_ROOT/bin/faststop

sleep 3

echo ""
echo "================================================================"
echo "|  All Daylight X applications have been started; their"
echo "|  icons will appear momentarily.  "
echo "|"
echo "|"
echo "|  To kill the demo programs, use:"
echo "|          $DY_ROOT/bin/faststop"
echo "================================================================"
echo ""



echo "...done."


exit
