#!/bin/ksh -p
#
# ident "@(#)utimageinstall-srss.ksh	1.32	10/12/17 Oracle"
#
# Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
#

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

OS="$(uname -s)"

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)/"

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
}

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

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

# main() {

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

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

OPT_A=false
OPT_D=false
OPT_F=false
DIALOG_DIR=
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";;
    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|v) ARGS="$ARGS -$OPT";;
    *)   Usage;;
  esac
done
shift $(($OPTIND - 1))

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

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

TMP_DIR="/tmp/$PROGRAM_ID.$$"

rm -rf $TMP_DIR

mkdir -m 0700 -p $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

if [ -n "$DIALOG_DIR" ]; then
  # if dialog files are specified, "install them" into temp location
  cp $DIALOG_DIR/*.utdialog_responses.props $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.3"
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.3-?*.i386.rpm
  rpm --root $TMP_DIR --nodeps -i $PKG_DIR/SUNWutid-4.3-?*.i386.rpm
  ;;
esac

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 [ $($TMP_DIR/opt/SUNWut/lib/utdialog $UTDIALOG_OPTS -H YesOrNo utinstall.AcceptLicenseYN) = N ]; then
    rm -rf $TMP_DIR
    exit 1
  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

$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

rm -rf $TMP_DIR
#
# no need to 'pkgrm' or 'rpm -e' the SUNWuti pkg in TMP_DIR

exit $STATUS

# }
