#!/bin/sh
#
#	"@(#)rm_admin.sh 1.12 97/02/20"
#
# Copyright (c) 1996 by Sun Microsystems, Inc.
#
# Remove the AdminSuite 2.1 product from a file server
#

PATH="/usr/bin:/usr/sbin:/sbin"

CDROOT=`/usr/bin/pwd`
cd $CDROOT 2>/dev/null
[ -f `/usr/bin/basename $0` ] || {
        cd `/usr/bin/dirname $0` 2>/dev/null
        CDROOT=`/usr/bin/pwd`
}
ARCH=`/usr/bin/uname -p`
TEXTDOMAINDIR="${CDROOT}/Solaris/${ARCH}/locale"
TEXTDOMAIN="SUNW_ADMIN_INSTALL"

export CDROOT PATH TEXTDOMAINDIR TEXTDOMAIN

rm_dir()
{
	dir=$1
	if [ ! -d $dir ]; then
		return
	fi

	count=`ls -a $dir/etc | wc -l`
	if [ $count -eq 2 ]; then
		rmdir $dir/etc
	fi

	count=`ls -a $dir | wc -l`

	if [ $count -eq 2 ]; then
		rm -rf $dir
	fi
}

cleanup_spool() {
	for i in ${TEMPLATES}/*PATCH*/*/pkginfo
	do
		if [ ! -f $i ]; then
			continue
		fi
		PKG=`egrep "^PKG=" $i | cut -d= -f 2`
		VERSION=`egrep "^VERSION=" $i | sed -e 's/VERSION=//'`
		ARCH=`egrep "^ARCH=" $i | cut -d= -f 2`
		suffix=`echo ${ARCH} | sed -e 's/.*\.//'`
		if [ "$ARCH" = "$suffix" ]; then
			ARCH=${ARCH}.all
		fi
		TEMPLATEDIR=${TEMPLATES}/${PKG}_${VERSION}_${ARCH}
		grep -v "^SPOOLED_ROOT=${ARCH}:${TEMPLATEDIR}\$" ${SOFTINFO} \
		  | grep -v "^ROOT=${ARCH}:${PKG},[0-9]*,${VERSION}\$" \
		  > /tmp/ACBsoftinfo.$$
		rm -rf ${TEMPLATEDIR}
		mv /tmp/ACBsoftinfo.$$ ${SOFTINFO}
	done
}

usage()
{
gettext "./rm_admin [-a] [-f] [-v version] [-d directory]\n\
    -a - remove previous versions of AutoClient product (1.0, 1.0.1 or 2.0)\n\
    -f - force; do not prompt for confirmation\n\
    -v - specify the version of AdminSuite to remove (2.1, 2.2 or 2.3 )\n\
    -d - specify installed directory of AdminSuite (ex. -d /opt)\n\
Defaults to removing AdminSuite 2.1 from /export/opt.\n\
This command must be run as root.\n"
    exit 1
}

####
# main

REM_AC=0
FORCE=0
DIR=/export/opt
as_list="SUNWsadma SUNWsadmo SUNWspapp"
as2_list="SUNWsadma SUNWsadmo SUNWspapp SUNWsaco SUNWsacu SUNWsadmc SUNWsacd SUNWsacb SUNWsadmb SUNWadmsm SUNWsacsr SUNWsacsu SUNWsadmm"
ac_list="SUNWsaco SUNWsacu SUNWacmgr"
list=$as_list

VER=2.1

FIX_REG=0

PROGNAME=`basename $0`

while getopts afd:v: c
do
	case $c in
	a) REM_AC=1		# remove AC 1.0 & 1.0.1
	   list=$ac_list
	   PVERS="PRODVERS=1.0|PRODVERS=1.0.1"
	   DIR=/opt
	   VER=1.x
	   ;;
	f) FORCE=1		# don't prompt for confirmation
	   ;;
	v) VER=$OPTARG		# specify version
	   ;;
	d) DIR=$OPTARG		# specify directory location
	   ;;
	\?) usage
	   ;;
	esac
done
shift `expr $OPTIND - 1`

[ $# -ne 0 ] && usage

if [ $REM_AC -eq 0 ]; then
	case $VER in
	"2.1")
		PVERS="PRODVERS=AdminSuite 2.1"
		CONFIRM=`gettext 'Removing AdminSuite 2.1. OK? [y|n] '`
		;;
	"2.2")
		PVERS="PRODVERS=AdminSuite 2.2|PRODVERS=1.1"
		list=$as2_list
		CONFIRM=`gettext 'Removing AdminSuite 2.2. OK? [y|n] '`
		;;
	"2.3")
		PVERS="PRODVERS=AdminSuite 2.2|PRODVERS=1.1|PRODVERS=AdminSuite 2.3"
		list=`echo $as2_list SUNWsadmp`
		CONFIRM=`gettext 'Removing AdminSuite 2.3. OK? [y|n] '`
		;;
	*)
		gettext 'Error: Invalid Version.'; echo
		exit 1
		;;
	esac
	# check if the product is installed
	if [ ! -d $DIR/sparc/SUNWadm/$VER -a ! -d $DIR/i386/SUNWadm/$VER -a \
	     ! -d $DIR/ppc/SUNWadm/$VER -a ! -d $DIR/SUNWadm/$VER ]; then
		printf \
		"`gettext 'Error: AdminSuite %s is not installed in %s.'`" \
		$VER $DIR
		echo
		usage
    	fi
else
    if [ ! -d /opt/SUNWsac -a ! -d /var/sadm/pkg/SUNWacmgr ]; then
	gettext 'Error: AutoClient 1.x is not installed.'; echo
	usage
    fi
    CONFIRM=`gettext 'Removing AutoClient 1.x. OK? [y|n] '`
fi

# verify that we are root
WHOAMI=`id`
WHOAMI=`expr "$WHOAMI" : 'uid=\([0-9][0-9]*\)(.*'`
[ $WHOAMI != "0" ] && usage

# prompt for confirmation before doing anything
if [ $FORCE -ne 1 ]; then
	echo "${CONFIRM}\c"
	read RESP
	if [ $RESP = "y" -o $RESP = "Y" ]; then
		:
	else
		exit 0
	fi
fi

# if we're removing AC and AC 1.0 was installed, remove AC patches from root
# template
if [ $REM_AC -eq 1 -a -d /var/sadm/pkg/SUNWacmgr ]; then
	INST_RELEASE=/var/sadm/softinfo/INST_RELEASE
	TMP_OS=`sed -n 's/^OS=//p' ${INST_RELEASE}`
	TMP_VERS=`sed -n 's/^VERSION=//p' ${INST_RELEASE}`
	TMP_OSVERS=${TMP_OS}_${TMP_VERS}
	TEMPLATES=/export/root/templates/${TMP_OSVERS}
	SOFTINFO=/var/sadm/softinfo/${TMP_OSVERS}

	if [ "$TMP_VERS" = "2.4" ]; then
		cleanup_spool
	fi
fi

if [ $REM_AC -eq 0 -a $VER = "2.1" ]; then
	# if the 2.2 or 2.3 apps are already registered in the launcher, save
	# the registery and restore it after the pkgrm, since removing the 2.1
	# pkgs will remove the 2.2/2.3 apps from the launcher
	egrep -s "\"Host Manager\" /opt/SUNWadm/2.[23]" \
		/opt/SUNWadm/etc/.solstice_registry 2>/dev/null
	if [ $? -eq 0 ]; then
		cp /opt/SUNWadm/etc/.solstice_registry /tmp/reg.$$
		FIX_REG=1
	fi
fi

if [ $REM_AC -eq 0 -a $VER = "2.2" ]; then
	# if the 2.3 apps are already registered in the launcher, save
	# the registery and restore it after the pkgrm, since removing the 2.2
	# pkgs will remove the 2.3 apps from the launcher
	egrep -s "\"Host Manager\" /opt/SUNWadm/2.3" \
		/opt/SUNWadm/etc/.solstice_registry 2>/dev/null
	if [ $? -eq 0 ]; then
		cp /opt/SUNWadm/etc/.solstice_registry /tmp/reg.$$
		FIX_REG=1
	fi
fi

cat >/tmp/admin.$$ << DONE
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=quit
setuid=nocheck
conflict=nocheck
action=nocheck
DONE

for i in $list
do
	for j in /var/sadm/pkg/${i}*
	do
		if [ ! -d $j ]; then
			continue
		fi
		egrep -s "$PVERS" $j/pkginfo
		if [ $? -eq 0 ]; then
			pkg=`basename $j`
			gettext "removing: "
			echo $pkg
			pkgrm -n -a /tmp/admin.$$ $pkg
		fi
	done
done

# SUNWsadmc needs special handling since it is shipped in AS 2.1 & AC 1.0.1
# if AC 1.0.1 or AS 2.1 is still installed, don't remove SUNWsadmc (vers 2.1)
# look to see if either of the app level pkgs for that release is installed
rm_sadmc=1
for i in /var/sadm/pkg/SUNWsaco*/pkginfo
do
	if [ ! -f $i ]; then
		continue
	fi
	egrep -s "PRODVERS=1.0.1" $i
	if [ $? -eq 0 ]; then
		rm_sadmc=0
		break
	fi
done
for i in /var/sadm/pkg/SUNWsadma*/pkginfo
do
	if [ ! -f $i ]; then
		continue
	fi
	egrep -s "PRODVERS=AdminSuite 2.1" $i
	if [ $? -eq 0 ]; then
		rm_sadmc=0
		break
	fi
done

if [ $rm_sadmc -eq 1 ]; then
	for i in /var/sadm/pkg/SUNWsadmc*
	do
		if [ ! -d $i ]; then
			continue
		fi
		egrep -s "PRODVERS=AdminSuite 2.1" $i/pkginfo
		if [ $? -eq 0 ]; then
			pkg=`basename $i`
			gettext "removing: "
			echo $pkg
			pkgrm -n -a /tmp/admin.$$ $pkg
		fi
	done
fi

rm /tmp/admin.$$

if [ "$DIR" = "/opt" ]; then
	rm_dir /opt/SUNWadm
	if [ $REM_AC -eq 0 -a -d /opt/SUNWadmd ]; then
		rm -rf /opt/SUNWadmd
	fi
else
	rm_dir $DIR/sparc/SUNWadm
	rm_dir $DIR/i386/SUNWadm
	rm_dir $DIR/ppc/SUNWadm
	if [ -s /opt/SUNWadm ]; then
		count=`ls -aL /opt/SUNWadm | wc -l`
		if [ $count -eq 2 ]; then
			rm /opt/SUNWadm
		fi
	fi
	if [ $REM_AC -eq 0 -a -d $DIR/all/SUNWadmd ]; then
		rm -rf $DIR/all/SUNWadmd
		if [ -s /opt/SUNWadmd ]; then
			rm /opt/SUNWadmd
		fi
	fi
fi

if [ $FIX_REG -eq 1 ]; then
	cp /tmp/reg.$$ /opt/SUNWadm/etc/.solstice_registry
	rm /tmp/reg.$$
fi

exit 0
