#!/bin/ksh -p
#
# ident "@(#)utimageinstall-srss.ksh	1.38	12/09/06 Oracle"
#
# Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
#

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

OS="$(uname -s)"

function GetParentDir {
  (cd $1; /bin/pwd)
}

PROGRAM="$0"
PDIR="$(dirname $PROGRAM)"
# were we invoked via symlink? If so, what's our real path?
if [ -L "$PROGRAM" ]; then
  # Expand the symlink
  PROGRAM=$(ls -l "$PROGRAM" | /bin/awk '{print $NF}')
  # If the symlink is relative, construct a full path by basing it
  # from the dir in which it resides (of course, that could have
  # contained a symlink as well, but that shouldn't affect finding
  # things within the image using this path) and recalculate the
  # parent dir
  if [ -z "${PROGRAM%%../*}" ]; then
    PROGRAM=$PDIR/$PROGRAM
    PDIR=$(dirname "$PROGRAM")
  fi
fi
PROGRAM_ID="$(basename $PROGRAM)"
IMAGE_BASE_DIR="$(GetParentDir $PDIR)/../../.."
COMPONENT_BASE_DIR="$(GetParentDir $PDIR)/"

function Usage {
  cat 1>&2 <<-EOF
	Usage: $G_PROGRAM_ID [opts]

	 -a admin_file
	         specify an admin(4) file to be used by pkgadd(1m)
		 calls during install (Solaris only)

	 -d media_dir
	         specify a directory to install the software from

	 -j jre
	         specify a directory to find the Java Runtime Environment

	 -u      uninstall the software
	EOF

  exit 1
}

function Fatal {
  print -u2 "$PROGRAM_ID: fatal, $1"
  exit 1
}

function CheckSetOptA {
  case "$OS" in
    Linux) Fatal "can not use option '-a' on Linux";;
    *)     ARGS="$ARGS -a $1"; OPT_a=true;;
  esac
}

function CheckInstalledFW {
  typeset rootdir=$1
  typeset fwver

  fwver=$(installed_version $rootdir SUNWutdfw)

  if [ "$fwver" = 0 ]; then
    print -n "WARNING: "
    if [ -d /opt/SUNWut/lib/firmware ]; then
      print -n "The latest version of "
    fi
    print "Sun Ray Operating Software (firmware) is not installed."
    print "It is recommended that you install the latest firmware"
    print "before installing Sun Ray Software."
    if [ "$($UTDIALOG -H YesOrNo:Y utinstall.NoFWContinueYN)" = N ]; then
      return 1
    fi
    return 0
  fi

  if [ ${fwver#*_} = "Experimental" ]; then
    # If either fw or srs version is experimental, allow it
    return 0
  fi

  CompareFullVersion $fwver 11.1.0.0
  STATUS=$?

  if [ "$STATUS" = "$G_COMPVER_LT" ]; then
    print "You are attempting to install SRS on a system with an old version"
    print "of Sun Ray Operating Software (firmware)".
    if [ "$($UTDIALOG -H YesOrNo:N utinstall.OldFWContinueYN)" = N ]; then
      return 1
    fi
  fi
  return 0
}

function cleanup {
  if [ -n "$TMP_DIR" ]; then
    # no need to 'pkgrm' or 'rpm -e' the
    # SUNWuti pkg in TMP_DIR
    rm -rf $TMP_DIR
  fi
}

# main() {

. $IMAGE_BASE_DIR/Lib/install-support-lib

trap cleanup 0

LICENSE_AGREEMENT="$IMAGE_BASE_DIR/Legal/SLA.txt"

OPTSTR=":a:Acd:D:fj:nuv"

OPT_a=false
OPT_d=false
OPT_f=false
OPT_A=false
OPT_u=false
DIALOG_DIR=/var/opt/SUNWut/utdialog.d
UTDIALOG_OPTS="-c SRSS"
ARGS=""

# Installation and support of SRSS in local zones on Solaris
# not supported
if [ -x /bin/zonename -a "$(/bin/zonename 2> /dev/null)" != "global" ]
then
 print -u2 $PROGRAM_ID can only be run from the global zone
 exit 1
fi 

while getopts $OPTSTR OPT; do
  case "$OPT" in
    A)   ARGS="$ARGS -$OPT"; UTDIALOG_OPTS="$UTDIALOG_OPTS -a"; OPT_A=true;;
    c)   ARGS="$ARGS -$OPT"; UTDIALOG_OPTS="$UTDIALOG_OPTS -d";;
    a)   CheckSetOptA "$OPTARG";;
    d)   ARGS="$ARGS -d $OPTARG"; OPT_d=true;;
    D)   DIALOG_DIR=$OPTARG;;
    f)   ARGS="$ARGS -$OPT"; OPT_f=true;;
    j)   ARGS="$ARGS -j $OPTARG";;
    n)   ARGS="$ARGS -$OPT"; UTDIALOG_OPTS="$UTDIALOG_OPTS -n";;
    u)   ARGS="$ARGS -$OPT"; OPT_u=true;;
    v)   ARGS="$ARGS -$OPT";;
    *)   Usage;;
  esac
done
shift $(($OPTIND - 1))

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

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

TMP_DIR=$(mktemp -d /tmp/$PROGRAM_ID.$$.XXXXXX)

UTDIALOG="$TMP_DIR/opt/SUNWut/lib/utdialog $UTDIALOG_OPTS"

mkdir -m 0700 $TMP_DIR/opt

TMP_VAR_DIALOG_DIR=$TMP_DIR/var/opt/SUNWut/utdialog.d
mkdir -p $TMP_VAR_DIALOG_DIR
mkdir -p $TMP_DIR/var/opt/SUNWut/tmp

UTDIALOG_RESPONSE_FILE_PAT="$DIALOG_DIR/*.utdialog_responses.props"
# Try to expand the glob pattern (it won't expand if no matches)
UTDIALOG_RESPONSE_FILES=$(print $UTDIALOG_RESPONSE_FILE_PAT)
# If the files exist, copy them where needed
if [ "$UTDIALOG_RESPONSE_FILES" = "$UTDIALOG_RESPONSE_FILE_PAT" ]; then
  if $OPT_A; then
    print -u2 "$PROGRAM_ID: No response files found"
    Usage
    # NOT REACHED
  fi
else
  # if dialog files are specified, "install them" into temp location
  cp $UTDIALOG_RESPONSE_FILES $TMP_VAR_DIALOG_DIR
  chmod 0600 $TMP_VAR_DIALOG_DIR/*.utdialog_responses.props
fi

OS="$(uname -s)"

ISA="$(uname -m)"
case "$ISA" in
  sun4*)      ISA=sparc;;
  i?86*|i86*) ISA=i386;;
esac  

CORE_DIR="$COMPONENT_BASE_DIR/Sun_Ray_Core_Services_4.5"
case "$OS" in
SunOS)
  case "$ISA" in
    sparc) PKG_DIR="$CORE_DIR/Solaris_10+/sparc/Packages";;
    i386)  PKG_DIR="$CORE_DIR/Solaris_10+/i386/Packages";;
  esac
  cp $IMAGE_BASE_DIR/.install/admin_default $TMP_DIR
  pkgadd -n -R $TMP_DIR -a $TMP_DIR/admin_default -d $PKG_DIR SUNWuti
  pkgadd -n -R $TMP_DIR -a $TMP_DIR/admin_default -d $PKG_DIR SUNWutid
  if [ $? -ne 0 ]; then
    Fatal "SRSS installer installation failed"
  fi
  ;;

Linux)
  PKG_DIR="$CORE_DIR/Linux/Packages"
  mkdir -p $TMP_DIR/bin
  mkdir -p $TMP_DIR/var/lib/rpm
  mkdir -p $TMP_DIR/var/lock/rpm
  PKGS_RPM="/var/lib/rpm/packages.rpm"
  if [[ -f $PKGS_RPM ]]; then
    ln -s $PKGS_RPM $TMP_DIR$PKGS_RPM
  fi
  rpm --root $TMP_DIR --initdb
  rpm --root $TMP_DIR --nodeps -i $PKG_DIR/SUNWuti-4.5-?*.i386.rpm
  rpm --root $TMP_DIR --nodeps -i $PKG_DIR/SUNWutid-4.5-?*.i386.rpm
  ;;
esac

if ! $OPT_u;  then
  if $OPT_f; then
    cat $LICENSE_AGREEMENT
    $TMP_DIR/opt/SUNWut/lib/utdialog -d $UTDIALOG_OPTS -H YesOrNo:Y utinstall.AcceptLicenseYN
    # answer 'Y' is supplied
  else
    # XXX
    cat $LICENSE_AGREEMENT
    if [ $($UTDIALOG -H YesOrNo utinstall.AcceptLicenseYN) = N ]; then
      exit 1
    fi
  fi
fi

if [[ "$OS" != "Linux" ]]; then
  if ! $OPT_a; then
    ARGS="$ARGS -a $TMP_DIR/opt/SUNWut/etc/admin_default"
  fi
fi

if ! $OPT_d; then
  ARGS="$ARGS -d ${COMPONENT_BASE_DIR}"
fi

if ! $OPT_u; then
  if ! CheckInstalledFW "$TMP_DIR"; then
    exit 1
  fi
fi

$TMP_DIR/opt/SUNWut/lib/utinstall-srss $ARGS
STATUS=$?

if [ $STATUS -eq 0 ]; then
  if [ -d /var/opt/SUNWut ]; then
    # Copy the dialog files into the final location
    cp -p $TMP_VAR_DIALOG_DIR/*.utdialog_responses.props /var/opt/SUNWut/utdialog.d
  else
    cp -p $TMP_VAR_DIALOG_DIR/*.utdialog_responses.props /tmp
  fi
fi

exit $STATUS

# }
