#!/bin/ksh -p
#
# ident "@(#)utpkg-utwarctl.ksh	1.3 10/02/03 SMI"
#
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u

function disable {

   
# Trigger stop command just in case the web server is still running
    typeset PROCESS_PIDFILE=/var/opt/SUNWut/tmp/utwebadmin.pid
    if [ -f "$PROCESS_PIDFILE" ]; then
	typeset PID=`cat $PROCESS_PIDFILE`
	if [ -n "`/bin/ps -deaf | awk '{print $2}' | grep $PID`" ]; then
	    kill -9 $PID >/dev/null 2>&1
	fi
	rm -f $PROCESS_PIDFILE
    fi
    
# Remove temporary files from etc, log and work directory
    typeset LOG_FILES=/var/opt/SUNWut/log/utwebadmin.log*
    typeset SERVER_FILE=/etc/opt/SUNWut/webadmin/server.xml
    typeset WEB_FILE=/etc/opt/SUNWut/webadmin/web.xml
    typeset KEYSTORE_FILE=/etc/opt/SUNWut/webadmin/keystore
    typeset WORK_DIR=/var/opt/SUNWut/webadmin
    
    rm -f $LOG_FILES
    if [ -f "$SERVER_FILE" ]; then
	rm -f $SERVER_FILE
    fi
    if [ -f "$WEB_FILE" ]; then
	rm -f $WEB_FILE
    fi
    if [ -f "$KEYSTORE_FILE" ]; then
	rm -f $KEYSTORE_FILE
    fi
    
    if [ -d "$WORK_DIR" ]; then
	rm -rf $WORK_DIR
    fi
    
#
# On Linux, we need to remove the links to init.d files using
# chkconfig.  
    typeset INITFILE="utwadmin"
    chkconfig --del $INITFILE > /dev/null
	
    #
    # On SuSE/JDS, chkconfig (based on insserv) does not remove Kill links in
    # lower runlevels.  They must be removed manually.
    #
    if [ -f /etc/SuSE-release ]; then
	# we are installing on either SuSE or JDS
	typeset ETCINIT=/etc/rc.d
	rm -f $ETCINIT/rc4.d/K??$INITFILE
	rm -f $ETCINIT/rc3.d/K??$INITFILE
	rm -f $ETCINIT/rc2.d/K??$INITFILE
	rm -f $ETCINIT/rc1.d/K??$INITFILE
    fi
    
    return 0
    

}

function enable {

    #
    # On Linux, we need to create the links to init.d files using
    # chkconfig.
    typeset INITFILE="utwadmin"
    chkconfig --add $INITFILE
    #
    # On SuSE/JDS, chkconfig (based on insserv) does not create Kill links in
    # lower runlevels.  They must be created manually.
    #
    if [ -f /etc/SuSE-release ]; then
	# we are installing on either SuSE or JDS
	typeset ETCINIT=/etc/rc.d
	typeset KILLFILE="K35utwadmin"
	typset INITF=`echo $KILLFILE | sed -e "s/^K..//"`

	rm -f $ETCINIT/rc4.d/$KILLFILE
	rm -f $ETCINIT/rc3.d/$KILLFILE
	rm -f $ETCINIT/rc2.d/$KILLFILE
	rm -f $ETCINIT/rc1.d/$KILLFILE

	ln -s ../$INITF $ETCINIT/rc4.d/$KILLFILE
	ln -s ../$INITF $ETCINIT/rc3.d/$KILLFILE
	ln -s ../$INITF $ETCINIT/rc2.d/$KILLFILE
	ln -s ../$INITF $ETCINIT/rc1.d/$KILLFILE
    fi
    
    return 0

}


########################################################################
# Local Variables
# They must defined here to resolve some of the parameter resolution
# problem
########################################################################
local_variables() {
    return
}


########################################################################
#
# Configurable parameter
#
########################################################################
# NONE


########################################################################
#
# Variables used in the shared script utctl-shlib
#
########################################################################
DESCR="Establishes/cleans up the environment for the Web Admin service"


########################################################################
#
# Execution starts here
#
PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
