#!/bin/ksh -pu

#
# ident "@(#)utsunmcagent.ksh	1.6 05/03/15 SMI"
#
# Copyright 2002,2005 Sun Microsystems, Inc.  All rights reserved.
#

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

PROGRAM_ID=$(basename $0)

###
#
# Description
#       This script will install and un-install
#       Sun Ray module for Sun Management Center (SunMC) agent.
#       Invoked privately by utsunmc
#       Invoked privately by SUNWutesa pre and postinstall
#
###

StopAgent() {
  RUNNING="$(ps -ef | grep agent | grep -v grep | grep -v -c utsunmc)"
  if [[ "$RUNNING" == "0" ]]; then
    return 1
  fi
  print "\nStopping the SunMC agent..."
  ${SUNMC_BASEDIR}/SUNWsymon/sbin/es-stop -a
  return 0
}

StartAgent() {
  print "\nStarting the SunMC agent..."
  ( ${SUNMC_BASEDIR}/SUNWsymon/sbin/es-start -a 1>/dev/null 2>&1 <&- ) &
  #
  # Test if the agent is running in a while loop (5 times of 2 seconds)
  #
  RUNNING="0"
  MAXRETRY=5
  while [[ "$RUNNING" == "0" && $MAXRETRY -gt 0 ]]; do
    sleep 2
    RUNNING="$(ps -ef | grep agent | grep -v -c grep)"
    let "MAXRETRY = MAXRETRY - 1"
  done
  if [[ "$RUNNING" == "0" ]]; then
    print "\nNOTICE:\t\tSunMC agent failed to start."
    print "\t\tTo start it manually run the command"
    print "\t\t${SUNMC_BASEDIR}/SUNWsymon/sbin/es-start -a\n"
    return 1
  fi
  print " Agent started.\n"
  return 0
}

#
# For SunMC 2.1.1 and 3.0 the SunRay TCL package library must exist in an
# OS release specific directory.
# for the SunMC release beyond 3.0 (variously known as 3.1, 4.0 and 3.5)
# packages which are not OS release specific go in base/lib instead
# of OS release specific directories.
# Establish links in base/lib plus every OS release directory to survive
# an OS upgrade to a new release or an upgrade of SunMC.
#
InstallPackageLib() {
  rm -f $sunmc_instdir/base/lib/pkgutesa.so
  ln -s $ut_instdir/pkgutesa.so.1.0 $sunmc_instdir/base/lib/pkgutesa.so

  OS_LIB_DIR_LIST=$(find $sunmc_instdir/base/lib -name "sparc-sun-solaris*")

  for dir in ${OS_LIB_DIR_LIST} ; do
    rm -f ${dir}/pkgutesa.so
    ln -s $ut_instdir/pkgutesa.so.1.0 ${dir}/pkgutesa.so
  done
}

#
# For SunMC 2.1.1 and 3.0 the SunRay TCL package library must exist in an
# OS release specific directory.
# for the SunMC release beyond 3.0 (variously known as 3.1, 4.0 and 3.5)
# packages which are not OS release specific go in base/lib instead
# of OS release specific directories.
# Remove links in base/lib plus every OS release directory to survive
# an OS upgrade to a new release or an upgrade of SunMC.
#
RemovePackageLib() {

  if [[ ! -d $sunmc_instdir/base/lib ]] ; then
    # no such directory, no package libraries can be present.
    return
  fi

  rm -f $sunmc_instdir/base/lib/pkgutesa.so

  OS_LIB_DIR_LIST=$(find $sunmc_instdir/base/lib -name "sparc-sun-solaris*")

  for dir in ${OS_LIB_DIR_LIST} ; do
    rm -f ${dir}/pkgutesa.so
  done
}

#
# For SunMC 2.1.1 and 3.0 the SunRay TCL package library must exist in an
# OS release specific directory.
# for the SunMC release beyond 3.0 (variously known as 3.1, 4.0 and 3.5)
# packages which are not OS release specific go in base/lib instead
# of OS release specific directories.
# Check for links in base/lib plus every OS release directory to survive
# an OS upgrade to a new release or an upgrade of SunMC.
#
CheckPackageLib() {

  if [[ ! -d $sunmc_instdir/base/lib ]] ; then
    # no such directory, no package libraries can be present.
    return 1
  fi

  if [[ -L $sunmc_instdir/base/lib/pkgutesa.so ]] ; then
    return 0
  fi

  OS_LIB_DIR_LIST=$(find $sunmc_instdir/base/lib -name "sparc-sun-solaris*")

  for dir in ${OS_LIB_DIR_LIST} ; do
    if [[ -L ${dir}/pkgutesa.so ]] ; then
      return 0
    fi
  done

  return 1
}

DoInstall() {
  typeset sunmc_version=""
  typeset NEED_START=false
  typeset NEED_AGENT=true

  # make sure SUNMC is installed on this machine
  # NOTE: no message since utsunmc would generate the message when needed
  #
  if ( ! ProductInstalled SUNWescom ) ; then
    return
  fi

  # make sure SUNMC is at least 2.1.1
  # NOTE: no message since utsunmc would generate the message when needed
  #
  sunmc_version="$(pkgparam SUNWescom VERSION 2>&-)"
  if [[ "$sunmc_version" < "2.1" ]]; then
    return
  fi

  # make sure SUNMC directories exist
  #
  if ( ProductInstalled SUNWesagt ) ; then
    if [[ ! -d $sunmc_instdir/modules/cfg ]]; then
      Note "Required Sun Management Center agent directory missing: " \
	"$sunmc_instdir/modules/cfg"
      NEED_AGENT=false
    fi
    if [[ ! -d $sunmc_instdir/modules/sbin ]]; then
      Note "Required Sun Management Center agent directory missing: " \
	"$sunmc_instdir/modules/sbin"
      NEED_AGENT=false
    fi
    if [[ ! -d $sunmc_instdir/base/lib ]]; then
      Note "Required Sun Management Center agent directory missing: " \
	"$sunmc_instdir/base/lib"
      NEED_AGENT=false
    fi
    if [[ ! -d $sunmc_instdir/util/cfg ]]; then
      Note "Required Sun Management Center agent directory missing: " \
	"$sunmc_instdir/util/cfg"
      NEED_AGENT=false
    fi
  else
    NEED_AGENT=false
  fi

  # make sure Sun Ray subagent base is installed on this machine
  #
  if ( ! ProductInstalled SUNWutesa ) ; then
    Fatal "Sun Ray server package SUNWutesa not installed"
  fi

  # prepare for installations
  #
  if ( ! ProductInstalled SUNWuto ) ; then
    Fatal "Sun Ray server agent not required"
    NEED_AGENT=false
  fi

  # check if any of agent file exists
  # if yes, ask if want to override
  #
  if ( AnyAgentFilesInstalled ) ; then
    print "\n\tPrevious agent files exist."
    if ( ! ReplyIsYes "\tDo you want to override them"); then
      NEED_AGENT=false
    fi
  fi
 
  if "$NEED_AGENT" ; then
    # Stop the running SunMC agent and remember if we need to restart it
    #
    if StopAgent ; then
      NEED_START=true
    fi

    # begin agent installation
    # Delete any previous link that may exist (i.e. re-install)
    #
    print "\nAdding Sun Ray server module agent files.\n"

    rm -f $sunmc_instdir/modules/cfg/sunray-d.x
    ln -s $ut_instdir/sunray-d.x $sunmc_instdir/modules/cfg/sunray-d.x

    rm -f $sunmc_instdir/modules/cfg/sunray-d.def
    ln -s $ut_instdir/sunray-d.def $sunmc_instdir/modules/cfg/sunray-d.def

    rm -f $sunmc_instdir/modules/cfg/sunray-d.rul
    ln -s $ut_instdir/sunray-d.rul $sunmc_instdir/modules/cfg/sunray-d.rul

    rm -f $sunmc_instdir/modules/cfg/sunray-m.x
    ln -s $ut_instdir/sunray-m.x $sunmc_instdir/modules/cfg/sunray-m.x

    rm -f $sunmc_instdir/modules/cfg/sunray-models-d.x
    ln -s $ut_instdir/sunray-models-d.x $sunmc_instdir/modules/cfg/sunray-models-d.x

    rm -f $sunmc_instdir/modules/cfg/sunray-ruleinit-d.x
    ln -s $ut_instdir/sunray-ruleinit-d.x $sunmc_instdir/modules/cfg/sunray-ruleinit-d.x

    rm -f $sunmc_instdir/modules/cfg/sunray-ruletext-d.x
    ln -s $ut_instdir/sunray-ruletext-d.x $sunmc_instdir/modules/cfg/sunray-ruletext-d.x

    rm -f $sunmc_instdir/modules/sbin/utsdhcpsum
    ln -s ${UT_BASEDIR}/SUNWut/lib/utsdhcpsum $sunmc_instdir/modules/sbin/utsdhcpsum

    rm -f $sunmc_instdir/modules/sbin/utspatches
    ln -s ${UT_BASEDIR}/SUNWut/lib/utspatches $sunmc_instdir/modules/sbin/utspatches

    # install the package library
    InstallPackageLib

    rm -f $sunmc_instdir/util/cfg/sunray.mib
    ln -s $ut_instdir/sunray.mib $sunmc_instdir/util/cfg/sunray.mib

    if [[ -f $sunmc_vardir/cfg/base-modules-d.dat ]] ; then
      /usr/bin/grep sunray $sunmc_vardir/cfg/base-modules-d.dat > /dev/null 2>& 1
    else
      cp $sunmc_instdir/base/cfg/base-modules-d.dat $sunmc_vardir/cfg/base-modules-d.dat
      /usr/bin/grep sunray $sunmc_vardir/cfg/base-modules-d.dat > /dev/null 2>& 1
    fi
    if [ $? -eq 1 ] ; then
      print "\nRegistering Sun Ray module as loaded\n"
      echo sunray = \
	\".iso.org.dod.internet.private.enterprises.sun.prod.sunRay.sunrayMib \
sun sunray {module = \\\"sunray\\\"\; enterprise = \\\"sun\\\"\; \
i18nModuleType = \\\"base.modules.sunray:moduleType\\\"\; \
moduleType = \\\"operatingSystem\\\"\; oid = \\\"1.3.6.1.4.1.42.2.75.1\\\"\; \
i18nModuleName = \\\"base.modules.sunray:moduleName\\\"\; location = \
\\\".iso.org.dod.internet.private.enterprises.sun.prod.sunRay.sunrayMib\\\"\; \
moduleName = \\\"Sun Ray\\\"\; version = \\\"1.0\\\"\; \
i18nModuleDesc = \\\"base.modules.sunray:moduleDesc\\\"\; \
console = \\\"sunray\\\"\; instance = \\\"\\\"\; }\" \
>> $sunmc_vardir/cfg/base-modules-d.dat

    fi # end of grep match
  fi # end of module add (NEED_AGENT)

  # if we need to, restart the SunMC agent
  #
  if "$NEED_START"; then
    StartAgent
  fi

  return 0
}

DoUninstall() {
  typeset NEED_START=false

  # check if any of agent file exists
  #
  if ! ( AnyAgentFilesInstalled || CheckPackageLib ) ; then
    print "\tAgent files do not exist. Nothing was removed.\n"
    return 0
  fi

  # Stop the running SunMC agent and remember if we need to restart it
  #
  if StopAgent ; then
    NEED_START=true
  fi

  # begin actual removal
  #
  print "\nRemoving Sun Ray server module agent files.\n"
  rm -f $sunmc_instdir/modules/cfg/sunray-d.x
  rm -f $sunmc_instdir/modules/cfg/sunray-d.def
  rm -f $sunmc_instdir/modules/cfg/sunray-d.rul
  rm -f $sunmc_instdir/modules/cfg/sunray-m.x
  rm -f $sunmc_instdir/modules/cfg/sunray-models-d.x
  rm -f $sunmc_instdir/modules/cfg/sunray-ruleinit-d.x
  rm -f $sunmc_instdir/modules/cfg/sunray-ruletext-d.x
  rm -f $sunmc_instdir/modules/sbin/utsdhcpsum
  rm -f $sunmc_instdir/modules/sbin/utspatches
  rm -f $sunmc_instdir/util/cfg/sunray.mib

  # remove package library
  RemovePackageLib

  # if we need to, restart the SunMC agent
  #
  if "$NEED_START"; then
    StartAgent
  fi

  return 0
}

# main 

typeset -r UT_BASE="$(pkginfo -r SUNWutesc 2>&-)"
typeset -r UT_BASEDIR="${UT_BASE:-/opt}"

#
# include shared library
. "${UT_BASEDIR}/SUNWut/lib/support_lib/utsunmc_lib"

trap "CleanupAndExit 1" HUP INT QUIT TERM

#
# undocumented option -f for use by install process
# undocumented option -v for use during testing
#
typeset -r OPTSTR=":fuv"
typeset -r PROGRAM_OPTS="[-u]"

typeset MODE="install"
typeset FORCE=false
typeset VFLAG=""

while getopts $OPTSTR OPT; do
  case "$OPT" in
    f) FORCE=true;;
    u) MODE="uninstall";;
    v) VFLAG="-v";;
   \?) Usage;;
  esac
done
shift $(($OPTIND - 1))

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

CheckUidIsZero

typeset OSMINOR=""

# This version of SRSS is supported on 5.8 through 5.10.
# If the OS is 5.N where N is greater than $MAXSUPP then this
# is probably someone trying an experiment, so just emit a
# warning and allow the install to proceed.  If the OS is
# earlier than $MINSUPP or if the major release number is
# something other than 5 then report an error and terminate
# the install.
#
MINSUPP=8		# lowest supported minor release
MAXSUPP=10		# highest supported minor release

ValidOS

#
# SunMC uses OS release specific library directories.
# Instead of 5.x, it uses 2.x and it only uses major and minor.
#
typeset OS_LIB_DIR="sparc-sun-solaris2.${OSMINOR}"

print "# Script: ${PROGRAM_ID}\tVersion: 3.1_32,REV=2005.08.24.08.55\n" >> $LOGFILE

# return code from DoInstall() and DoUninstall() will not be visible
# due to outputs piped (| tee') for logging purpose
case "$MODE" in
  install)   DoInstall   2>&1 | tee -a $LOGFILE;;
  uninstall) DoUninstall 2>&1 | tee -a $LOGFILE;;
esac

# pass error conditions from DoInstall() and DoUninstall()
# by using $ERRFILE file
if [[ -f $ERRFILE ]]; then
   echo "Please check the log file, $LOGFILE, for errors.\n"
   CleanupAndExit 1
fi
CleanupAndExit 0
