#!/bin/ksh -pu

#
# ident "@(#)utsunmc.ksh	1.27 05/03/15 SMI"
#
# Copyright 2001-2002,2005 Sun Microsystems, Inc.  All rights reserved.
#

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

PROGRAM_ID=$(basename $0)

###
#
# Description
#       This script will plug in and unplug the
#       Sun Ray module for Sun Management Center (SunMC)
#	That is, make the Sun Ray module usable from SunMC.
#       See manpage for details.
#
###

DoInstall() {
  typeset sunmc_version=""
  typeset NEED_AGENT=true
  typeset NEED_PROPERTIES=true

  # make sure SUNMC is installed on this machine
  #
  if ( ! ProductInstalled SUNWescom ) ; then
    print "\nNOTE:\tSun Management Center not installed."
    print "\tTo implement monitoring install Sun Management Center"
    print "\tthen run ${UT_BASEDIR}/SUNWut/sbin/${PROGRAM_ID}\n"
    TouchErrFile
    return
  fi

  # make sure SUNMC is at least 2.1.1
  #
  sunmc_version="$(pkgparam SUNWescom VERSION 2>&-)"
  if [[ "$sunmc_version" < "2.1" ]]; then
    print "\nNOTE:\tSun Management Center version ${sunmc_version} installed."
    print "\tMinimum version required is 2.1.1"
    TouchErrFile
    return
  fi

  # make sure SUNMC directories exist
  #
  if ( ProductInstalled SUNWessrv ) ; then
    if [[ ! -d $sunmc_instdir/classes/com/sun/symon/base/modules ]]; then
      Note "Required Sun Management Center server directory missing: " \
	"$sunmc_instdir/classes/com/sun/symon/base/modules"
      NEED_PROPERTIES=false
    fi
  else
    NEED_PROPERTIES=false
  fi

  # See if Sun Ray subagent base is installed on this machine
  #
  if ( ! ProductInstalled SUNWutesa SUNWuto SUNWesagt ) ; then
    NEED_AGENT=false
  fi

  print "\nAdding module for Sun Ray server to Sun Management Center.\n"

  # prepare for installations
  #
  if "$NEED_AGENT" ; then

    # begin agent installation
    #
    $UTSUNMCAGENT
    if [[ $? != 0 ]] ; then
      print "\nutsunmcagent failed.\n"
      TouchErrFile
    fi
  fi # end of module add (NEED_AGENT)

  # check if any server file exists
  # if yes, ask if want to override
  #
  if ( AnyServerFilesInstalled ) ; then
    print "\n\tPrevious MetaData/Properties files exist."
    if ( ! ReplyIsYes "\tDo you want to override them"); then
      NEED_PROPERTIES=false
    fi
  fi
 
  if "$NEED_PROPERTIES" ; then
    # Add the metadata files if needed
    # Delete any previous link that may exist (i.e. re-install)
    #
    if [[ -d $sunmc_instdir/mdr/modules/cfg ]]; then
      print "\nAdding Sun Ray server MetaData files.\n"
      rm -f $sunmc_instdir/mdr/modules/cfg/sunray-d.x
      ln -s $ut_instdir/sunray-d.x $sunmc_instdir/mdr/modules/cfg/sunray-d.x

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

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

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

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

      rm -f $sunmc_instdir/mdr/modules/cfg/sunray-ruletext-d.x
      ln -s $ut_instdir/sunray-ruletext-d.x $sunmc_instdir/mdr/modules/cfg/sunray-ruletext-d.x
    fi # end adding metadata files

    # Add the properties files including all the L10N files
    # Delete any previous link that may exist (i.e. re-install)
    #
    print "\nAdding Sun Ray server properties files.\n"

    rm -f $sunmc_propdir/sunray.properties
    ln -s $ut_instdir/sunray.properties $sunmc_propdir/sunray.properties

    for locale in $UT_LOCALES ; do
      InstallLocale $locale
    done

  fi # end add server files (NEED_PROPERTIES)

  return 0
}

DoUninstall() {

  print "\nRemoving module for Sun Ray server from Sun Management Center\n"

  if ( ProductInstalled SUNWutesa ); then
    # check if any of agent file exists
    # if so, invoke utsunmcagent
    #
    if ( AnyAgentFilesInstalled ) ; then

      $UTSUNMCAGENT -u

    fi
  fi

  if ( ! AnyServerFilesInstalled ) ; then
    print "\tMetaData/Properties files do not exist. Nothing was removed.\n"
    return 0
  fi

  # remove metadata files as well
  #
  print "\nRemoving Sun Ray server module MetaData files.\n"
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-d.x
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-d.def
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-m.x
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-models-d.x
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-ruleinit-d.x
  rm -f $sunmc_instdir/mdr/modules/cfg/sunray-ruletext-d.x

  # remove properties files as well
  #
  print "\nRemoving Sun Ray server module properties files.\n"
  rm -f $sunmc_propdir/sunray.properties
  for locale in $UT_LOCALES ; do
    RemoveLocale $locale
  done

  return 0
}

DoLocaleInstall() {
  # make sure SUNMC directories exist
  #
  if ( ! ProductInstalled SUNWessrv ) ; then
    return
  fi
  if [[ ! -d $sunmc_propdir ]]; then
    return
  fi

  # See if the base properties file is linked. If not, do not link this one
  #
  if [[ ! -L $sunmc_propdir/sunray.properties ]]; then
    return
  fi

  if [[ "${LOC}" = "zh" ]] ; then
    locale="zh_CN"
  else
    locale="${LOC}"
  fi
  if [[ ! -f $ut_instdir/sunray_$locale.properties ]]; then
    print "\nSpecified locale \"${LOC}\" not installed\n"
    return
  fi

  InstallLocale ${LOC}

  return 0
}

DoLocaleUninstall() {

  RemoveLocale ${LOC}

  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
# undocumented option -l locale for use by SUNW?utes preremove and postinstall
#
typeset -r OPTSTR=":fl:uv"
typeset -r PROGRAM_OPTS="[-u]"

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

while getopts $OPTSTR OPT; do
  case "$OPT" in
    f) FORCE=true;;
    l) LOC="$OPTARG"; LOCALE_ONLY=true;;
    u) MODE="uninstall";;
    v) VFLAG="-v";;
   \?) Usage;;
    :) print "\nArgument missing" ; Usage;; # to handle -l w/o argument
  esac
done
shift $(($OPTIND - 1))

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

CheckUidIsZero

#
# establish the path for utsunmcagent
UTSUNMCAGENT="${UT_BASEDIR}/SUNWut/lib/utsunmcagent"

#
# UT_LOCALES lists the possible locales that could be present
##	pl locale not supported at this time
#
typeset UT_LOCALES="de es fr it ja ko zh zh_TW"

typeset OSMINOR=""

# This version of SRSS is supported on 5.8 and 5.10.  However,
# the SunMC server may be a different release, as early as 5.6 and
# part of the SunRay module must be installed on the SunMC server.
# Therefor it is necessary to allow 5.6 through 5.10. Proceed without
# comment if this OS is that range.  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 5.6 or if the major release number is something
# other than 5 then report an error and terminate the install.
#
MINSUPP=6		# 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}"

# If only a single locale file is involved do not generate a log
# (used by L10N packages preremove and postinstall)
if "$LOCALE_ONLY" ; then
  VALID_LOCALE=false
  for loc in $UT_LOCALES ; do
    if [[ "$loc" = "$LOC" ]] ; then
      VALID_LOCALE=true
    fi
  done
  if ! $VALID_LOCALE ; then
    print "\nLocale \"${LOC}\" invalid, must be one of: ${UT_LOCALES}\n"
    CleanupAndExit 1
  fi
  case "$MODE" in
    install)   DoLocaleInstall;;
    uninstall) DoLocaleUninstall;;
  esac
  CleanupAndExit 0
fi

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
