#!/bin/ksh -p
#
# ident "utimageinstall-srwc.ksh $Date: 2013-02-04 22:02:50 +0000 (Mon, 04 Feb 2013) $ $Revision: 15731 $ Oracle"
#
# Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
#

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

OS="$(uname -s)"

case "$OS" in
  Linux)        OS_REL="$(lsb_release -sr)";;
  SunOS)        OS_REL="$(uname -r)";;
esac

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

GetPROGRAM_DIR() {
  (cd $1; pwd)
}

PROGRAM_ID="${0##*/}"
PROGRAM_DIR="$(GetPROGRAM_DIR ${0%/*})"

Usage() {
  cat 1>&2 <<-EOF
	Usage: $PROGRAM_ID $PROGRAM_OPTS

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

	   -d dir
	      specify a directory to install the software from
	EOF

  exit 1
}

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

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

UPGRADE_QUESTION () {
  print "\nSun Ray Connector $PROD_VER is currently installed."
  print "Do you want to uninstall it"
  print "and install Sun Ray Connector 2.6?"
}

PreserveGroupName () {
    key=admin.srwc.groupname
    UTCONF_FILE=/etc/opt/SUNWut/utadmin.conf

    # If there used to be SRWC software on the system, 
    # look for groupname in utadmin.conf or derive it from uttsc-bin's groupname and save it.
    
    GROUPNAME=$(sed -n -e "s/^[ 	]*${key}[ 	]*=[ 	]*//p" $UTCONF_FILE 2>/dev/null)
    if ! [ -n "$GROUPNAME" ]; then
	GROUPNAME="$(ls -lL  /opt/SUNWuttsc/lib/uttsc-bin | awk -F" " '{print $4}')"  
	echo "admin.srwc.groupname	= $GROUPNAME" >> /etc/opt/SUNWut/utadmin.conf
    fi
}

REMOVE_PROD () {
    ## Begin workaround for bug with SRWC data loss during upgrades
    ##Delete this code when upgrades from 5.4 is no longer supported
    if ! $UNINSTALL && $IS_UPGRADE && /opt/SUNWut/sbin/utrelease | awk '/^Sun Ray Software/{ split($NF, verarr, "."); if ( verarr[1] <= 5 && verarr[2] <= 4 ) exit 0 ; else exit 1 }'; then
        $UNINSTALLER_IMAGE -u -p
        return
    ## end workaround for bug with SRWC data loss during upgrades
    elif [[ -f $UNINSTALLER ]]; then 
            $UNINSTALLER -u
    else
            $UNINSTALLER_IMAGE -u
    fi

}

UPGRADE_PROD () {

    IS_UPGRADE=true
    PreserveGroupName
}
# main() {

OPTSTR="Aa:cd:D:j:nufp:v"
PROGRAM_OPTS="[-a admin] [-d dir] [-u]"

FORCE=false
OPT_V=""
IMAGE_ROOT=""
ADMIN_FILE=""
PREFIX=""
UNINSTALL=false
UNINSTALLER=""
UNINSTALLER_IMAGE=""
IS_UPGRADE=false
GROUPNAME=""

while getopts $OPTSTR OPT; do
  case "$OPT" in
    A|c|D|n) ;;	# utdialog opts required by common installer fw, but irrelevant here
    f) FORCE=true;;
    u) UNINSTALL=true;;
    v) OPT_V="-v";;
    d) IMAGE_ROOT="$OPTARG";;
    a) ADMIN_FILE="$OPTARG";;
    p) PREFIX="$OPTARG";;
    j) ;;
    *) 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

if [[ -z "$IMAGE_ROOT" ]] || $UNINSTALL ; then
  IMAGE_ROOT="$PROGRAM_DIR"
fi

if [[ ! -d $IMAGE_ROOT ]]; then
    Fatal "'$IMAGE_ROOT' is not a directory"
fi

case "$OS" in
SunOS)
  if [[ -n "$PREFIX" ]]; then
    Fatal "can not specify -p option on Solaris"
  fi

  if [[ -z "$ADMIN_FILE" ]]; then
    ADMIN_FILE="$IMAGE_ROOT/../../../.install/admin_default"
  fi
  if [[ ! -r $ADMIN_FILE ]]; then
    Fatal "can not read '$ADMIN_FILE'"
  fi
  ;;

Linux)
  if [[ -n "$ADMIN_FILE" ]]; then
    Fatal "can not specify -a option on Linux"
  fi
  if [[ -z "$PREFIX" ]]; then
    PREFIX="/opt"
  fi

  ;;
esac

#LICENSE_AGREEMENT="$IMAGE_ROOT/../../../Legal/SLA.txt"

#if [[ ! -r "$LICENSE_AGREEMENT" ]]; then
#  Fatal "can not read '$LICENSE_AGREEMENT'"
#fi

#PROMPT="Accept (Y/N):"

#if $FORCE; 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

PRODUCT_ROOT="$IMAGE_ROOT/Sun_Ray_Connector_2.6"
UNINSTALLER_IMAGE="$IMAGE_ROOT/utuninstall-srwc"
UNINSTALLER=""
UTPRODINFO=/opt/SUNWut/lib/utprodinfo
BASEPKG="SUNWuttsc"

case "$OS,$OS_REL,$ISA" in
  SunOS,5.11,sparc)     DIR="$PRODUCT_ROOT/Solaris_11+/sparc/Packages";;
  SunOS,5.10,sparc)     DIR="$PRODUCT_ROOT/Solaris_10/sparc/Packages";;
  SunOS,5.11,i386)      DIR="$PRODUCT_ROOT/Solaris_11+/i386/Packages";;
  SunOS,5.10,i386)      DIR="$PRODUCT_ROOT/Solaris_10/i386/Packages";;
  Linux,*,*)            DIR="$PRODUCT_ROOT/Linux/Packages";;
esac

  if  ls  /etc/opt/SUNWut/utadmin.conf 1> /dev/null 2>&1 ; then
  	UPGRADE_PROD
    fi

case "$OS" in
SunOS)
  PKGS="SUNWuttsc SUNWuttscr"
  KIOPKG="SUNWuttsc-kiosk"
  DIALOG_PKG="SUNWuttscd"
  KIODEP="SUNWkio"
  IPS_REPO=$PRODUCT_ROOT/../IPS.$ISA
  
  INSTALL_OPTS="-a $ADMIN_FILE"

  if pkginfo -q $BASEPKG; then
    PROD_VER="$($UTPRODINFO -p $BASEPKG PRODVERS)"
    UNINSTALLER="/opt/SUNWuttsc/lib/utuninstall-srwc"

    if $UNINSTALL; then
	REMOVE_PROD
	exit 0
    else
	UPGRADE_PROD
	REMOVE_PROD
    fi
  elif $UNINSTALL; then 
      # this is the corner case, where no SRWC package was present on the 
      # system and yet -u was called - do nothing and exit.
      exit 0
  fi

  print "\n+++ Installing 'Sun Ray Connector 2.6'"


  if [[ "$OS_REL" != "5.10" ]]; then
    PKGS=SUNWut-srwc
    if pkginfo -q $KIODEP ; then
      PKGS="$PKGS $KIOPKG"
    fi
    pkg install $PKGS
  else
    if pkginfo -q $KIODEP ; then
      PKGS="$PKGS $KIOPKG"
    else  
      print "\n+++ Kiosk Mode Package '$KIODEP' is not installed"
      print   "+++ Skipping '$KIOPKG'."
    fi	  

    for PKG in $PKGS
    do
      if pkginfo -q $PKG; then
	PROD_VER="$($UTPRODINFO -p $PKG PRODVERS)"
	print "\n+++ Package '$PKG' (version $PROD_VER) is already installed"
	print   "+++ Skipping '$PKG' (version 2.6)."
	
      else  
	print "\n+++ Adding '$PKG'"
	pkgadd $OPT_V $INSTALL_OPTS -d $DIR $PKG
      fi  
    done

    LADMIN_FILE=/var/opt/SUNWut/tmp/admin.$$
    rm -f $LADMIN_FILE
    cat > $LADMIN_FILE <<-EOF
	basedir=/opt
	mail=
	instance=unique
	partial=quit
	runlevel=nocheck
	idepend=nocheck
	rdepend=nocheck
	space=quit
	setuid=nocheck
	conflict=nocheck
	action=nocheck
EOF
    pkgadd $OPT_V -a $LADMIN_FILE -d $DIR $DIALOG_PKG
    rm -f $LADMIN_FILE
  fi
  ;;

Linux)
  PKG="SUNWuttsc" 
  ROOT_PKG="SUNWuttscr" 
  DIALOG_PKG="SUNWuttscd" 
  NORELOC_PKG="SUNWuttsc-kiosk"
  INSTALL_OPTS="--prefix $PREFIX $INSTALL_PATH"
  
  case "$OS_REL" in
    5.?)        OS_REL_TAG="el5";;
    6.?)        OS_REL_TAG="el6+";;
  esac

  if $UTPRODINFO -t installed $BASEPKG; then
    PROD_VER="$($UTPRODINFO -p $BASEPKG PRODVERS)"
    UNINSTALLER="/opt/SUNWuttsc/lib/utuninstall-srwc"

    if $UNINSTALL; then
	REMOVE_PROD
	exit 0
    else
	UPGRADE_PROD
	REMOVE_PROD
    fi
  elif $UNINSTALL; then
      # this is the corner case, where no SRWC package was present on the 
      # system and yet -u was called - do nothing and exit.
      exit 0
  fi
  if [[ -n "$OPT_V" ]]; then
    OPT_V="-vv"
  fi
  print "\n+++ Installing 'Sun Ray Connector 2.6'"

  print "\n+++ Adding '$PKG'"
  rpm $OPT_V -i --nodeps $INSTALL_OPTS $DIR/$PKG-[0-9]*.$OS_REL_TAG.*.rpm

  print "\n+++ Adding '$ROOT_PKG'"
  rpm $OPT_V -i --nodeps $DIR/$ROOT_PKG-[0-9]*.$OS_REL_TAG.*.rpm

  print "\n+++ Adding '$DIALOG_PKG'"
  # Figure out if SUNWuti has been relocated, and if so to what path, then
  # follow its lead for the dialog files
  UTI_PREFIX="$(rpm -q --qf='%{PREFIXES}' SUNWuti)"
  rpm $OPT_V -i --nodeps --prefix $UTI_PREFIX $DIR/$DIALOG_PKG-[0-9]*.$OS_REL_TAG.*.rpm


  if rpm -q $NORELOC_PKG 1>&- 2>&-; then
    PROD_VER="$(rpm -q --qf='%{VERSION}' $NORELOC_PKG)"
    print "\n+++ RPM '$NORELOC_PKG' (version $PROD_VER) is already installed"
    print   "+++ Skipping '$NORELOC_PKG' (version 2.6)."
  else
    print "\n+++ Adding '$NORELOC_PKG'"
    rpm $OPT_V -i --nodeps $DIR/$NORELOC_PKG-[0-9]*.$OS_REL_TAG.*.rpm
  fi
  ;;
esac

if $IS_UPGRADE ; then
    if ! [ -n "$GROUPNAME" ]; then
	print "Run /opt/SUNWut/sbin/utconfig -c to configure Sun Ray Windows Connector"
    else
	/opt/SUNWuttsc/lib/uttscadm -m -g $GROUPNAME
    fi
fi

exit 0
# }
