#!/bin/sh

#
# Script to shutdown all the systems
#

# GRACE, minutes
GRACE=5
ABORT=false
MESSAGE=""

VERSION="0.0.4a, SPDsoft.  Thu Mar 29 12:27:12 CEST 2007"
DIST=http://webdiis.unizar.es/pub/unix/util/sd
BIN=/root/bin
DO=

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/etc/:/etc:/usr/bsd
if [ -x /opt/sfw/bin/lynx ]
then
	PATH=$PATH:/opt/sfw/bin
fi
unset LD_LIBRARY_PATH
unset LD_LIBRARYN32_PATH
unset DYLD_LIBRARY_PATH
unset LYNX_TEMP_SPACE

if [ -x /usr/bin/pgrep ]
then
	PGREP="/usr/bin/pgrep"
else
	PGREP=f_pgrep

	f_pgrep()
	{
		/usr/bin/ps -e | /usr/bin/grep "[ ]*"$1 | \
		/usr/bin/sed -e 's/^  *//' -e 's/ .*//'
	}
fi

killproc()
{
	_procname=$1
	_signal=$2
	${PGREP} ${_procname} | head -1 | xargs -t -I {} $DO kill -${_signal} {}
}



usage()
{
cat <<EOF
sd -VIDhngm
    -V: version
    -I: internet version
    -D: download and install update
    -h: help
    -n: do nothing
    -g: grace minutes (default 5)
    -a: abort shutdown
    -m: message (must be the last option)
EOF
}

set -- `getopt VIDhang:m: "$@"`

if test $? != 0
then
	usage
	exit 1
fi

for i in "$@"
do
	case $i in
	-n) DO=echo; shift;;
	-a) ABORT=:; shift;;
	-g) GRACE=$2; shift; shift;;
	-m) shift; MESSAGE="$*"; shift;;
	-V) echo "$VERSION"; exit 0;;
	-I)
		PATH=$PATH:/usr/local/bin:/sw/bin; export PATH
		LD_LIBRARY_PATH=/usr/local/lib; export LD_LIBRARY_PATH
		LD_LIBRARYN32_PATH=/usr/local/lib32; export LD_LIBRARYN32_PATH
		DV=`lynx -source $DIST | awk -F'"' '/^VERSION/{print $2; exit;}'`
		if [ "_$DV" != "_$VERSION" ]
		then
			echo "#### Warning: Server version is  \"$DV\""
			echo "####          program version is \"$VERSION\""
			exit 1
		fi
		exit 0
		;;
	-D)
		PATH=$PATH:/usr/local/bin:/sw/bin; export PATH
		LD_LIBRARY_PATH=/usr/local/lib; export LD_LIBRARY_PATH
		LD_LIBRARYN32_PATH=/usr/local/lib32; export LD_LIBRARYN32_PATH
		lynx -source $DIST > /tmp/sd.$$ ||\
		exit 1 && \
		mv /tmp/sd.$$ $BIN/sd
		chmod 755 $BIN/sd
		chown root $BIN/sd
		chgrp 0 $BIN/sd
		exit 0
		;;

	-h)
		usage
		exit 0
		;;
	esac
done

GRACES=`expr $GRACE '*' 60`
MESSAGE=`echo "$MESSAGE" | sed -e 's/[ -]*$//'`

case `uname` in
	OSF1)
		ARCH=osf-alpha
		f_pgrep()
		{
			/usr/bin/ps -e | /usr/bin/grep "[ ]*"$1 | grep -v grep | \
			/usr/bin/sed -e 's/^  *//' -e 's/ .*//'
		}

		;;

	SunOS)
		ARCH=sunos-`arch`
		;;

	Linux)
		ARCH=linux-`arch`
		;;

	IRIX*)
		ARCH=irix-mips
		;;

	Darwin*)
		ARCH=darwin-`arch`
		;;

	HP-UX)
		if /bin/hp-mc680x0
		then
			ARCH=hpux-68k
		else
			ARCH=hpux-hppa
		fi
		;;

	CYGWIN_NT*)
		ARCH=cygwin-i386
		;;

	FreeBSD)
		ARCH=FreeBSD-i386
		;;

	*)
		echo Warning: unsupported system
		;;
esac

$DO cd /

if $ABORT
then
	case $ARCH in

		sunos-sun4|sunos-i86pc)
			killproc shutdown KILL
			$DO rm -f /etc/nologin
			;;
		
		osf-alpha)
			killproc shutdown TERM
			;;

		linux-*)
			$DO killall -v -e shutdown
			;;

		irix-mips|darwin-ppc)
			$DO killall -v shutdown
			;;

		hpux-*)
			;;

		cygwin-*)
			;;

		FreeBSD-i386)
			;;

		*)
			;;
	esac

else
	if [ -x /root/bin/pre-sd ]
	then
		/root/bin/pre-sd
	fi
	case $ARCH in

		sunos-sun4|sunos-i86pc)
			if test -f /usr/sbin/shutdown
			then
				$DO exec /usr/sbin/shutdown -y -g$GRACES -i5 "$MESSAGE"
			else
				$DO exec /usr/etc/shutdown -h +$GRACE "$MESSAGE"
			fi
			;;
		
		osf-alpha|darwin-ppc|linux-*)
			$DO exec /sbin/shutdown -h +$GRACE "$MESSAGE"
			;;

		irix-mips)
			$DO exec /etc/shutdown -y -g$GRACES -i0 -p
			;;

		hpux-*)
			;;

		cygwin-*)
			$DO exec /usr/bin/shutdown -s +$GRACE "$MESSAGE"
			;;

		FreeBSD-i386)
			;;

		*)
			;;
	esac

fi