#!/bin/sh
#
#       Equinox SST Loadable Module installation script
#
#       Copyright (C) 1989-2002 Equinox Systems Inc.     

#
#	install / remove eqnx startup script from appropriate
#	boot script mechanism.  Note that this varies based on linux
#	distribution
#
#	-i	: install eqnx startup script (default)
#	-v 	: verbose option, additional inforamation is displayed
#	-u 	: un-install eqnx startup script

#
#
# do_install 
#	install eqnx startup script
#
do_install() {

	PATH=$PATH:/sbin
	which chkconfig > /dev/null 2>&1
	CHK=$?
	chkconfig > /dev/null 2>&1
	RES=$? 
	if [ $CHK -eq 0 -a $RES -ne 0 ] 
	then	
		if [ ${VERBOSE} -eq 1 ]
		then
			echo "     boot installation method = chkconfig"
		fi
		chkconfig --add eqnx
	elif [ -d /etc/rc.d/rc2.d ] 
	then
		if [ ${VERBOSE} -eq 1 ]
		then
			echo "     boot installation method = /etc/rc.d/rcN.d scripts"
		fi
		ln -s /etc/rc.d/init.d/eqnx /etc/rc.d/rc2.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc.d/rc3.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc.d/rc4.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc.d/rc5.d/S90eqnx > /dev/null 2>&1
	elif [ -d /etc/rc2.d ]
	then
		if [ ${VERBOSE} -eq 1 ]
		then
			echo "     boot installation method = /etc/rcN.d scripts"
		fi
		ln -s /etc/rc.d/init.d/eqnx /etc/rc2.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc3.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc4.d/S90eqnx > /dev/null 2>&1
		ln -s /etc/rc.d/init.d/eqnx /etc/rc5.d/S90eqnx > /dev/null 2>&1
	elif [ -f /etc/rc.d/rc.local ]
	then
		if [ ${VERBOSE} -eq 1 ]
		then
			echo "     boot installation method = /etc/rc.d/rc.local script"
		fi
		echo "                                       # Equinox eqnx" >> /etc/rc.d/rc.local
		echo "if [ -e /etc/rc.d/init.d/eqnx ] ; then # Equinox eqnx" >> /etc/rc.d/rc.local
		echo "   /etc/rc.d/init.d/eqnx start         # Equinox eqnx" >> /etc/rc.d/rc.local
		echo "fi                                     # Equinox eqnx" >> /etc/rc.d/rc.local
	else
		echo "Cannot determine how drivers are invoked at boot time."
		echo "Please perform the necessary process to ensure that"
		echo "/etc/rc.d/init.d/eqnx is invoked at boot time."
	fi
}

#
# do_remove
#	remove eqnx startup script
#
do_remove() {

	PATH=$PATH:/sbin
	which chkconfig > /dev/null 2>&1
	CHK=$?
	chkconfig > /dev/null 2>&1
	RES=$? 
	if [ $CHK -eq 0 -a $RES -ne 0 ] 
	then	
		chkconfig --del eqnx
	elif [ -d /etc/rc.d/rc2.d ] 
	then
		rm -f /etc/rc.d/rc[2345].d/S90eqnx > /dev/null 2>&1
	elif [ -d /etc/rc2.d ]
	then
		rm -f /etc/rc[2345].d/S90eqnx > /dev/null 2>&1
	elif [ -f /etc/rc.d/rc.local ]
	then
		cp /etc/rc.d/rc.local /tmp/rc.local
		sed '/eqnx/ D' /tmp/rc.local > /etc/rc.d/rc.local
	fi
}

#######################################################################
#	main


#
# get the options
#
INSTALL=0;
REMOVE=0;
VERBOSE=0;

set -- `getopt iuv $*`

for i in $*
do
	case $i in
	-i) INSTALL=1;
	    shift ;;
	-u) REMOVE=1;
	    shift ;;
	-v) VERBOSE=1;
	    shift ;;
	--) shift ; break;;
	esac
done
	
if [ ${REMOVE} -eq 0 ]
then
	INSTALL=1
fi

if [ ${INSTALL} -eq 1 ] 
then
	do_install 
	exit 0
fi

if [ ${REMOVE} -eq 1 ] 
then
	do_remove 
	exit 0
fi

exit 0
