#! /bin/sh
#
#   Copyright 2009 Free Software Foundation, Inc.
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Insert system start for HP prior to 11

. ./Startdirs

sysname=`uname`
if  [ "$sysname" != 'HP-UX' ]
then
    cat <<EOF
Your system name is $sysname not HP-UX as expected

If your system is an HP one please would you tell us about this
problem installing you system startup routine so we can cater for this
type in future.

Better still can you patch it so it works and give us the fix!

Thank you
EOF

elif grep ' heart ' /etc/rc >/dev/null 2>&1;[ $? -ne 0 ]
then
    defaultstate=`sed -e '/:initdefault:/!d
s/^[^:]*:\([^:]*\):.*:/\1/' /etc/inittab`

    if [ -d /sbin/rc3.d ]
    then
	rcloc=/sbin
    elif [ -d /etc/rc.d ]
    then
	rcloc=/etc/rc.d
    fi

    if [ -z "$defaultstate" ] && [ -d $rcloc/rc3.d ]
    then
	echo "Assuming default startup state is 3...."
	defaultstate=3
    fi

    for loc in $rcloc/init.d /sbin/init.d /etc/rc.d/init.d /etc/init.d
    do
      if [ -d $loc ]
      then
	  initdir=$loc
	  break
      fi
    done

    if [ -z "$rcloc" ] || [ -z "$initdir" ] || [ -z "$defaultstate" ]
    then
	cat <<EOF
Sorry but your /etc/rc file is not like other ones on HP machines so
I dare not edit it.

We should be grateful if you let us see it so we can correctly edit
HP-UX /etc/rc files to permit automatic GNUbatch startup in future.
Better still can you suggest a fix!

Thank you.
EOF
    elif [ "$forceme" = "Y" ] || [ ! -f $initdir/gnubatch ]
    then
	rm -f $initdir/gnubatch $rcloc/rc?.d/*gnubatch
	Scriptfile=$initdir/gnubatch
	cat <<EOF >$Scriptfile
#
#	Startup of GNUbatch
#

case "\$1" in
start)
	# Start GNUbatch.
	# Delete previous memory-mapped stuff which might have got left behind

	rm -f $BATCHDIR/spmm*	

	# This allocates space for amaximum of 200 jobs and 500 variables
	# but please adjust as needed

	$USERPATH/gbch-start -j 200 -v 500
	echo GNUbatch Started
	;;
stop)
	$USERPATH/gspl-quit -y
	echo GNUbatch stopped
	;;
*)
	echo "Usage: \$0 { start | stop }"
        exit 1
	;;
esac
EOF
	chmod 744 $Scriptfile
	ln $Scriptfile $rcloc/rc0.d/K01gnubatch
	ln $Scriptfile $rcloc/rc$defaultstate.d/S80gnubatch
	ln $Scriptfile $rcloc/rc$defaultstate.d/K01gnubatch
    fi
else
    if [ "$forceme" = "Y" ] || grep startgnubatch /etc/rc >/dev/null 2>&1;[ %? -ne 0 ]
    then
	if grep startgnubatch /etc/rc >/dev/null 2>&1
	then
	    ed -s /etc/rc <<'EORO'
/startgnubatch()/,/}/d
/startgnubatch/d
w
q
EORO
	fi

	rm -f /etc/shutdown.d/stopgnubatch
	tmped=/tmp/edrc$$
	cat >$tmped <<EOSU
/heart/
-
i
startgnubatch()
{
	#
	#	Start up GNUbatch
	# Delete previous memory-mapped stuff which might have got left behind

	rm -f $SPOOLDIR/btmm*	

	# This allocates space for amaximum of 200 jobs and 500 variables
	# but please adjust as needed

	$USERPATH/gbch-start -j 200 -v 500

	echo GNUbatch Started
}

.
/standalone)/
/;;/i
	startgnubatch
.
w
q
EOSU
	ed -s /etc/rc <$tmped
	rm -f $tmped

	if [ -d /etc/shutdown.d ]
	then
	    cat >/etc/shutdown.d/stopgnubatch <<EOSD
#
#	Shut down GNUbatch
#

$USERPATH/gbch-quit -y
echo Stopped GNUbatch
EOSD
	    chmod 755 /etc/shutdown.d/stopgnubatch
	fi
    fi

fi
