#!/bin/ksh -p
#
# ident "$Id$"
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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_file
	         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_file]"

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

if [[ -z "$ADMIN_FILE" ]]; then
  ADMIN_FILE="/opt/SUNWuttsc/etc/admin_default"
  #
  # must be assigned after cmdline opts are parsed, PRODUCT_DIR change
else
  case "$OS" in
    Linux) Fatal "option '-a' can not be used on Linux";;
  esac
fi

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

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

PKG="SUNWuttsc"

print "\n+++ Removing '$PKG'"

case "$OS" in
SunOS)
  if pkginfo -q $PKG; then
    pkgrm $OPT_V -n -a $ADMIN_FILE $PKG
  else
    Error "package $PKG is not installed"
  fi
  ;;

Linux)
  if [[ -n "$OPT_V" ]]; then
    OPT_V="-vv"
  fi
  if rpm -q $PKG 2>&-; then
    rpm $OPT_V -e $PKG
  else
    Error "RPM $PKG is not installed"
  fi
  ;;
esac

rm -rf $TMP_DIR

exit 0
# }
