#
# ident "@(#)utsunmc_lib.ksh	1.7 06/05/07 SMI"
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# This script library is shared (sourced) by:
#   utsunmc
#   utsunmcagent
#   utsunmcinstall

TouchErrFile() {
  touch $ERRFILE
}

CleanupAndExit() {
  rm -f $ERRFILE
  exit $1
}

Usage() {
  print -u2 "Usage: $PROGRAM_ID $PROGRAM_OPTS"
  CleanupAndExit 1
}

Fatal() {
  print -u2 "$PROGRAM_ID: fatal, $*"
  CleanupAndExit 1
}

Error() {
  print -u2 "$PROGRAM_ID: error, $*"
  CleanupAndExit 1
}

Note() {
  print -u2 "$PROGRAM_ID: note, $*"
  return 0
}

CheckUidIsZero() {
  case "$(id)" in
    'uid=0('*) return 0;;  # uid is zero
    *)         Fatal "must be run as uid 0 (root)";;
  esac
}

ProductInstalled() {
  typeset pkg=""
 
  for pkg in $*; do
    pkginfo -q $pkg || return 1
  done

  return 0
}
 
AnyAgentFilesInstalled() {
  typeset sunmc_instdir="${SUNMC_BASEDIR}/SUNWsymon"
  if [[ -L $sunmc_instdir/modules/cfg/sunray-d.x ||
        -L $sunmc_instdir/modules/cfg/sunray-d.def ||
        -L $sunmc_instdir/modules/cfg/sunray-d.rul ||
        -L $sunmc_instdir/modules/cfg/sunray-m.x ||
        -L $sunmc_instdir/modules/cfg/sunray-models-d.x ||
        -L $sunmc_instdir/modules/cfg/sunray-ruleinit-d.x ||
        -L $sunmc_instdir/modules/cfg/sunray-ruletext-d.x ||
        -L $sunmc_instdir/modules/sbin/utsdhcpsum ||
        -L $sunmc_instdir/modules/sbin/utspatches ||
        -L $sunmc_instdir/base/lib/pkgutesa.so ||
        -L $sunmc_instdir/base/lib/${OS_LIB_DIR}/pkgutesa.so ||
        -L $sunmc_instdir/util/cfg/sunray.mib ]]; then
    return 0
  else
    return 1
  fi
}

AnyServerFilesInstalled() {
  typeset sunmc_instdir="${SUNMC_BASEDIR}/SUNWsymon"
  typeset sunmc_propdir="${sunmc_instdir}/classes/com/sun/symon/base/modules"

  # additional rules:
  #  if [[ -L $sunmc_instdir/mdr/modules/cfg/sunray-d.rul ]]; then
  # possible language:
  #  if [[ -L $sunmc_propdir/sunray_pl.properties ]]; then
  ##

  if [[ -L $sunmc_instdir/mdr/modules/cfg/sunray-d.x ||
        -L $sunmc_instdir/mdr/modules/cfg/sunray-d.def ||
        -L $sunmc_instdir/mdr/modules/cfg/sunray-m.x ||
        -L $sunmc_instdir/mdr/modules/cfg/sunray-models-d.x ||
        -L $sunmc_instdir/mdr/modules/cfg/sunray-ruleinit-d.x ||
        -L $sunmc_instdir/mdr/modules/cfg/sunray-ruletext-d.x ||
        -L $sunmc_propdir/sunray.properties ]]; then
    return 0
  fi
  for locale in $UT_LOCALES; do
    if [[ "$locale" = "zh" ]]; then
      locale="zh_CN"
    fi
    if [[ -L $sunmc_propdir/sunray_$locale.properties ]]; then
      return 0
    fi
  done
  return 1
}

RemoveLocale() {
  if [[ "$1" = "zh" ]]; then
    locale="zh_CN"
  else
    locale="$1"
  fi
  rm -f $sunmc_propdir/sunray_$locale.properties
}

InstallLocale() {
  if [[ "$1" = "zh" ]]; then
    locale="zh_CN"
  else
    locale="$1"
  fi

  # Delete any previous link that may exist (i.e. re-install)
  #
  rm -f $sunmc_propdir/sunray_$locale.properties

  if [[ -f $ut_instdir/sunray_$locale.properties ]]; then
    ln -s $ut_instdir/sunray_$locale.properties \
		$sunmc_propdir/sunray_$locale.properties
  fi
}

ReplyIsYes() {
  typeset reply=""

  if ! "$FORCE"; then
    while true; do
      print -n "$1 ([y]/n)? "
      read reply
      case "$reply" in
        "" | [yY]*) return 0;;
        [nN]*)      return 1;;
      esac
    done
  else
    return 0
  fi
}

# ValidOS - checks the Solaris release to insure it is within range
#
# The supported versions of OS differ between the SRSS server and
# the SunMC server. The range is set before invoking this function:
#	MINSUPP		# lowest supported minor release
#	MAXSUPP		# highest supported minor release
#
# If the OS is earlier than 5.${MINSUPP} or if the major release
# number is something other than 5 then report an error and terminate
# the install.

ValidOS() {
  SUNOSREL="$(uname -r)"
  MOSR="Malformed SunOS release '$SUNOSREL'"
  OSNS="SunOS $SUNOSREL is not supported"

  case "$SUNOSREL" in
  5.*)
    # isolate the minor release number and require it to be
    # entirely numeric
    #
    OSMINOR=$(echo "$SUNOSREL" | awk -F. '{print $2}')
    if [[ -z "$OSMINOR" ]]; then
      Error "$MOSR (no minor release)."
    elif [[ $(expr "$OSMINOR" : '[0123456789]*') \
      -ne $(expr "$OSMINOR" : '.*') ]]; then
      Error "$MOSR (non-numeric minor release)."
    elif [[ "$OSMINOR" -lt $MINSUPP ]]; then
      Error "$OSNS (must be at least 5.$MINSUPP, at most 5.$MAXSUPP)."
    elif [[ "$OSMINOR" -gt $MAXSUPP ]]; then
      Note "$OSNS (should be at most 5.$MAXSUPP).  Let's try it anyway."
    fi
    ;;
    
  *)
    Error "$OSNS (must be at least 5.$MINSUPP, at most 5.$MAXSUPP)."
    ;;
  esac

  return
}

#
# Uses: UT_BASEDIR which must be set by the caller
#

SUNMC_BASE="$(pkginfo -r SUNWescom 2>&-)"
SUNMC_BASEDIR="${SUNMC_BASE:-/opt}"

typeset -r ut_instdir="${UT_BASEDIR}/SUNWut/lib/snmp/sunmc"
typeset -r sunmc_instdir="${SUNMC_BASEDIR}/SUNWsymon"
typeset -r sunmc_vardir="/var/opt/SUNWsymon"
typeset -r sunmc_propdir="${sunmc_instdir}/classes/com/sun/symon/base/modules"

typeset -r TIME_STAMP="$(date '+%Y_%m_%d_%H:%M:%S')"
typeset -r LOGFILE="/var/adm/log/${PROGRAM_ID}.${TIME_STAMP}.log"
typeset -r ERRFILE="/var/adm/log/.${PROGRAM_ID}.$$.errcode"
