#!/bin/ksh -p
#
# ident "@(#)$Id: utuninstall-srwc.ksh 12881 2011-04-08 21:38:10Z shefali $ Oracle"
#
# Copyright (c) 2005, 2011, 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 <<-!
	Usage: $PROGRAM_ID $PROGRAM_OPTS

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

  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:nufv"
PROGRAM_OPTS="[-a admin] [-u]"

ADMIN_FILE=""
OPT_V=""
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;; 
  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 ]]; then
	    $UTTSCADM -u
	else 
	    CONTENT_BASE=`/usr/bin/dirname $0`
	    $CONTENT_BASE/uttscadm -u
	fi
    }


    case "$OS" in
	SunOS)
	PKGS="SUNWuttsc-kiosk SUNWuttscr SUNWuttscd SUNWuttsc"
	UNCONFIG_PROD
	for PKG in $PKGS 
	  do
	  if pkginfo -q $PKG; then
	      print "\n+++ Removing '$PKG'"
	      pkgrm $OPT_V -n -a $ADMIN_FILE $PKG
	  fi
	done
	;;
	
	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
# }
