#! /bin/sh
##########################################################################
#           Copyright (c) 2001, Cisco Systems, All Rights Reserved
###########################################################################
#
#  File:    vpn_uninstall
#  Date:    06/15/2001
#
###########################################################################
#
# linux vpn client installation script.
#
###########################################################################

#
# Initialization
#
if [ -x /usr/bin/id ]; then
    ID="/usr/bin/id"
elif [ -x /bin/id ]; then
    ID="/bin/id"
else
    echo "Unable to determine access level"
    exit 1
fi


WHOAMI=`$ID|sed -e 's/(.*//'`
if [ "$WHOAMI" != "uid=0" ] ; then
	echo "Sorry, you need super user access to run this script."
	exit 1
fi

#
#  Print abort message on interupt.
#
trap 'echo; echo "Oh no! UnInstall Aborted!"; exit 1' 1 2 3 15

#
# Assign a default value if we don't have it
#
if [ -z "$VPNBINDIR" ]; then
	VPNBINDIR=/usr/local/bin
fi

if [ -d /lib/modules/preferred ]; then
	MODULEDIR=/lib/modules/preferred/CiscoVPN
else
	MODULEDIR=/lib/modules/`uname -r`/CiscoVPN
fi

VPNDIR="/etc/CiscoSystemsVPNClient"


#
# Check for existence and validity of chkconfig
#
CHKCONFIG=""
if [ -x /sbin/chkconfig ]; then
    CHKCONFIG=/sbin/chkconfig
elif [ -x /usr/sbin/chkconfig ]; then
    CHKCONFIG=/usr/sbin/chkconfig
elif [ -x /usr/local/sbin/chkconfig ]; then
    CHKCONFIG=/usr/local/sbin/chkconfig
fi

WC=""
if [ -x /bin/wc ]; then
    WC=/bin/wc
elif [ -x /usr/bin/wc ]; then
    WC=/usr/bin/wc
elif [ -x /usr/local/bin/wc ]; then
    WC=/usr/local/bin/wc
fi

if [ -z "$WC" ]; then
    CHKCONFIG=""
fi

if [ -n "$CHKCONIG" ]; then
    if [ "0" = `"$CHKCONFIG" --list | "$WC" -l` ]; then
	CHKCONFIG=""
    fi
fi

#


if [ -d /etc/init.d ]; then
    INIT_D="/etc/init.d"
elif [ -d /etc/rc.d/init.d ]; then
    INIT_D="/etc/rc.d/init.d"
elif [ -d /etc/rc.d ]; then
    INIT_D="/etc/rc.d"
else
    INIT_D=""
fi

if [ -d /etc/rc3.d ]; then
    RC_D="/etc"
elif [ -d /etc/rc.d/rc3.d ]; then
    RC_D="/etc/rc.d"
else
    RC_D=""
fi

#
# UnInstall Start
#

echo "Cisco Systems VPN Client Version 3.7.2 (Rel) Linux UnInstaller"
echo "Copyright (C) 1998-2001 Cisco Systems, Inc. All Rights Reserved."



#
# Set up to do reads with possible shell escape and default assignment
# myread()
#

myread() {
	case "$fastread" in
		yes) ans=''; echo " " ;;
		*) ans='!';;
	esac

	while expr "X$ans" : "X!" >/dev/null; do
		read ans
		case "$ans" in
			!)
				sh
				echo " "
				echo $n "$rp $c"
				;;
			!*)
				set `expr "X$ans" : "X!\(.*\)$"`
				sh -c "$*"
				echo " "
				echo $n "$rp $c"
				;;
			esac
	done

	rp='Your answer:'

	case "$ans" in
		'') ans="$dflt";;
	esac
}


#
# Confirm that user is sure that he/she wishes to uninstall the Client
#

got_confirm=false

while [ "$got_confirm" = "false" ]; do
	dflt=no
	rp="Are you sure that you wish to uninstall the VPN Client?  [$dflt]"
	printf "\n%s" "$rp "
	myread

	the_uninstallconfirm=$ans

	case "$the_uninstallconfirm" in
	y* | Y*)
		got_confirm=true
		;;
	n* | N* )
	 	echo "Uninstall Aborted"
		exit 1
		;;
	*)
		;;
	esac
done

got_confirm=false

while [ "$got_confirm" = "false" ]; do
	dflt=no
	rp="Do you wish to remove all existing profiles and certificates?  [$dflt]"
	printf "\n%s" "$rp "
	myread

	clean_profiles=$ans

	case "$clean_profiles" in
	y* | Y*)
		got_confirm=true
		clean_profiles=true
		echo " - Existing Profiles and Certificates will be removed"
		;;
	n* | N* )
		got_confirm=true
		clean_profiles=false
		echo " - Existing Profiles and Certificates will be preserved"
		;;
	*)
		;;
	esac
done


if [ ! -f "$VPNBINDIR/vpnclient" ]; then
	dflt=$VPNBINDIR
	rp="Directory where binaries were installed [$dflt]"
	printf "\n%s" "$rp "
	myread
	VPNBINDIR=$ans
	if [ ! -d "$VPNBINDIR" ]; then
		echo " - Binary uninstall directory does not exist ${VPNBINDIR}"
			echo " - Please determine the correct directory and re-run"
			exit 1
	fi
fi

echo " - Binary uninstall directory set to ${VPNBINDIR}"

if [ -z "$INIT_D" ]; then
    rp="Directory containing init scripts:"
    printf "\n%s" "$rp "
    myread
    INIT_D=$ans
    if [ ! -d "$INIT_D" ]; then
        echo "Directory \"$INIT_D\" doesn't exist"
        echo " - Please determine the correct directory and re-run"
        exit 1
    fi
fi
if [ -z "$CHKCONFIG" -a -z "$RC_D" ]; then
    rp="Directory containing runlevel directories (rcX.d):"
    printf "\n%s" "$rp "
    myread
    RC_D=$ans
    if [ ! -d "$RC_D" ]; then
        echo "Directory \"$RC_D\" doesn't exist"
        echo " - Please determine the correct directory and re-run"
        exit 1
	fi
fi

printf "\n"


#
# Check for files and directories and clean up
#
echo "Cleaning up installed files and directory...."

if [ -f "$INIT_D/vpnclient_init" ]; then
	sh $INIT_D/vpnclient_init stop
	echo "Stopped: $INIT_D/vpnclient_init (VPN init script)"
fi

if [ -f "$VPNBINDIR/vpnclient" ]; then
	rm -f "$VPNBINDIR/vpnclient"
	echo "Removed: ${VPNBINDIR}/vpnclient (VPN dialer)"
fi

if [ -f "$VPNBINDIR/ipseclog" ]; then
	rm -f "$VPNBINDIR/ipseclog"
	echo "Removed: ${VPNBINDIR}/ipseclog (VPN log viewer)"
fi

if [ -f "$VPNBINDIR/cvpnd" ]; then
	rm -f "$VPNBINDIR/cvpnd"
	echo "Removed: ${VPNBINDIR}/cvpnd (VPN daemon)"
fi

if [ -f "$VPNBINDIR/cisco_cert_mgr" ]; then
	rm -f "$VPNBINDIR/cisco_cert_mgr"
	echo "Removed: ${VPNBINDIR}/cisco_cert_mgr (VPN certificate manager)"
fi

if [ -d "$MODULEDIR" ]; then 
	if [ "$MODULEDIR" != "/" ]; then
		rm -rf "$MODULEDIR"
		echo "Removed: ${MODULEDIR} (modules directory)"
	fi
fi

if [ -n "$CHKCONFIG" ]; then
    $CHKCONFIG --del vpnclient_init
else
    if [ -L "$RC_D/rc3.d/S85vpnclient_init" ]; then
	rm $RC_D/rc3.d/S85vpnclient_init
	echo "Removed: $RC_D/rc3.d/S85vpnclient_init (VPN rc3.d link)"
    fi

    if [ -L "$RC_D/rc4.d/S85vpnclient_init" ]; then
	rm $RC_D/rc4.d/S85vpnclient_init
	echo "Removed: $RC_D/rc4.d/S85vpnclient_init (VPN rc4.d link)"
    fi

    if [ -L "$RC_D/rc5.d/S85vpnclient_init" ]; then
	rm $RC_D/rc5.d/S85vpnclient_init
	echo "Removed: $RC_D/rc5.d/S85vpnclient_init (VPN rc5.d link)"
    fi
fi

if [ -f "$INIT_D/vpnclient_init" ]; then
	rm -f $INIT_D/vpnclient_init
	echo "Removed: $INIT_D/vpnclient_init (VPN init script)"
fi

if [ -f "$INIT_D/vpnclient_init.old" ]; then
	rm -f $INIT_D/vpnclient_init.old
	echo "Removed: $INIT_D/vpnclient_init.old (VPN init script - backup)"
fi

if [ "$clean_profiles" = "true" ]; then
 	if [ -d $VPNDIR ]; then 
		if [ $VPNDIR != "/" ]; then
			rm -rf ${VPNDIR}
			echo "Removed: $VPNDIR (profiles, certificates, INI)"
		fi
	fi
else
	echo "Preserved: $VPNDIR (profiles, certificates, INI)"
fi

echo "Done."
#
# All is well
#
exit 0
