#! /bin/sh

# $Id: nsr_install_tun.sh,v 1.4.20.2 2012/09/21 17:30:35 tompkb1 Exp $ Copyright 2011-2012 EMC Corporation.
#
# Copyright (c) 2011-2012 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb; export PATH
PROG=`/usr/bin/basename $0`
NWARCH=linux86

DRV_DIR=/usr/kernel/drv
NSR_DRV_DIR=/opt/nsr/drv

case $NWARCH in
	solarissparcv9) PLATFORM=sparcv9;;
	solarisx64) PLATFORM=amd64;;
	*) PLATFORM=;;
esac

usage()
{ 
	echo "To install the Universal Tun driver:"
	echo "	$PROG -i"
	echo "To uninstall the Universal Tun driver:"
	echo "	$PROG -u"
}

install_tun()
{
	if [ `uname` = "Linux" ]; then

		echo -n "Installing Universal TUN/TAP driver... "

		/sbin/modprobe tun

	elif [ `uname` = "SunOS" ]; then

		echo "Installing Universal TUN/TAP driver... \c"

		/usr/sbin/install -f $DRV_DIR -m 644 -g sys \
			-s $NSR_DRV_DIR/tun.conf
		/usr/sbin/install -f $DRV_DIR/$PLATFORM -m 755 -g sys \
			-s $NSR_DRV_DIR/$PLATFORM/tun

		/usr/sbin/rem_drv tun > /dev/null 2>&1
		/usr/sbin/add_drv tun
	fi

	return $?
}

uninstall_tun()
{
	if [ `uname` = "Linux" ]; then

		echo -n "Uninstalling Universal TUN/TAP driver... "

		/sbin/modprobe -r tun

		RET=$?

	elif [ `uname` = "SunOS" ]; then

		echo "Uninstalling Universal TUN/TAP driver... \c"

		/usr/sbin/rem_drv tun

		RET=$?

		/bin/rm /dev/tun > /dev/null 2>&1

		/bin/rm -f $DRV_DIR/tun.conf
		/bin/rm -f $DRV_DIR/$PLATFORM/tun
	fi

	return $RET
}

if [ `whoami` != "root" ]; then
	echo "You must run as root to install the Universal Tun driver"
	exit 1
fi

ACTION=ADD

while getopts "iu" ARG; do
	case $ARG in
		i)	ACTION=ADD;;
		u)	ACTION=REMOVE;;
		*)	usage; exit 1;;
	esac
done

if [ $ACTION = "ADD" ]; then

	install_tun

	if [ $? -eq 0 ]; then

		echo "success"
	else
		exit 1
	fi

else
	uninstall_tun

	if [ $? -eq 0 ]; then

		echo "success"
	else
		exit 1
	fi
fi

exit 0
