#!  /usr/bin/ksh
#----------------------------------------------------------------------------
# script for installing SICS software on to a system.
# The source of the data must 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
# focus                - install all files for focus
# sans                 - installs all files for sans 
# trics                - installs all files for TRICS 
# hrpt                 - installs all files for HRPT
# poldi                - installs all files for POLDI
# 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
# DM.  7-Sep-2000.   Add POLDI.
#----------------------------------------------------------------------------
#=========================== data area ====================================
devdir=~koenneck                     # development directory

disdir=~lnslib/src/sics              # distribution directory

dirdir="bin data doc log motor"      # SICS directories

bindir=~lnslib/bin              # common directory for binaries

clients="sicsclient.jar powderstatus.jar sansstatus.jar topsistatus.jar"
clients="$clients varwatcher.jar focusstatus.jar nndb.jar amor.jar fsync.jar"

clientdir=~koenneck/bin/sics   
# directory where the client jar files live

docmaster=~koenneck/src/sics/doc  # where the docs reside
#======================= 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"
    printf "Do you want me to continue (Y,y) or abort (any other character): "
    read answer
    if [[ ($answer = Y) || ($answer = y) ]] then
      echo "OK, you have been warned ..."
    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 "trics                - installs all files for trics "
echo "hrpt                 - installs all files for hrpt "
echo "amor                 - installs all files for amor"
echo "focus                - installs all files for focus"
echo "poldi                - installs all files for poldi"
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-October 1998, March 2000"
}
#------------------ copy new executables
copydev () {
 echo "Copying new executable files from development area to distribution.."
 cp $devdir/bin/sics/* $disdir/gen
 for cl in $clients; do
   cp $clientdir/$cl $disdir/gen
 done
}
#------------------ copy to common program area
copylib() {
  echo "Copying new client files to common LNS program area"
  cp $disdir/gen/*     $bindir
  rm $bindir/SICServer
  rm $bindir/TecsServer
# this is to overwrite the client start shell scripts with ones that point
# to lnslib 
  cp $disdir/bin/lib/* $bindir
}
#------------------ copy all new stuff
copydevfull () {
 echo "Copying ALL files from development area to distribution.."
 copydev
 echo "Copying documentation"
 cp $docmaster/user/*.htm $disdir/doc
 cp $docmaster/user/*.html $disdir/doc
 cp $docmaster/user/*.ps  $disdir/doc
 cp $docmaster/manager/*.htm $disdir/manager
}
#------------------ copy only new executables
copyexe () {
 echo "Copying new executable files"
 miau.py ~lnslib/src/sics/gen/genlist.dat
}
#----------------- 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"
 miau.py ~lnslib/src/sics/dmc/dmclist.dat
 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"
 miau.py ~lnslib/src/sics/topsi/topsilist.dat
 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"
 miau.py ~lnslib/src/sics/sans/sanslist.dat
 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"
}
#----------------- TRICS special files
copytrics() {
 echo "Copying TRICS special files"
 miau.py ~lnslib/src/sics/trics/tricslist.dat
 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"
}
#----------------- HRPT special files
copyhrpt() {
 echo "Copying HRPT special files"
 miau.py ~lnslib/src/sics/hrpt/hrptlist.dat
 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"
}
#----------------- FOCUS special files
copyfocus() {
 echo "Copying FOCUS special files"
 miau.py ~lnslib/src/sics/focus/focuslist.dat
 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"
}
#----------------- AMOR special files
copyamor() {
 echo "Copying AMOR special files"
 miau.py ~lnslib/src/sics/amor/amorlist.dat
 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"
}
#----------------- POLDI special files
copypoldi() {
 echo "Copying POLDI special files"
 miau.py ~lnslib/src/sics/poldi/poldilist.dat
 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
}
#======================= End of Functions ====================================
#
#-------------- the case for checking the command parameter

  printf "\n  Starting `basename $0` ...\n\n"

  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
      ;;
    trics)
      checkhome
      checkdir
      copyexe
      copydoc
      copytrics
      ;;
    hrpt)
      checkhome
      checkdir
      copyexe
      copydoc
      copyhrpt
      ;;
    amor)
      checkhome
      checkdir
      copyexe
      copydoc
      copyamor
      ;;
    focus)
      checkhome
      checkdir
      copyexe
      copydoc
      copyfocus
      ;;
    poldi)
      checkhome
      checkdir
      copyexe
      copydoc
      copypoldi
      ;;
    lib)
      copylib
      ;;
    debug)
      cp $devdir/SICServer .
      ;;
    *)
      usage
      ;;
  esac

  printf "\n  End of `basename $0`.\n\n"

#========================= End of sicsinstall ================================
