#!/bin/ksh -pu
#
# ident "@(#)utinstall.ksh	1.78 02/10/30 SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#

#
# utinstall
#
# master task to install/uninstall the Sunray software and the various bundles
# associated with it.
#

trap "CleanupAndExit 1" HUP INT QUIT TERM

#
# Usage()
#
# Description:
#    Shows usage string
#
# Parameters:
#    (none)
#

Usage() {
  print -u2 "Usage: $G_PROGRAM_ID $PROGRAM_OPTS"
  print -u2 ""
  print -u2 " -a admin_file : define a new admin file for pkgadd"
  print -u2 " -d media_dir  : specify a directory to install the"
  print -u2 "                 software from"
  print -u2 " -u            : uninstall the software"
  print -u2 " -q            : quick install (using defaults)"
           
  CleanupAndExit 1
}

#
# CleanupAndExit()
#
# Description:
#    clean up procedure before exiting. Send Exit event in case of normal
#    termination (error code 0). Abort event otherwise (failure or 
#    interruption)
#
# Parameters:
#    $1 : exit code
#

CleanupAndExit() {
  trap "" HUP INT QUIT TERM
 
  #
  # remove all temporary files created
  #
  rm -rf ${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.* 1> /dev/null 2>&1

  #
  # remove preserve/upgrade temporary files if procedure terminated 
  # successfully and send termination event (Exit or Abort) to all modules to 
  # do their cleanup procedure
  #
  if [[ $1 = "0" ]]; then

     rm -rf ${G_UPGRADE_DIR}/preserve 1> /dev/null 2>&1

     if [[ $G_MODE = "install" ]]; then
        rm -rf ${G_UPGRADE_DIR} 1> /dev/null 2>&1
     fi
     SendEventToModule Exit
  else
     SendEventToModule Abort
  fi

  # eventually, remove support_lib and modules from the temporary directory
  # if uninstalling the software
  #
  if [[ $G_MODE = "uninstall" ]]; then
     rm -rf ${G_TMP_DIR}/support_lib > /dev/null 2>&1
     rm -rf ${G_TMP_DIR}/iu_modules > /dev/null 2>&1
  fi

  #
  # This function (CleanupAndExit) can be called from StartInstall (a function
  # which is in pipe with tee). The exit code cannot be seen by the main script
  # (utinstall) as the last task of the "pipeline" is tee. So, a temporary
  # file is created to communicate the error code to the main script 
  # (utinstall). Of course, this file is created if needed (just check if tee 
  # is running...)
  
  if ps -f | grep tee | grep ${G_PID} > /dev/null 2>&1 ; then
     echo $1 > $ERROR_FILE
  fi

  exit $1
}

#
# PrintFinalMessage()
#
# Description:
#    Just to print a message at the end of installation/uninstallation
#
# Parameters:
#    (none)
#

PrintFinalMessage() {

   if [[ $G_MODE = "install" ]]; then

      fmt <<-!

	Installation of $G_UT_PROD_NAME has completed.

	The system must be rebooted in order to complete this installation
	and before starting the $G_UT_PROD_NAME services.

	!

   elif [[ $G_MODE = "uninstall" ]]; then

      print "The system must be rebooted in order to complete un-install."

   fi
}

#
# CheckDirectories()
#
# Description:
#    Check existence of install directories (support_lib, modules)
#
# Parameters:
#    (none)
#
# Globals used:
#    G_MEDIA_DIR

CheckDirectories() {

   # Check existence of support_lib dir. if it doesn't, libraries cannot be
   # sourced
   if [[ ! -d $G_MEDIA_DIR/support_lib ]]; then
      echo "$0: Error, cannot find support_lib directory"
      exit 1
   fi

   # Check existence of modules dir. if it doesn't, modules cannot be 
   # called.
   if [[ ! -d $G_MEDIA_DIR/iu_modules ]]; then
      echo "$0: Error, cannot find modules directory"
      exit 1
   fi

   return 0
}


#
# StartInstall()
#
# Description:
#    To kick off the install/uninstall procedure
#
# Parameters:
#    (none)
#

StartInstall() {

   #
   # Initialize all modules
   #
   SendEventToModule Init

   if PrintPostInitMessage && ! YesOrNo "\n\nContinue?" "Y"; then
      CleanupAndExit 1
   fi

   #
   # Start the install/unistall process
   #
   if [[ $G_MODE = "install" ]]; then
      SendEventToModule Preserve Remove Install Restore
   else
      SendEventToModule Remove 
   fi

   PrintFinalMessage

   CleanupAndExit 0
}

#
# main 
#

#
# global variables definition
#

# get the full pathname of the command
typeset command=$(whence $0)

# location of the support_lib and modules directories
export G_MEDIA_DIR=${command%/*}/../lib

# location of the bundles (SWS,SDS,LDAP,...) 
# (cdrom root directory)
export G_PRODUCT_DIR=${command%/*}/..

# Perform sanity checks on directories
CheckDirectories

#
# include libraries and definitions
#

. ${G_MEDIA_DIR}/support_lib/gl_defs
. ${G_MEDIA_DIR}/support_lib/iu_lib
. ${G_MEDIA_DIR}/support_lib/master_lib

#
# local variables definition
#

PATH="/usr/sbin:/usr/bin"

typeset -r OPTSTR=":a:d:quvf"
typeset -r PROGRAM_OPTS="[-a admin_file] [-d media_dir] [-u] [-q]"
typeset -r TMP_ADMIN_FILE="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.admin_default"
typeset -r ERROR_FILE="${G_TMP_DIR}/.${G_PROGRAM_ID}.${G_PID}.errcode"
typeset USED_MINUS_D=false
typeset UT_DIR="/opt/SUNWut"

#
# main starts here
#

G_ADMIN_FILE=${G_PRODUCT_DIR}/admin_default
if [[ ! -f ${G_ADMIN_FILE} ]]; then
   G_ADMIN_FILE="${UT_DIR}/etc/admin_default"
fi

#
# parse the command line
#
while getopts $OPTSTR OPT; do
  case "$OPT" in
    a) G_ADMIN_FILE="$OPTARG";;
    d) G_PRODUCT_DIR="$OPTARG"; USED_MINUS_D=true;;
    q) G_QUICK_INSTALL="yes";;
    f) G_QUICK_INSTALL="yes";;
    u) G_MODE="uninstall";;
    v) G_DEBUG="yes";;
   \?) Usage;;
  esac
done
shift $(($OPTIND - 1))

if (( $# != 0 )); then
  Usage
fi

#
# sanity checks
#
CheckUidIsZero

CheckMediaDir

#
# change into "/" (root) directory to avoid the problem of 
# being invoked in one of the SunRay directories.
#
cd /

CheckAdminFile

cp "$G_ADMIN_FILE" "$TMP_ADMIN_FILE"
G_ADMIN_FILE="$TMP_ADMIN_FILE"

#
# if uninstalling the software, copy support_lib and modules directory
# in a temporary location. Otherwise, they will be removed with the rest of
# the software

if [[ $G_MODE = "uninstall" ]]; then
   rm -rf ${G_TMP_DIR}/support_lib > /dev/null 2>&1
   rm -rf ${G_TMP_DIR}/iu_modules > /dev/null 2>&1
   cp -pr ${G_MEDIA_DIR}/support_lib ${G_TMP_DIR}/support_lib > /dev/null 2>&1
   if [[ $? != 0 ]]; then
      Fatal "cannot copy support library in a temporary directory"
   fi
   cp -pr ${G_MEDIA_DIR}/iu_modules ${G_TMP_DIR}/iu_modules > /dev/null 2>&1
   if [[ $? != 0 ]]; then
      Fatal "cannot copy modules in a temporary directory"
   fi

   G_MEDIA_DIR=${G_TMP_DIR}
fi

print "# Script: ${G_PROGRAM_ID}\tVersion: 2.0_37.b,REV=2002.12.19.07.46\n" > $G_LOGFILE

#
# Start install/uninstall procedure. Using tee all output will be redirected
# to the stdout as well as saved in the log file.
#
StartInstall 2>&1 | tee -a $G_LOGFILE

#
# print the log pathname

echo "\nPlease check the log file $G_LOGFILE for errors.\n"

#
# As StartInstall is in pipe with tee, we never get the exit code of it
# (we get the one from tee instead, which is always 0). The cleanup procedure,
# then, create a temporary file (ERROR_FILE) which contains the error code
# of StartInstall. 

if [[ -f $ERROR_FILE ]]; then
   errcode=`cat $ERROR_FILE`
   rm -f $ERROR_FILE
   exit $errcode
fi

exit 0

