#!/usr/bin/ksh
#----------------------------------------------------------------------------
# script for installing SICS software on to a system.
# The source of the data muste be visible to the filesystem.
# This script installs SW into directories beyond your current directory
# So, on an instrument account it would be feasible to run this from the
# home directory. The script checks if we are at home, else it will print
# a warning.
#
# this script understands a few arguments:
# 
# dev                  - copies new executables from development area to
#                        distribution area
# devfull              - copies all files from development area to 
#                        distribution area
# dmc                  - installs all files for running dmc
# topsi                - install all files for topsi
# sans                 - installs all files for sans 
# doc                  - updates only the documentation
# exe                  - installs only new executables
# debug                - copies only the server to current directory
# lib                  - copies clients from the distribution area to 
#                        the common program library.
#
# Mark Koennecke, Juli 1998
#----------------------------------------------------------------------------
#=========================== data area ====================================
devdir=/data/koenneck/src/sics     # development directory

disdir=/data/lnslib/src/sics       # distribution directory

dirdir="bin data doc log"          # SICS directories

bindir=/data/lnslib/bin            # common directory for binaries

clients="sicsclient.jar powderstatus.jar sansstatus.jar topsistatus.jar"
#======================= Functions ========================================
#------------------ check if in home directory
checkhome() {
if [ $HOME != $(pwd) ]; then
   echo "You are not in your home directory."
   echo "This may cause a non standard SICS installation"
   echo "Do you want me to continue(Y,y) or abort(any other character)"
   read answer
   if [ \( $answer = Y \) -o \( $answer = y \) ]; then
     echo "OK, I start clobbering......"
   else
     echo "Script aborting........."
     exit 0
   fi 
fi
}

#------------------- print usage
usage() {
echo "USAGE:"
echo "This script copies SICS software. You need to tell this program"
echo "what to do by specifying one of the keywords below:"
echo "" 
echo "dev                  - copies new executables from development area to"
echo "                       distribution area (MK only)"
echo "devfull              - copies all files from development area to"
echo "                       distribution area (MK only)"
echo "dmc                  - installs all files for running dmc"
echo "topsi                - install all files for topsi"
echo "sans                 - installs all files for sans "
echo "doc                  - updates only the documentation"
echo "exe                  - installs only new executables"
echo "lib                  - copies new clients to common program area"
echo ""
echo "Mark Koennecke, LNS, Juli 1998"
}
#------------------ copy new executables
copydev () {
 echo "Copying new executable files from development area to distribution.."
 cp $devdir/SICServer $devdir/psish/psish $devdir/psish/pish $disdir/bin
 for cl in $clients; do
   cp $devdir/SicsClient/$cl $disdir/bin
 done
 cp $devdir/replicator/replicator.jar $disdir/bin
}
#------------------ copy to common program area
copylib() {
  echo "Copying new client files to common LNS program area"
  cp $disdir/bin/*.jar $bindir
}
#------------------ copy all new stuff
copydevfull () {
 echo "Copying ALL files from development area to distribution.."
 copydev
 echo "Copying documentation"
 cp $devdir/user/*.htm $disdir/doc
 cp $devdir/user/*.html $disdir/doc
 cp $devdir/user/*.ps  $disdir/doc
 cp $devdir/manager/*.htm $disdir/manager
}
#------------------ copy only new executables
copyexe () {
 echo "Copying new executable files"
 cp $disdir/bin/SICServer $disdir/bin/psish $disdir/bin/pish bin
 for cl in $clients; do
   cp $disdir/bin/$cl bin
 done
 cp $disdir/bin/sics bin
 cp $disdir/bin/powderstatus bin
 cp $disdir/bin/sansstatus bin
 cp $disdir/bin/topsistatus bin
 cp $disdir/bin/replicator.jar
}
#----------------- copy documentation
copydoc () {
 echo "Copying documentation"
 cp $disdir/doc/*.htm doc
 cp $disdir/doc/*.html doc
 cp $disdir/doc/*.ps doc
}
#----------------- DMC special files
copydmc () {
 echo "Copying DMC special files"
 cp $disdir/bin/count.tcl  $disdir/bin/init.tcl \
    $disdir/bin/log.tcl $disdir/bin/xydialog.tcl $disdir/bin/CheckSICS bin
 cp $disdir/bin/dmc/* bin
 if [ -e data/DataNumber ]; then
    echo "DataNumber found, OK"
 else
    echo "WARNING: Creating new DataNumber file with 0, adjust as needed"
    echo "   0" > data/DataNumber
 fi
 echo "Done, configuration files may need editing"
}
#----------------- TOPSI special files
copytopsi () {
 echo "Copying TOPSI special files"
 cp $disdir/bin/count.tcl  $disdir/bin/init.tcl \
    $disdir/bin/log.tcl $disdir/bin/CheckSICS bin
 cp $disdir/bin/topsi/* bin
 if [ -e data/DataNumber ]; then
    echo "DataNumber found, OK"
 else
    echo "WARNING: Creating new DataNumber file with 0, adjust as needed"
    echo "   0" > data/DataNumber
 fi
 echo "Done, configuration files may need editing"
}
#----------------- SANS special files
copysans() {
 echo "Copying SANS special files"
 cp $disdir/bin/count.tcl  $disdir/bin/init.tcl \
    $disdir/bin/log.tcl $disdir/bin/CheckSICS bin
 cp $disdir/bin/sans/* bin
 if [ -e data/DataNumber ]; then
    echo "DataNumber found, OK"
 else
    echo "WARNING: Creating new DataNumber file with 0, adjust as needed"
    echo "   0" > data/DataNumber
 fi
 echo "Done, configuration files may need editing"
}

#------------------ check SICS directories
checkdir() {
  echo "Checking directory structure, creating as needed"
  for dd in $dirdir; do
    if [ -d $dd ]; then
       echo "$dd present"
    else
       echo "Creating $dd"
       mkdir $dd
    fi  
  done
}
#-------------- the case for checking the command parameter
case ${1-nix} in 
   dev)
      copydev
      ;;
   devfull)
      copydevfull
      ;;
    exe)
      checkhome
      checkdir
      copyexe
      ;;
    doc)
      checkhome
      checkdir
      copydoc
      ;;
    dmc)
      checkhome
      checkdir
      copyexe
      copydoc
      copydmc
      ;;
    topsi)
      checkhome
      checkdir
      copyexe
      copydoc
      copytopsi
      ;;
    sans)
      checkhome
      checkdir
      copyexe
      copydoc
      copysans
      ;;
    lib)
     copylib
     ;;
    debug)
      cp $devdir/SICServer .
      ;;
   *)
      usage
    ;;
esac
echo "sicsinstall finished with more or less success"
