#!/bin/ksh -p
#
# ident "@(#)iu_lib.ksh	1.58	12/11/21 Oracle"
#
# Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
#

#
# function YesOrNoDialog
#
# Description:
#    prompt user for yes or no answer using utdialog
#    usage:  YesOrNo <SRSS dialog key> [ <default value> ]
#    Returns 0 if the user specified "Y" or "Yes" (case insensitive),
#	nonzero otherwise.
#
# Parameters:
#    $1 - SRSS dialog keyname
#    $2 - default value "Y" or "N"
#
# Globals used:
#    G_QUICK_INSTALL

function YesOrNoDialog {
    typeset OPTS=$G_UTDIALOG_OPTS

    while [ -z "${1##-*}" ]; do
	OPTS="$OPTS $1"
	shift
    done

    if [ "$G_QUICK_INSTALL" = "yes" ]; then
	OPTS="$OPTS -d"
    fi

    [ $(${G_MEDIA_DIR}/utdialog $OPTS -H YesOrNo${2:+:$2} utinstall.$1) = "Y" ]
    # The syntax following "YesOrNo" above looks rather confusing:
    # if $2 is empty it turns to nothing so YesOrNo handler will require a response
    # otherwise it turns to ":$2", thus providing a default for the YesOrNo handler
}

#
# function YesOrNo
#
# This is copied verbatim in SRCS
# src/server/admin/install/utfwuninstall.ksh and SRSS
# src/release/image/install-support-lib.ksh and probably other
# places. It should be kept in sync with that source unless/until we
# find a way to share it.

# Ultimately we should find a better way to share this. Perhaps it
# should be driven by common source in install-support-lib which can
# then at least be copied around into different products (copied in
# order to avoid unattractive package dependencies)

# Note this function does *not* use utdialog and is not AIC compliant,
# so should only be used in situations where utdialog is not
# necessarily available. Should it be enhanced to fall
# back on utdialog if present (in future)? Let's not enhance it until
# it's shared, at least, so we don't diverge.

# Description:
#    prompt user for yes or no answer
#    usage:  YesOrNo <prompt> <default value>
#    Returns 0 if the user specified "Y" or "Yes" (case insensitive),
#	nonzero otherwise.
#
# Parameters:
#    $1 - PROMPT prefix
#    $2 - default value "Y" or "N"
#
# Globals used:
#    G_QUICK_INSTALL (optional)
#    G_LOGFILE

function YesOrNo {
   typeset PROMPT=""
   typeset DEFAULT=""
   typeset RESPONSE=""

   if [[ $# != 2 ]]; then
      return 1
   fi

   if [ "$2" == "Y" ]; then
      PROMPT="$1 ([Y]/N): "
      DEFAULT="Y"
   else
      PROMPT="$1 (Y/[N]): "
      DEFAULT="N"
   fi

   while true; do
      print -n "$PROMPT"

      case "$G_QUICK_INSTALL" in
        yes) RESPONSE="";;    # as if user only pressed Enter
        *)   read RESPONSE;;  # actually get user input
      esac
      print "$RESPONSE" >>$G_LOGFILE

      if [[ -z "$RESPONSE" ]]; then
        RESPONSE="$DEFAULT"  # empty response results in use of default
      fi

      case "$RESPONSE" in
	[Yy] | [Yy][Ee][Ss]) return 0;;
	[Nn] | [Nn][Oo])     return 1;;
      esac
   done
}

#
# function isTrustedSolaris
#
## Check if this is a trusted Solaris host.
##
## Returns 0     if this is a trusted Solaris host
##         non-0 otherwise
#
function isTrustedSolaris {
 typeset isTrustedSolaris=1
 if [[ "$G_OS" == "SunOS" && -x /bin/plabel ]] ; then
  /bin/plabel > /dev/null 2>&1
  if [ $? -eq 0 ] ; then
   isTrustedSolaris=0
  fi
 fi
 return $isTrustedSolaris
}

#
# function StartActivityMeter
#
# Description:
#    Start a subprocesses which prints out a "." (dot) each 2 seconds. This
#    is to show that the installer is not hanging.
#
# Parameters:
#    (none)
#
# Globals used:
#    G_ACTIVITYM_PID

function StartActivityMeter {

   if [[ -z "$G_ACTIVITYM_PID" ]]; then
      (
         while true; do
            print -n '.'
            sleep 2
         done
      ) &
      G_ACTIVITYM_PID=$!
   fi
}

#
# function StopActivityMeter
#
# Description:
#    Stop a subprocesses which prints out a "." (dot)
#
# Parameters:
#    (none)
#
# Globals used:
#    G_ACTIVITYM_PID

function StopActivityMeter {

   if [[ -n "$G_ACTIVITYM_PID" ]]; then
      kill -9 $G_ACTIVITYM_PID
      echo
      G_ACTIVITYM_PID=""
   fi
}

#
# function InstallMsg
#
# Description:
#    to print a message before installing a product
#
# Parameters:
#    $1 : product name
#    $2 : product version
#
# Globals used:
#    (none)

function InstallMsg {
  print "\nInstalling $1 version $2 ..."
  return 0
}

#
# function RemoveMsg
#
# Description:
#    to print a message before removing a product
#
# Parameters:
#    $1 : product name
#    $2 : product version
#
# Globals used:
#    (none)

function RemoveMsg {
  print "\nRemoving $1 version $2 ..."
  return 0
}

#
# function InstallPatches
#
# Description:
#    to install patches
#
# Parameters:
#    $1 : patches directory
#
# Globals used:
#    G_PROGRAM_ID
#    G_PID
#    G_TMP_DIR

function InstallPatches {
  typeset dir="$1"
  typeset all_patches=""
  typeset patch=""
  typeset patchid=""
  typeset exist=""
  typeset pkgs=""
  typeset file="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.patchadd"

  # If G_IPSABLE, package updates will be installed by Metapkg in
  # M01SunRay_first_IPS
  if ! "$G_IPSABLE"; then
    if [[ -r "${dir}/.order" ]]; then
      all_patches=$(awk '/^[^#]/ { print $1 }' ${dir}/.order 2>&- |
	grep "^[0-9][0-9][0-9][0-9][0-9][0-9]")
    elif [[ -d "${dir}" ]]; then
      all_patches=$(\ls -1 ${dir} 2>&- | awk -F- '{ print $1 }' |
	grep "^[0-9][0-9][0-9][0-9][0-9][0-9]" | sort -u)
    fi

    [[ -n "$all_patches" ]] || return 0

    case "$G_OS" in
      SunOS)
	typeset showrev_p="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.showrev_p"
	/bin/rm -f ${showrev_p}
	print "\nGenerating patch list..."
	showrev -p 2>&- |
	sed -n 's/Patch: \([0-9][0-9]*-[0-9][0-9]\).*/\1/p' |
	sort -rn >${showrev_p}

	for patchid in $all_patches; do
	  print -n "... checking patch ${patchid} ... "
	  exist=$(grep "${patchid}-[0-9][0-9]" ${showrev_p} | head -1)
	  patch=$(\ls -1 "$dir" 2>&- | grep "^${patchid}" | tail -1)
	  pkgs=$(cd "${dir}/${patch}" 2>&-; \ls -1 */pkginfo 2>&- | 
            sed -e 's:/pkginfo::g')

	  [[ -n "$pkgs" ]] || continue
	  pkginfo -q $pkgs || continue
	  if [[ "$patch" > "$exist" ]]; then
            print "installing patch $patch ..."

            StartActivityMeter
            patchadd -M "$dir" "$patch" > "$file" || Note "\npatch $patch did not" \
              "install correctly, log follows, continuing ..."
            StopActivityMeter
            cat "$file"
	  else
            print "patch ${exist} already installed."
	  fi
	done
	/bin/rm ${showrev_p}
	;;

      Linux)
	cd $dir
	for patchid in $all_patches; do
	  patch=$(ls -d $patchid-*)
	  print "installing patch $patch ..."
	  (
	    cd $patchid-*
            rpm --nodeps -F --noscripts *.rpm
	  )
	done
	;;
    esac
  fi

  return 0
}

#
# function ProductInstalled
#
# Description:
#    to check if a product has been installed succesfully
#
# Parameters:
#    $* : list of packages names
#
# Globals used:
#    G_MEDIA_DIR

function ProductInstalled {
  typeset PKG=""
  typeset STATUS=0

  for PKG in $*; do
    if ! $G_MEDIA_DIR/utprodinfo -t installed $PKG; then
      STATUS=1
      break
    fi
  done

  return $STATUS
}

#
# function AnyPackageInstalled
#
# Description:
#    to check if one of the listed packages is installed correctly
#
# Parameters:
#    $* : list of packages names
#
# Globals used:
#    (none)

function AnyPackageInstalled {
  typeset pkg=""

  for pkg in $*; do
    if $G_MEDIA_DIR/utprodinfo -t installed $pkg; then
      return 0
    fi
  done

  return 1
}

#
# function ProductPartiallyInstalled
#
# Description:
#    to check if a package has not been completely installed
#
# Parameters:
#    $* : list of packages names
#
# Globals used:
#    G_MEDIA_DIR

function ProductPartiallyInstalled {
  typeset PKG=""

  for PKG in $*; do
    if $G_MEDIA_DIR/utprodinfo -t partial $PKG; then
      return 0
    fi
  done

  return 1
}

#
# function InstallProductMetapkg
#
# Description:
#    to install a specific product metapackage
#
# Parameters:
#    $1            : admin file (used for pkgadd)
#    $2	           : device where the packages are located
#    $* (after $2) : list of metapackage names
#

function InstallProductMetapkg {
  InstallProductCommon $@
}

#
# function InstallProduct
#
# Description:
#    to install a specific product
#
# Parameters:
#    $1            : admin file (used for pkgadd)
#    $2	           : device where the packages are located
#    $* (after $2) : list of packages names
#
# Globals used:
#    G_IPSABLE
#    G_IPS_INSTALL_PKGS

function InstallProduct {
  typeset ADMINFILE="$1"
  typeset DIR="$2"
  shift 2

  if $G_IPSABLE; then
    PKGS_NEEDED=$(GetPkgsNotInIPSList "$*" "$G_IPS_INSTALL_PKGS")
  else
    PKGS_NEEDED=$@
  fi
  if [[ -n "$PKGS_NEEDED" ]]; then
    InstallProductCommon $ADMINFILE $DIR $PKGS_NEEDED
  fi
}

#
# function InstallProductCommon
#
# Description:
#    to install a specific product
#
# Parameters:
#    $1            : admin file (used for pkgadd)
#    $2	           : device where the packages are located
#    $* (after $2) : list of packages names
#
# Globals used:
#    G_OS

function InstallProductCommon {
  typeset VOPT=""

  typeset ADMINFILE="$1"
  typeset DIR="$2"
  shift 2
  typeset PKGS="$*"
  typeset PKG

  typeset STATUS=0

  if [ G_DEBUG = "yes" ]; then
     VOPT="-v"
  fi

  if $G_IPSABLE; then
    print "\n+++ $PKGS"
    pkg install --no-index --no-refresh $PKGS
    return
  fi

  for PKG in $PKGS; do
    case "$PKG" in
      *.rpm) eval PKG="\$(basename \$(ls $DIR/$PKG))";;
    esac

    print "\n+++ $PKG"

    DOPT="--nodeps"
    case "$PKG" in
      SUNWuta-*.rpm) ;;
      SUNWuto-*.rpm) ;;
      SUNWutr-*.rpm) ;;
      SUNWutgsm-*.rpm) ;;
      gdm-*.rpm)     ;;
      SUNWutps-*.rpm)     ;;
      SUNWutref-*.rpm)     ;;
      SUNWutxrandr-*.rpm)     ;;
      SUNWutesa-*.rpm)     ;;

      *)             DOPT="";;
    esac

    case "$G_OS" in
      SunOS)
	pkgadd $VOPT -a $ADMINFILE -d $DIR $PKG
	;;
      Linux)
	rpm -i $VOPT $DOPT $DIR/$PKG
	;;
    esac

    if (( $? != 0 )); then
      STATUS=1
    fi
  done

  return $STATUS
}

#
# function RemoveProductUsingMetapkg
#
# Description:
#    to remove a product meta package (IPS only) and all packages it
#    depends upon
#
# Parameters:
#    $1           : meta package name

function RemoveProductUsingMetapkg {
  pkg uninstall $1 $(pkg contents -H -t depend -o fmri $1)
}

#
# function GetPkgsNotInIPSList
#
# Description:
#    List the subset of packages in the first list which are *not* in
#    the second list. Used to postpone uninstallation or suppress
#    re-installation of packages covered by the IPS meta-package
#    dependencies handled at the start of install or at the end of
#    uninstall.
#
# Parameters:
#    $1           : list of packages to check
#    $2           : list of meta-package dependencies to check against
#

function GetPkgsNotInIPSList {
  # WARNING: This code will only work for ksh99 (i.e. not on Solaris
  # 10) which is OK because this code is only executed on Solaris
  # 11. It relies on ksh99 associative arrays and variable
  # concatenation.

  # Too bad this array can't be populated once for all modules. Note
  # IU framework variable save/restore mechanism cannot support arrays
  # of any sort so we can't even do this once for *each* module.
  typeset -A DEPENDENCY_PKGS
  for pkg in $(echo $2); do
    DEPENDENCY_PKGS[${pkg%%@*}]=true
  done

  typeset NON_DEPENDENCY_PKGS=""
  for pkg in $(echo $1); do
    if ! ${DEPENDENCY_PKGS[$pkg]:-false}; then
      NON_DEPENDENCY_PKGS+="$pkg "
    fi
  done
  print $NON_DEPENDENCY_PKGS
}

#
# function RemoveProduct
#
# Description:
#    to remove a specific product
#
# Parameters:
#    $1            : admin file (used for pkgrm)
#    $* (after $1) : list of packages names
#
# Globals used:
#    G_DEBUG
#    G_OS
#    G_IPSABLE
#    G_IPS_UNINSTALL_PKGS

function RemoveProduct {
  typeset ADMINFILE="$1"
  shift
  set -A PKG $*
  set ${PKG[*]}

  typeset -i I=$(($# - 1))
  typeset ERRLIST=""
  typeset STATUS=0
  typeset VOPT=""
  typeset IPS_PKGLIST=""
  typeset IPS_PKGS_TO_CHECK=""

  if [ G_DEBUG = "yes" ]; then
     VOPT="-v"
  fi

  while (( $I >= 0 )); do
    if $G_MEDIA_DIR/utprodinfo -t installed ${PKG[$I]}; then
      if ! $G_IPSABLE || [[ -n "$(pkgparam ${PKG[$I]} PSTAMP)" ]]; then
	print "\n+++ ${PKG[$I]}"
	case "$G_OS" in
          SunOS)
	    pkgrm $VOPT -n -a $ADMINFILE ${PKG[$I]}
	    ;;

          Linux)
	    rpm --nodeps -e --allmatches $VOPT ${PKG[$I]}
	    ;;
	esac
      fi
    fi
    if $G_MEDIA_DIR/utprodinfo -t installed ${PKG[$I]}; then
      if $G_IPSABLE; then
	# construct a list of what needs cleaning up, do it all at once later
	IPS_PKGLIST="$IPS_PKGLIST ${PKG[$I]}"
      else
	STATUS=1
	ERRLIST="$ERRLIST ${PKG[$I]}"
      fi
    fi

    I=$(($I - 1))
  done

  if $G_IPSABLE && [[ -n "$(print $IPS_PKGLIST)" ]]; then
    if [ -n "$G_IPS_UNINSTALL_PKGS" ]; then
      IPS_PKGS_TO_CHECK=$G_IPS_UNINSTALL_PKGS
    else
      # Some modules do their own explicit upgrade-like processing
      # even on fresh install for reasons unknown, so use the install
      # list for this purpose
      IPS_PKGS_TO_CHECK=$G_IPS_INSTALL_PKGS
    fi
    PKGS_TO_DELETE=$(GetPkgsNotInIPSList "$IPS_PKGLIST" "$IPS_PKGS_TO_CHECK")

    if [[ -n "$PKGS_TO_DELETE" ]]; then
      print "\n+++ " $PKGS_TO_DELETE
      pkg uninstall $PKGS_TO_DELETE
      return
    fi
  fi

  if (( $STATUS != 0 )); then
    if [[ -n "$ERRLIST" ]]; then
      print "\nThe following packages were not successfully removed:"
      print " $ERRLIST\n"
    fi
  fi

  return $STATUS
}

#
# function RemovePatch
#
# Description:
#    to remove a specific patch
#
# Parameters:
#    $* : list of patches
#
# Globals used:
#    (none)

function RemovePatch {
  typeset patches="$*"
  typeset installed=""
  typeset patchrev=""
  typeset patch=""
  typeset status=0

  [[ -n "$patches" ]] || return 0

  # If G_IPSABLE, package updates will be removed by Common Installer
  if ! "$G_IPSABLE"; then
    typeset showrev_p="${G_TMP_DIR}/${G_PROGRAM_ID}.${G_PID}.showrev_p"
    /bin/rm -f ${showrev_p}
    showrev -p 2>&- | \
      sed -n 's/Patch: \([0-9][0-9]*-[0-9][0-9]\).*/\1/p' | sort -rn \
      > ${showrev_p}

    for patch in $patches; do
      installed=$(grep "${patch}" ${showrev_p})
      [[ -n "$installed" ]] || continue
      for patchrev in $installed; do
	patchrm "$patchrev" || status=1
      done
    done

    /bin/rm ${showrev_p}
  fi
  return $status
}

#
# function ProductMsg
#
# Description:
#    to print the product name/version in a standard format
#
# Parameters:
#    $1 : product name
#    $2 : product version
#
# Globals used:
#    (none)

function ProductMsg {
  typeset -L40 S

  case "$1" in
    -n) S="$2"; print -n "$S ";;
    -l) print "$2";;
  esac

  return $?
}

#
# function GetPackagePath
#
# Description:
#     Return the directory path for the packages based on the
#     system architecture and kernel.  Nothing will be printed if
#     either the G_INSTALL_ARCH or the G_INSTALL_OS is invalid.
#
# Parameters:
#     $1 : product name
#
# Globals used:
#     G_INSTALL_OS
#     G_INSTALL_ARCH

function GetPackagePath {
  ProductName="$1"

  case "$G_INSTALL_OS,$G_INSTALL_ARCH" in
  SunOS,sparc)
    print "${G_PRODUCT_DIR}/${ProductName}/Solaris_10+/sparc/Packages"
    ;;

  SunOS,i386)
    print "${G_PRODUCT_DIR}/${ProductName}/Solaris_10+/i386/Packages"
    ;;

  Linux,i[3-6]86 | Linux,x86_64 | Linux,athlon)
    print "${G_PRODUCT_DIR}/${ProductName}/Linux/Packages"
    ;;

  Linux,*)
    if [[ -n "${SUN_SUNRAY_SERVER_ARCH:-}" ]]; then
    	G_INSTALL_ARCH=${SUN_SUNRAY_SERVER_ARCH}
    	print "${G_PRODUCT_DIR}/${ProductName}/Linux/Packages"
    else
    	Error "unsupported platform '$G_INSTALL_OS,$G_INSTALL_ARCH'"
    fi
    ;;

  *)
    Error "unsupported platform '$G_INSTALL_OS,$G_INSTALL_ARCH'"
    ;;
  esac

  return 0
}
