#!/bin/ksh -p
#
# ident "@(#)utinstall.ksh	1.19	12/11/26 Oracle"
#
# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin
ARGS=
umask 022 
OPTSTR=":a:Acd:j:nufv"
PROGRAM_OPTS="[-u]"
DJ=false
G_PROGRAM_ID="$(basename $0)"
G_TIME_STAMP="$(date '+%Y_%m_%d_%H:%M:%S')"
G_ADM_LOG_DIR=""
UNINSTALL=false
RELEASE_PKG=SUNWutsrs

# Copy/pasted from src/release/image/utinstall.ksh, keep in sync
UTDSD_WAS_DISABLED=false
UTDSD_WAS_STOPPED=false
ETCOPT=/etc/opt/SUNWut
UTDSD_ENABLED=$ETCOPT/utenabled.utdsd

OS="$(uname -s)"
case "$OS" in
  SunOS) G_ADM_LOG_DIR="/var/adm/log";;
  Linux) G_ADM_LOG_DIR="/var/log";;
esac

G_LOGFILE="${G_ADM_LOG_DIR}/${G_PROGRAM_ID}.${G_TIME_STAMP}.log"
ERROR_FILE=$(mktemp /tmp/${G_PROGRAM_ID}.errcode.XXXXXX)

function Usage {
  cat 1>&2 <<-ENDIT
	Usage: $G_PROGRAM_ID $PROGRAM_OPTS
	 -u      uninstall the software
	 -A	 use responses from utdialog_responses.props
	 -c	 use responses from utdialog_defaults.props
	 -n	 error if user interaction is required
	ENDIT

  exit 1
}

function cleanup {
    rm -f $ERROR_FILE
}

# Copy/pasted from src/release/image/utinstall.ksh, keep in sync
function CheckSrdsUp {
    if [ -f /etc/opt/SUNWut/utadmin.conf ]; then
	if [ ! -f $UTDSD_ENABLED ]; then
	    UTDSD_WAS_DISABLED=true
	    touch $UTDSD_ENABLED
	fi
	if ! pgrep utdsd >/dev/null; then
	    UTDSD_WAS_STOPPED=true
	    /etc/init.d/utds start
	fi
    fi
}

# Copy/pasted from src/release/image/utinstall.ksh, keep in sync
function RestoreSrdsEnabledState {
    if $UTDSD_WAS_STOPPED && [ -x /etc/init.d/utds ]; then
	/etc/init.d/utds stop
    fi
    if $UTDSD_WAS_DISABLED; then
	rm -f $UTDSD_ENABLED
    fi
}

# Copy/pasted from src/release/image/utinstall.ksh, keep in sync
function RestartServiceManifest {
    typeset SVCADM="/usr/sbin/svcadm"
    typeset IMPORT_MANIFEST="svc:/system/manifest-import"
    if [ -x $SVCADM ]; then
	if [ "`uname -r`" != 5.10 ]; then
            $SVCADM restart $IMPORT_MANIFEST
        fi
    fi
}

# Copy/pasted from src/release/image/utinstall.ksh, keep in sync
function PostRestoreAction {
    RestoreSrdsEnabledState
    RestartServiceManifest
}

# main

# ensure the cleanup function gets invoked on any exit 
trap cleanup 0

while getopts $OPTSTR OPT  
do 
    case $OPT in 
        A|c|f|n|v)    ARGS="$ARGS -$OPT";;
	# we need to recognize uninstall to invoke SRWC uninstaller 
	# only  when -u is specified.
	u)   ARGS="$ARGS -$OPT"; UNINSTALL=true;;
	# we need to know which args take optargs, and handle them appropriately
	d|j|a) ARGS="$ARGS -$OPT $OPTARG"; DJ=true;; 
	# anything else is an illegal option - print Usage
	?)	Usage;;
    esac 
done 

function CheckNotSRSession {
    if [ -n "$SUN_SUNRAY_TOKEN" ]; then
	print -u2 "ERROR: You are attempting to run the uninstaller while in a Sun Ray session"
	return 1
    fi
    return 0
}

# Source the utility library 
. /opt/SUNWut/lib/support_lib/iu_lib

function main {
    if $DJ; then
	print "d, j and a are install time options. Please use only with utinstall on image."
    fi
    error_code=0

    if $UNINSTALL ; then
	if ! CheckNotSRSession; then
	    STATUS=1
	else
	    print "\nAbout to Uninstall Sun Ray Software"
	    if ! YesOrNo "\nContinue?" "Y"; then
		return 0
	    fi
	    CheckSrdsUp
	    /opt/SUNWuttsc/lib/utuninstall-srwc $ARGS
	    STATUS=$?
	    if [ $STATUS != 0 ]; then
		error_code=$STATUS 
	    fi
	    /opt/SUNWut/lib/utinstall-srss $ARGS
	    STATUS=$?
	    if [ $STATUS != 0 ]; then
		error_code=$STATUS 
	    fi

	    case "$OS" in             
		SunOS)
		    # If IPS then SUNWutsrs is already removed via
		    # R99SunRay_first_IPS. PSTAMP indicates SVr4.
		    if pkginfo -iq $RELEASE_PKG \
			&& [[ -n "$(pkgparam $RELEASE_PKG PSTAMP)" ]]; then
			PKGRM_OUT=$(pkgrm -n $RELEASE_PKG 2>&1)
			STATUS=$?
			print "$PKGRM_OUT" | egrep -v 'was successful|^$'
			if [ $STATUS != 0 ]; then
			    error_code=$STATUS 
			fi
		    fi
		    ;;
		Linux)
		    rpm -e --nodeps $RELEASE_PKG
		    STATUS=$?
		    if [ $STATUS != 0 ]; then
			error_code=$STATUS 
		    fi
		    ;;
	    esac

	    PostRestoreAction
	    STATUS=$?
	fi

	if [ $STATUS != 0 ]; then
	    error_code=$STATUS 
	fi
	if [ $error_code != 0 ]; then	    
	    echo $error_code > $ERROR_FILE
	    return $error_code
	fi	

    else
	print "Please specify -u option for uninstall. Use the downloaded image utinstall for software installation."
	return 1
    fi
}

main 2>&1 | tee -a $G_LOGFILE

cat <<-!

	Please check for errors/warnings in

	    $G_LOGFILE

	+++ Done.
	!
#
# As main is in pipe with tee, we never get the exit code of it
# (we get the one from tee instead, which is always 0). The main procedure,
# then, creates a temporary file ($ERROR_FILE) which contains the error code
# of main. 

if [[ -f $ERROR_FILE ]]; then
   exit $(cat $ERROR_FILE)
fi
exit 0 
