#!/bin/ksh -p
#
# ident "@(#)utpkg-utr-ctl.ksh	1.7 09/10/08 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

set -u

function disable {    
    
#
# Clean up /var/opt/
# 
#
   
#
# no need to prepend the PKG_INSTALL_ROOT to UTLIBDIR because it's
# only used within the crontab.
    UTLIBDIR=/opt/SUNWut/lib
    
#
# Remove symlinks
#
    rm -rf /var/opt/SUNWut/tokens /var/opt/SUNWut/idle /var/opt/SUNWut/displays
    rm -rf /var/opt/SUNWut/dispinfo
    rm -rf /var/opt/SUNWut/snmp 2
    
#
# Clean up directories
#

    for dir in /var/opt/SUNWut/log /var/opt/SUNWut/cgitokens; do
	if [ -d "$dir" ]; then
	    find $dir -type f -exec rm {} \;
	fi
    done
    
    
#
# Clean up /etc/opt/
#
       
    rm -f /etc/opt/SUNWut/utadmin.conf
    rm -f /etc/opt/SUNWut/utadmin.pw
    rm -f /etc/opt/SUNWut/gmSignature
    rm -f /etc/opt/SUNWut/ut_disable_*
    rm -f /etc/opt/SUNWut/jre
    rm -f /etc/opt/SUNWut/guijre
    [ -d /etc/opt/SUNWut/net ] && find /etc/opt/SUNWut/net -type f -exec rm -f {} \;
    
#
# Cleanup /etc/dt
#
# NOTE: these directoires do not exist on Linux
    EDTDIR=/etc/dt/config
    
    for i in Xservers Xconfig; do
#
# Remove the symlink made by utacleanup
#
	if [ -h $EDTDIR/$i ]; then
	    rm -f $EDTDIR/$i
	fi
	
#
# If a saved "prototype" template file exists, move it into place as
# the new site-customised config file.
#
	proto=$EDTDIR/$i.SUNWut.prototype
	if [ -f $proto ]; then
	    mv -f $proto $EDTDIR/$i
	fi
    done
    
    rm -f /etc/opt/SUNWut/utctl.conf
    rm -f /etc/opt/SUNWut/utctl.run

    
    OS=`uname -s`
#
# For Linux only.  We need to remove the links to init.d files using
# chkconfig.  This is something else we can't do in an altroot install,
# but luckily Linux doesn't support altroot installs anyway.
#
    if [ $OS = "Linux" ]; then
	INITFILES="utsvc utacleanup utsyscfg"
	for F in $INITFILES
	  do
	  chkconfig --del $F > /dev/null
	done
#
# On SuSE, 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
	    ETCINIT=/etc/rc.d
	    for F in $INITFILES
	      do
	      rm -f $ETCINIT/rc4.d/K??$F
	      rm -f $ETCINIT/rc3.d/K??$F
	      rm -f $ETCINIT/rc2.d/K??$F
	      rm -f $ETCINIT/rc1.d/K??$F
	    done
	fi
	
# cleanup the log directory on Linux
	ROOT_VAR_LOG=/var/log/SUNWut
	rm -f $ROOT_VAR_LOG/*
    fi
    return 0
    
}

function enable {

  
# Run utacleanup to initialize the symlinks.  Do this only if we're not
# doing an altroot install.  In the altroot case it'll all get taken care
# of when the client gets booted.
#
    
    /etc/init.d/utacleanup
    
    
# Clean up /var/opt/
#

    
    for dir in /var/opt/SUNWut/log; do
	if [ -d "$dir" ]; then
	    find $dir -type f -exec rm {} \;
	fi
    done
    
    typeset OS=`uname -s`
    if [ $OS = "Linux" ]; then 
#
# For Linux only.  We need to create the links to init.d files using
# chkconfig.  This is something else we can't do in an altroot install,
# but luckily Linux doesn't support altroot installs anyway.
#
	typeset INITFILES="utsyscfg utacleanup utsvc"
	for F in $INITFILES
	  do
	  chkconfig --add $F > /dev/null
	done
#
# On SuSE, 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
	    typeset ETCINIT=/etc/rc.d
	    typeset KILLFILES="K31utsyscfg K15utacleanup K02utsvc"
	    for F in $KILLFILES
	      do
	      INITF=`echo $F | sed -e "s/^K..//"`
	      ln -s ../$INITF $ETCINIT/rc4.d/$F
	      ln -s ../$INITF $ETCINIT/rc3.d/$F
	      ln -s ../$INITF $ETCINIT/rc2.d/$F
	      ln -s ../$INITF $ETCINIT/rc1.d/$F
	    done
	fi
    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=""


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