#!/bin/sh
# @(#)autostart	1.6 01/06/06 11:43:57
#
# Copyright (C) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
#
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
#
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043

# Enable and disable the VMSA server auto-start

usage()
{
	echo "Usage:  autostart on | off  [ <VMSAHOME_DIR> ]"
}

hangup_inetd()
{
	pid=`ps -e | grep inetd | grep -v grep | awk '{print $1}'`
	if [ $? -eq 0 ]
	then
		echo "HANGUP inetd"
		kill -HUP $pid
	fi
}


stopserver()
{
	$VMSAHOME/bin/vmsa_server -k > /dev/null 2>&1
}

check_nisplus()
{
	if [ -f /etc/nsswitch.conf ]
	then
		grep services /etc/nsswitch.conf | grep NOTFOUND >/dev/null 2>&1
		if [ $? -eq 0 ]
		then
		        echo "\
WARNING:
	services database entry in file /etc/nsswitch.conf
	may need the files source field before NOTFOUND=return.
	inetd must then be terminated and restarted."
		fi
	fi
}


update_file()
{
	FILE=$1

	OLDFILE=$FILE
	NEWFILE=$FILE.NEW1

	START=`egrep -n vmsa_START $OLDFILE | awk -F: '{print $1}'`
	END=`egrep -n vmsa_END $OLDFILE | awk -F: '{print $1}'`
	cat $OLDFILE | sed -e "$START,$END d" > $NEWFILE 2>/dev/null
	if [ $? -ne 0 ]
	then
	        echo "\
Error removing server auto-startup lines from:
	${OLDFILE}
Please remove the lines manually."
	else
		echo "Removing changes to $OLDFILE..."
        	cp $NEWFILE $OLDFILE 2>/dev/null
	        if [ $? -ne 0 ]
        	then
                echo "\
File could not be updated:
	${OLDFILE}
Please check the file for correctness."
	        fi
	fi
	rm -f $NEWFILE
}


update_boot_script()
{
	OS=`uname -s`
	case $OS in
		"SunOS")
			SCRIPT=../init.d/vmsa-server
			LINK=S96vmsa-server
			cd /etc/rc2.d
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi
			if [ -f $SCRIPT ]
			then
				return
			fi
			echo "Adding boot-time server start up script..."
			cat > $SCRIPT <<EOF

#!/bin/sh
#
# Copyright(C) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# start the Volume Manager Storage Administrator server
#
case \$1 in
'start'|'')
	$VMSAHOME/bin/vmsa_server &
	;;
'stop')
	$VMSAHOME/bin/vmsa_server -k &
	;;
*)
	echo "Usage: \$0 { start | stop }"
	exit 1
	;;
esac
exit 0
EOF
			chmod 744 $SCRIPT;;

		"HP-UX")
			SCRIPT=/sbin/init.d/vmsa_start_srvr
			LINK=/sbin/rc3.d/S950vmsa_start_srvr
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi
			if [ -f $SCRIPT ]
			then
				return
			fi
			echo "Adding boot-time server start up script..."
			cat > $SCRIPT <<EOF

#!/bin/ksh
#
# Copyright(C) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
PATH=/usr/sbin:/usr/bin:/sbin; export PATH

mode=\$1
case "\$mode" in
    'start_msg')
        echo "Start VMSA Server" 
        exit 0
        ;;
 
    'stop_msg')
        echo "Stop VMSA Server"
        exit 0
        ;;
esac
/bin/nohup $VMSAHOME/bin/vmsa_server >/dev/null 2>&1 &
exit 0
EOF
			chmod 744 $SCRIPT;;

		"Linux")
			SCRIPT=../init.d/vmsa_start_srvr
			LINK=S950vmsa_start_srvr

			cd /etc/rc.d/rc3.d
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi

			cd /etc/rc.d/rc4.d
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi

			cd /etc/rc.d/rc5.d
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi

			cd /etc/rc.d/rc6.d
			if [ ! -f $LINK ]
			then
				ln -s $SCRIPT $LINK
			fi

			if [ -f $SCRIPT ]
			then
				return
			fi

			echo "Adding boot-time server start up script..."
			cat > $SCRIPT <<EOF
#!/bin/sh
# @(#)autostart	1.6 01/06/06 11:43:57
#
# Copyright(C) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# start the Volume Manager Storage Administrator server

/usr/bin/nohup $VMSAHOME/bin/vmsa_server >/dev/null 2>&1 &

exit 0
EOF
			chmod 744 $SCRIPT;;

		"OSF1")
			SCRIPT=/sbin/init.d/vmsa_start_srvr
			if [ -f $SCRIPT ]
			then
				return
			fi

			chmod 744 $SCRIPT;;
	esac

}


job=$1
vhome=$2
PKGNAME=VRTSvmsa
VMSAHOME=/opt/$PKGNAME
OS=`uname -s`
SHEL=/bin/sh

if [ $# -eq 0 -o $# -gt 2 ]
then
	usage
	exit 1
fi

_UID=`id | cut -c5- | sed 's/(.*//'`
if [ $_UID != "0" ]; then echo "Permission denied"; exit 1; fi


if [ $vhome ]
then
	VMSAHOME=$vhome
fi

if [ ! -d $VMSAHOME ]
then
	echo "$VMSAHOME directory does not exit."
	usage
	exit 1
fi


if [ $job = "on" ]
then
####update /etc/services
	OLDFILE=/etc/services
	NEWFILE=/etc/NEW1.services

	if [ ! -f ${OLDFILE} -a ! -h ${OLDFILE} ]; then
		echo "File ${OLDFILE} does not exist.  Exiting..."
		exit 1
	fi

	egrep -s "vmsa_START" $OLDFILE 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo "Adding server auto-start lines to $OLDFILE..."
		sed -e '$ a\
#vmsa_START (do not remove)\
vmsa		2793/tcp\
#vmsa_END (do not remove)'\
		< $OLDFILE > $NEWFILE 2>/dev/null
		cp $NEWFILE $OLDFILE 2>/dev/null
		rm -f $NEWFILE
	fi

####update /etc/inetd.conf
	LOGDIR=/var/opt/vmsa/logs
	mkdir -p $LOGDIR
	OLDFILE=/etc/inetd.conf
	NEWFILE=/etc/NEW1.inetd.conf

	if [ ! -f ${OLDFILE} -a ! -h ${OLDFILE} ]; then
		echo "File ${OLDFILE} does not exist.  Exiting..."
		exit 1
	fi

	egrep -s "vmsa_START" $OLDFILE 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo "Adding server auto-start lines to $OLDFILE..."
		sed -e '$ a\
#vmsa_START (do not remove)\
vmsa stream tcp nowait root '$VMSAHOME'/vmsa/server/vmsad vmsad '$LOGDIR'/server.log '$SHEL' '$VMSAHOME'/bin/vmsa_server -s\
#vmsa_END (do not remove)'\
		< $OLDFILE > $NEWFILE 2>/dev/null
		cp $NEWFILE $OLDFILE 2>/dev/null
		rm -f $NEWFILE
	fi


####update vmsa boot-time server start up script 
	echo "Removing boot-time server start up script..."
	rm -f /etc/init.d/vmsa-server			# Solaris script
	rm -f /etc/rc2.d/S96vmsa-server			# Solaris link

	rm -f /sbin/rc3.d/S950vmsa_start_srvr   	# HP-UX link
	rm -f /sbin/init.d/vmsa_start_srvr		# HP-UX script

	rm -f /etc/rc.d/rc3.d/S950vmsa_start_srvr	# Linux link
	rm -f /etc/rc.d/rc4.d/S950vmsa_start_srvr	# Linux link
	rm -f /etc/rc.d/rc5.d/S950vmsa_start_srvr	# Linux link
	rm -f /etc/rc.d/rc6.d/S950vmsa_start_srvr	# Linux link
	rm -f /etc/rc.d/init.d/vmsa_start_srvr	       	# Linux script

	rm -f /sbin/init.d/vmsa_start_srvr		# OSF1 script
	stopserver
	hangup_inetd
	check_nisplus
elif [ $job = "off" ]
then
	update_file /etc/services
	update_file /etc/inetd.conf
	update_boot_script
	stopserver
	hangup_inetd
else
	usage
fi
