#!/bin/sh
#
#       This script will remove, as necessary, the changes introduced by the
#       NCAfs, NCA Proxy and sendfilev project.
#

PATID=109234
NUNDO_STATUS=02
NUNDO_ACTIVE=03

INFO="$PKG_INSTALL_ROOT/var/sadm/pkg/SUNWncar/pkginfo"
pids=`egrep '^PATCHLIST' $INFO | nawk -F= '{print $2}'`

rmv_all=1

undo_ncakmod_active() {
	sed -e '/nca_active/d' $PKG_INSTALL_ROOT/etc/nca/ncakmod.conf > \
		/tmp/ncakmodconf.$$
	cp /tmp/ncakmodconf.$$ $PKG_INSTALL_ROOT/etc/nca/ncakmod.conf
	rm /tmp/ncakmodconf.$$
}

undo_ncakmod_status() {
	sed -e '/ncad_status/d' $PKG_INSTALL_ROOT/etc/nca/ncakmod.conf > \
		/tmp/ncakmodconf.$$
	cp /tmp/ncakmodconf.$$ $PKG_INSTALL_ROOT/etc/nca/ncakmod.conf
	rm /tmp/ncakmodconf.$$
}

for i in `echo $pids`; do
	echo $i | egrep -s $PATID || continue
	rev=`expr //$i : '.*-\(.*\)'`
	#No edits for rev-03 or higher
	[ $rev -ge $NUNDO_ACTIVE ] && exit
	#Remove nca_active for -03 or higher
	[ $rev -ge $NUNDO_STATUS ] && no_nca_active=1 && continue
	#Remove all changes if highest rev is less than -02
	[ $rev -le 01 ] ] && rmv_all=1 && continue
done

[ "$no_nca_active" ] && undo_ncakmod_active && exit
[ "$rmv_all" ] && undo_ncakmod_active && undo_ncakmod_status && exit

exit 0
