#!/bin/ksh -p
#
# ident "@(#)$Id: utuninstall-srwc.ksh 15731 2013-02-04 22:02:50Z lea.li@oracle.com $ Oracle"
#
# Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
#

#
# Solaris 10 Trusted Extension guard
#
ORIGIN=`/usr/bin/dirname $0`
TXGUARD=/opt/SUNWuttsc/lib/txguard
UTTSCADM=${ORIGIN:-/opt/SUNWuttsc/lib}/uttscadm

. $TXGUARD
#
# Solaris 10 Trusted Extension guard 
#


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

PROGRAM_ID="${0##*/}"

OS="$(uname -s)"

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

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

	 -a admin
	         specify an admin(4) file to be used by pkgrm(1m)
		 calls during uninstall (Solaris only)
	EOF

  exit 1
}

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

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

GetPRODUCT_DIR() {
  (
    cd $(dirname $1)
    print "$(pwd)/.."
  )
}

installed_version() {
    typeset iver
    utprodinfo=/opt/SUNWut/lib/utprodinfo
    BASEPKG=SUNWuttsc

    if [ -x $utprodinfo ] && iver="$($utprodinfo -p $BASEPKG VERSION 2>/dev/null)" ; then
       print ${iver%%_*}
   else
       print ""
   fi
}
# main() {

TMP_DIR="/tmp/$PROGRAM_ID.$$"
rm -rf $TMP_DIR      # make sure it doesn't exist
mkdir -p $TMP_DIR    # create it anew

OPTSTR="Aa:cd:D:j:nufvp"
PROGRAM_OPTS="[-a admin] [-u]"

ADMIN_FILE=""
OPT_V=""
UNINSTALL=false
PRESERVE_UNINSTALL=false

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;;
    v) OPT_V="-v";;
    a) ADMIN_FILE="$OPTARG";;
    u) UNINSTALL=true;; 
    p) PRESERVE_UNINSTALL=true;; # Preserve SRDS data (for use during upgrade) 
  d|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



case "$OS" in
SunOS)
  if [[ -z "$ADMIN_FILE" ]]; then
  ADMIN_FILE="/opt/SUNWuttsc/etc/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
  ;;
esac

if $UNINSTALL; then

    inst_ver="$(installed_version)"
    print "\n+++ Uninstalling 'Sun Ray Connector $inst_ver'"
      
    UNCONFIG_PROD () {
        if [[ -z $UTTSCADM ]] && ! $PRESERVE_UNINSTALL; then #added ! $PRESERVE_UNINSTALL condition to correct a problem with SRWC data loss during upgrades 
	    $UTTSCADM -u
        else 
            CONTENT_BASE=`/usr/bin/dirname $0`	
        ## Begin workaround for bug with SRWC data loss during upgrades
        ##Delete this code when upgrades from 5.4 is no longer supported
            if $PRESERVE_UNINSTALL && /opt/SUNWut/sbin/utrelease | awk '/^Sun Ray Software/{ split($NF, verarr, "."); if ( verarr[1] <= 5 && verarr[2] <= 4 ) exit 0 ; else exit 1 }';  then
                 $CONTENT_BASE/uttscadm -u -p
        ##end workaround for bug with SRWC data loss during upgrades
            else
                 $CONTENT_BASE/uttscadm -u
            fi
         fi
    }


    case "$OS" in
	SunOS)
	PKGS="SUNWuttsc-kiosk SUNWuttscr SUNWuttscd SUNWuttsc"
	UNCONFIG_PROD
	if [[ "$(uname -r)" != "5.10" ]]; then
		pkg uninstall SUNWut-srwc $PKGS
	else
		for PKG in $PKGS 
		do
			if pkginfo -q $PKG; then
				print "\n+++ Removing '$PKG'"
				pkgrm $OPT_V -n -a $ADMIN_FILE $PKG
			fi
		done
	fi
	;;
	
	Linux)
	PKGS="SUNWuttsc-kiosk SUNWuttscr SUNWuttscd SUNWuttsc"
	UNCONFIG_PROD
	for PKG in $PKGS 
	  do
	  if [[ -n "$OPT_V" ]]; then
	      OPT_V="-vv"
	  fi
	  if rpm -q $PKG 1> /dev/null 2>&1; then
	      print "\n+++ Removing '$PKG'"
	      rpm $OPT_V -e --nodeps $PKG
	  fi
	done
	;;
    esac
    
    rm -rf $TMP_DIR
fi
exit 0
# }
