#!/bin/ksh -p
#
# ident "@(#)utinstall-srss.ksh	1.27	10/07/23 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)/.."

Usage() {
  cat 1>&2 <<-!
	Usage: $G_PROGRAM_ID $PROGRAM_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
	!

  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:d:j:ufv"
PROGRAM_OPTS="[-a admin_file] [-d media_dir] [-j jre] [-u]"

OPT_F=false
OPT_A=false
OPT_D=false
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
    f)   ARGS="$ARGS -$OPT"; OPT_F=true;;
    u|v) ARGS="$ARGS -$OPT";;
    a)   CheckSetOptA "$OPTARG";;
    d)   ARGS="$ARGS -d $OPTARG"; OPT_D=true;;
    j)   ARGS="$ARGS -j $OPTARG";;
    *)   Usage;;
  esac
done
shift $(($OPTIND - 1))

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

PROMPT="Accept (Y/N):"

if $OPT_F; then
  cat $LICENSE_AGREEMENT
  print "$PROMPT Y"   # answer 'Y' is supplied
else
  more $LICENSE_AGREEMENT

  while true; do
    print -n "$PROMPT "
    read RESPONSE
    case "$RESPONSE" in
      [Yy]|[Yy][Ee][Ss]) break;;
      [Nn]|[Nn][Oo])     exit 1;;
    esac
  done
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 -p $TMP_DIR/opt

OS="$(uname -s)"

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

CORE_DIR="$IMAGE_BASE_DIR/Sun_Ray_Core_Services_4.2"
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
  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.2-?*.i386.rpm
  ;;
esac

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 ${IMAGE_BASE_DIR}"
fi

$TMP_DIR/opt/SUNWut/sbin/utinstall $ARGS

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

exit 0

# }
