#!/bin/csh
#
###################################################################
#
#   Installation Script for GENESIS
#   -------------------------------
#
###################################################################



###################################################################
# set global variables
#
#
set VER  = 34
set VERSION = 3.4
set INSTALLDIR = $SystemDrive/genesis

  set SYS = WINDOWS_NT
  set SYSID = `nodeid | grep Node | cut -c 16-`
  set MEDIA = /tmp/get.nt.$VERSION
  set WHOAMI = $USERNAME
  set HOSTNAME = `hostname`
###################################################################

###################################################################
# check if we are root
#
if ($WHOAMI != 'Administrator' ) then
  echo Please log in as Administrator before running this script.
  goto error
endif
###################################################################

###################################################################
# welcome page
#
clear
echo "*********************************************************"
echo "*                                                       *"
echo "* Welcome to the GENESIS Version $VERSION Installation. *"
echo "*                                                       *"
echo "*********************************************************"
echo " "
while 1
  echo  -n "Are you ready to continue (y(es)/n(o)) ? :"
  set ANSWER=$<
  if ($ANSWER =~ [Nn]*) goto error
  if ($ANSWER =~ [Yy]*) break 
end
echo " "
###################################################################

###################################################################
# check we are on GENESIS SERVER
#
while 1
  echo -n "Will this station ($HOSTNAME) be the GENESIS server (y(es)/n(o)) ? :"
  set ANSWER=$<
  if ($ANSWER =~ [Nn]*) then
    echo "To install a client, first install on the server and then create a link"
    echo "to the GENESIS server installation directory."
    goto error
  endif
  if ($ANSWER =~ [Yy]*) break
end
echo " "
###################################################################

###################################################################
# get installation media source
#
#
echo -n "Please enter the installation media source [$MEDIA] :"
set ANSWER=$<
echo " "
if ($ANSWER != "") then
  set MEDIA = $ANSWER
endif
echo "Installing from $MEDIA."
###################################################################

###################################################################
# Get installation directory
#
echo "The complete installation set will need about 80Mb disk space"
echo -n "Please enter installation directory [$INSTALLDIR] :"
set ANSWER=$<
echo " "
if ($ANSWER != "") then
  set TMPDIR = $ANSWER
  set PARENT_DIR = $TMPDIR:h
  if ( $PARENT_DIR != "" ) then
    if ( ! -e $PARENT_DIR ) then
      while 1	
        echo -n "Directory $TMPDIR:h does not exist, create it (y(es)/n(o)) ? :"
        set ANSWER=$<
        if ($ANSWER =~ [Nn]*) goto error
        if ($ANSWER =~ [Yy]*) break
      end
      echo " "
      mkdir -p $PARENT_DIR
    endif
  endif
  set INSTALLDIR = $TMPDIR
endif

###################################################################
# check existence of previous installation
#
if ( -e $INSTALLDIR/share ) then
  while (1)
    echo "GENESIS seems to have been installed in $INSTALLDIR already \!"
    echo "Do you wish to overwrite the existing installation or just update"
    echo -n "the version (o(verwrite) u(pdate) q(uit)) ? :"
    set ANSWER=$<
    echo " "
    if ($ANSWER =~ [Qq]*) goto error
    if ($ANSWER =~ [Oo]*) then
      set UPDATE = 0
      break
    endif
    if ($ANSWER =~ [Uu]*) then
      set UPDATE = 1
      break
    endif
  end
else
  set UPDATE = 0
endif
##################################################################

###################################################################
# read files form media
#
while 1
 echo -n "Begin installation from $MEDIA (y(es)/n(o)/s(kip)) ? :"
 set ANSWER=$<
 if ($ANSWER =~ [Nn]*) goto error
 if ($ANSWER =~ [Yy]*) break
 if ($ANSWER =~ [Ss]*) goto rcupdate
end
echo " "

#
# clean the installation directory
#
if (! $UPDATE && -e $INSTALLDIR) then
  while 1
    echo -n "Delete previous installation directory ($INSTALLDIR) (y(es)/n(o)) ? :"
    set ANSWER=$<
    if ($ANSWER =~ [Nn]*) break
    if ($ANSWER =~ [Yy]*) then
      rm -r $INSTALLDIR
      break
    endif
  end
endif

if (! -e $INSTALLDIR) then
  mkdir $INSTALLDIR
  if ($status) then
    echo "Error creating directory $INSTALLDIR, aborting installation"
    goto error
  endif
endif

#
#   Read from the media:  
#
#
chdir $INSTALLDIR
if ($UPDATE) then
  #
  # if version already exists then
  # move it aside and inform operator.
  #
  if ( -e e$VER ) then
    echo ""
    echo "Executable directory ($INSTALLDIR/e$VER) already exists,"
    echo "moving it to $INSTALLDIR/e$VER.SAVE ..."
    if ( -e e$VER.SAVE) then
      rm -r e$VER.SAVE
    endif
    mv e$VER e$VER.SAVE
  endif

  set XDIR = "e$VER install"
else
  set XDIR = 
endif

#

echo ""
echo "Reading media..."
tar xvf $MEDIA $XDIR

###################################################################

###################################################################
# set permissions of writeable files.
# open them to 777 as we do not know which
# users/groups to allow access
#
chdir $INSTALLDIR
set OPENDIRS = (share fw hosts logs tmp)
if (! $UPDATE) then
  foreach OPD ($OPENDIRS)
    chmod -R 777 $OPD
  end
endif
set OPENDIRS = (e$VER/prog e$VER/prog/*)
if (! $UPDATE) then
  foreach OPD ($OPENDIRS)
    chmod 777 $OPD
  end
endif
###################################################################

echo -n "Update system for automatic GENESIS daemon startup (y(es)/n(o)) ? :"
while 1
  set ANSWER=$< 
  if ($ANSWER =~ [Nn]*) break
  if ($ANSWER =~ [Yy]*) then
     $GENESIS_EDIR/gnd/gnd.exe -install
  endif
end
echo " "
endif
###################################################################

###################################################################
# see if there is anything to update
#
if ($UPDATE && -e install/update.$VER) then
  source install/update.$VER
endif
###################################################################


###################################################################
# update cshrc.target file
#
#
set CSHRCFILE = $INSTALLDIR/e$VER/all/cshrc.target
if ( -e $CSHRCFILE) then
   cp $CSHRCFILE $CSHRCFILE.$$
   sed "s~setenv .*GENESIS_VER .*~setenv GENESIS_VER $VER~g" $CSHRCFILE.$$ > $CSHRCFILE
   cp $CSHRCFILE $CSHRCFILE.$$
   sed "s~setenv .*GENESIS_DIR .*~setenv GENESIS_DIR $INSTALLDIR~g" $CSHRCFILE.$$ > $CSHRCFILE
   rm $CSHRCFILE.$$

   # inform operator of need to source.
   echo " "
   echo "The file $CSHRCFILE can be sourced to provide"
   echo "the necessary environment to run the GENESIS."
   echo " "
endif

###################################################################


###################################################################
# update license
#
#
###################################################################
set DONGLE_INFO = $INSTALLDIR/e$VER/misc/dongle_info
set TEMP_DONGLE_INFO = "/tmp/dongle_$$" 
echo "The license installation is split into two parts"
echo "The first part check whether you have a dongle installed" 

dongle:
while 1 
   echo -n "Checking for a dongle..." 
   $DONGLE_INFO >& $TEMP_DONGLE_INFO
   if (! -e $TEMP_DONGLE_INFO) then 
     echo "Could not write to file $TEMP_DONGLE_INFO - cannot continue with dongle setup"
     goto enddongle
   endif     
   grep ERROR $TEMP_DONGLE_INFO
   set value = $status 
   if ($value == 0) then
        set DONGLE_FOUND = 0 
        echo "\n\n"
        cat  $TEMP_DONGLE_INFO
   Q1:
	echo "\nThere is a problem with the dongle or with the serial port"
        echo "You can check the dongle in future by running $DONGLE_INFO"
	echo -n "Do you want to try the dongle again? [Y] " 
        set ANSWER=$<
        echo " "
        if ($ANSWER == "") goto dongle
        if ($ANSWER =~ [Nn]*) goto enddongle
        if ($ANSWER =~ [Yy]*) goto dongle
	goto Q1 
   endif
   if ($value != 0) then 
     set DONGLE_FOUND = 1 
     echo "\n\nDongle found" 
     foreach a (`cat $TEMP_DONGLE_INFO`) 
       if ($a =~ ????-????-????-????) then
            set LICNAME = L$SYSID"."$a 
       endif
     end   

     echo "The full license name is $LICNAME" 
     break 
   endif
end
enddongle:

license:
while 1
   echo -n "Do you want to install the license file? [Y] " 
   set ANSWER=$<
   echo " "
   if ($ANSWER == "") set ANSWER = "Y" 
   if ($ANSWER !~ [NnYy]*) goto license
   if ($ANSWER =~ [Nn]*) goto endlicense
   # If there is no dongle then we don't know the basename of the license file
   if $DONGLE_FOUND == 0 then
     echo "Please give the full license name" 
     echo "\n\n\nInstructions for the license file"
     echo "Remember, the license name file MUST be of the form L$SYSID.1234-1234-1234-1234"
     echo "The first part is the node id, and the second part is the button id that you"
     echo "see printed when you run $DONGLE_INFO"
     echo -n "Enter the full file name "
     set ANSWER=$<
     echo " "
   
     set NEWLICFILE = $ANSWER
     if (! -e $NEWLICFILE) then
      echo "The file you have chosen does not exist"
      goto license
     endif      
     if $NEWLICFILE !~ *L??????.????-????-????-???? then
       echo "The filename is not of the form  L123456.1234-1234-1234-1234"
       goto license
     endif
     set LICDIR = $INSTALLDIR/share/license
     echo "Copying the license file from $NEWLICFILE to $LICDIR" 
     cp $NEWLICFILE $LICDIR
     if ($status) then
      echo "The copy of the license file from $NEWLICFILE to $LICDIR FAILED"
      echo "You will have to copy it manually"
     endif
     set NEWLICBASE = $NEWLICFILE:t
     chmod a+r $LICDIR/$NEWLICBASE

   endif
   if ($DONGLE_FOUND == 1) then
     echo "The name of the license file is $LICNAME" 
     echo -n "Enter the directory where it can be found " 
     set DIR=$<
     echo " "
     if (! -d $DIR) then
       echo "$DIR is not a directory"
       goto license 
     endif 

     set LICDIR = $INSTALLDIR/share/license
     echo "Copying the license file from $DIR/$LICNAME to $LICDIR" 
     cp $DIR/$LICNAME $LICDIR
     if ($status) then
      Q2:
      echo "The copy of the license file from $DIR/$LICNAME to $LICDIR FAILED"
      echo "You will have to copy it manually"
      echo -n "Do you want to try again? [Y] "
      set ANSWER=$<
      echo " "
      if ($ANSWER == "") set ANSWER = "Y"
      if ($ANSWER =~ [Nn]*) goto endlicense
      if ($ANSWER =~ [Yy]*) goto license
      goto Q2 
     endif
     chmod a+r $LICDIR/$LICNAME
 endif
  goto endlicense
end
endlicense:

###################################################################
# exit if this is just an update installation
#
if ($UPDATE) goto end
###################################################################

###################################################################
# define system admin user
# and group
#
chdir $INSTALLDIR/share
echo -n "Please Enter system administrator GENESIS login name :"
set LOGIN_NAME  = $<
echo -n "Please Enter password :"
set LOGIN_PASSWD  = $<
echo -n "Please Enter system administrator GENESIS group name :"
set GROUP_NAME  = $<

echo "OPRS {"                      > users
echo "    NAME=$LOGIN_NAME"       >> users
echo "    PASSWD=$LOGIN_PASSWD"   >> users
echo "    PRIV=100"               >> users
echo "    AUTOLOGOUT=0"           >> users
echo "    GROUP=$GROUP_NAME"      >> users
echo "    REAL_NAME=Sys Admin"    >> users
echo "}"                          >> users

echo "GRPS {"                      > groups
echo "    NAME=$GROUP_NAME"       >> groups
echo "    PRIV=100"               >> groups
echo "    AUTOLOGOUT=0"           >> groups
echo "    DESC=Sys Admin Group"   >> groups
echo "}"                          >> groups
###################################################################

###################################################################
#
# initialize databases
#
#
chdir $INSTALLDIR/share
echo "JOBS {"                 > joblist
echo "    NAME=genesislib"   >> joblist
echo "    DB=genesis"        >> joblist
echo "    ACCESS="           >> joblist
echo "}"                     >> joblist

chdir $INSTALLDIR/sys
echo "DBS {"                  > dblist
echo "  NAME=genesis"        >> dblist
echo "  PATH=$INSTALLDIR/fw" >> dblist
echo "  ACCESS="             >> dblist
echo "}"                     >> dblist


###################################################################
#
# add databases
#
#
chdir $INSTALLDIR/sys
set prefix = a
while 1
  echo -n "Do you wish to add $prefix database to the system (y(es)/n(o)) ?"
  set ANSWER=$<
  echo " "
  if ($ANSWER =~ [Nn]*) break
  if ($ANSWER =~ [Yy]*) then
    set prefix = another
    echo -n "Enter db name :"
    set DBNAME=$<
    #
    # check that db is not already defined
    #
    set DBDEF = `grep NAME=$DBNAME dblist | wc -l`
    if ($DBDEF != 0) then
      echo "Database $DBNAME already exists in dblist."
      continue
    endif
    echo -n "Enter db path (e.g. /disk/fw) :"
    set DBPATH=$<
    if ( ! -e $DBPATH) then
      echo -n "directory $DBPATH does not exist, create it (y(es)/n(o)) ?"
      set ANSWER=$< 
      if ($ANSWER =~ [Nn]*) continue
      if ($ANSWER =~ [Yy]*) then
        mkdir $DBPATH
      endif
    endif
    echo "Creating jobs directory ..."
    mkdir $DBPATH/jobs
    echo "Adding database $DBNAME to $INSTALLDIR/sys/dblist ..."

    echo "DBS {"                >> dblist
    echo "  NAME=$DBNAME"       >> dblist
    echo "  PATH=$DBPATH"       >> dblist
    echo "  ACCESS="            >> dblist
    echo "}"                    >> dblist
    
  endif
end

###################################################################


###################################################################
#
# update kernel
#
#
# these params need to be updated
#
# options SEMMNI=64
# options SEMMNS=200
# options SEMUME=100

else if ($SYS == HP || $SYS == HP10) then
  echo "Update the following Configuration params through sam"
  echo "and reboot the system: These values represent the minumum"
  echo "values. If a value is larger than specified, it should not"
  echo "be reduced"
  echo " "
  echo "maxdsiz   =      0x10000000"
  echo "semmns    =      200"
  echo "semume    =      100"
endif

goto end

###################################################################

error:

echo ""
echo "Installation Aborted \!\!"
echo ""
exit 1

end:

echo ""
echo "             IMPORTANT" 
echo "             =========" 
echo "Before running Genesis, please ensure that the dongle has been" 
echo "inserted into one of the serial ports of the gnd server."
echo "Also, ensure that the license file for the dongle is in directory"
echo "$INSTALLDIR/share/license with the correct name." 
echo "The file name looks like L123456.1234-1234-1234-1234"
echo "If you want to check that the dongle works, run the dongle_info program"
echo "which can be found in $INSTALLDIR/e${GENESIS_VER}/misc/dongle_info" 
echo "For more detailed information look in kit item 2409"
echo "" 

echo ""
echo "Installation completed successfully. Please reboot the system."
echo ""
exit 0
