#!/bin/ksh -p
#
# ident "uninstaller.ksh $Date: 2007-06-13 14:16:48 -0700 (Wed, 13 Jun 2007) $ $Revision: 157 $"
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Solaris 10 Trusted Extension guard
#
ORIGIN=`/usr/bin/dirname $0`
TXGUARD=${ORIGIN:-/opt/SUNWuttsc/lib}/../lib/txguard
. $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)/.."
  )
}

# main() {

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

OPTSTR="a:v"
PROGRAM_OPTS="[-a admin]"

ADMIN_FILE=""
OPT_V=""

while getopts $OPTSTR OPT; do
  case "$OPT" in
    f) FORCE=true;;
    v) OPT_V="-v";;
    a) ADMIN_FILE="$OPTARG";;
    *) 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

print "\n+++ Uninstalling 'Sun Ray Connector 2.0'"

PROMPT="About to uninstall 'Sun Ray Connector 2.0'. Continue? (Y/[N]):"

REMOVE_PROD () {
   while true; do
         print -n "\n"
         print -n "$PROMPT "
           read RESPONSE
           case "$RESPONSE" in
             [Yy]|[Yy][Ee][Ss]) break;;
             [Nn]|[Nn][Oo] | "")   exit 1;;
           esac
   done
}

case "$OS" in
SunOS)
  PKGS="SUNWuttsc-kiosk SUNWuttscr SUNWuttsc"
  REMOVE_PROD
  for PKG in $PKGS 
  do
	 print "\n+++ Removing '$PKG'"
 	 if pkginfo -q $PKG; then
	    pkgrm $OPT_V -n -a $ADMIN_FILE $PKG
	  else
	    Error "package $PKG is not installed"
	  fi
  done
  ;;

Linux)
  PKGS="SUNWuttsc-kiosk SUNWuttsc"
    REMOVE_PROD
  for PKG in $PKGS 
  do
      print "\n+++ Removing '$PKG'"
      if [[ -n "$OPT_V" ]]; then
	OPT_V="-vv"
      fi
      if rpm -q $PKG 2>&-; then
	rpm $OPT_V -e --nodeps $PKG
      else
	Error "RPM $PKG is not installed"
      fi
  done
  ;;
esac

rm -rf $TMP_DIR

exit 0
# }
