#!/bin/sh -e
#
# Authors: Bas van der Vlies & Jaap Dijkshoorn
# Desc.  : Put a default torque file in /etc/default/torque
#
# SVN INFO: 
#	$Id: postinst 2329 2006-09-21 10:12:53Z bas $
#

SPOOLDIR=/var/spool/torque
CONFIG_FILE=/etc/default/torque
MKDIR="/usr/sbin/pbs_mkdirs"

# Always check/create the directory structure
#
$MKDIR all

# Also build the tclindexes again for xpbs xpbsmon
#
for file in xpbs xpbsmon
do
	if [ -d  /usr/lib/torque/$file ] 
	then
		cd /usr/lib/torque/$file
		./buildindex /usr/lib/torque/$file
	fi
done

# creating default file torque in /etc/default
#
create_default_torque_file() {
  cat > $CONFIG_FILE << EOF
#
# Author: Bas van der Vlies & Jaap Dijkshoorn
# created with install of torque
#
# setting default to run pbs_mom. To enable an service
# set the value to 1.
#
#
PBS_MOM=1
PBS_SCHED=0
PBS_SERVER=0

PBS_MOM_OPTS=''
PBS_SCHED_OPTS=''
PBS_SERVER_OPTS='-a t'

EOF
}

if [ ! -f $CONFIG_FILE ]
then
	create_default_torque_file
else
	# If old style config replace it we new one
	#
	OLD_CONFIG=`awk '/^HOST=/ {  print $0 }'  $CONFIG_FILE`
	if [ ! -z "$OLD_CONFIG" ]
	then
		create_default_torque_file
	fi
fi

#
# make the spool directory an set mode bits.
#
if [ ! -d $SPOOLDIR/spool ]
then
	mkdir $SPOOLDIR/spool
fi
chmod 1777 $SPOOLDIR/spool

# Automatically added by dh_installinit
if [ -x "/etc/init.d/torque" ]; then
	update-rc.d torque defaults >/dev/null
	if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
		invoke-rc.d torque start || exit 0
	else
		/etc/init.d/torque start || exit 0
	fi
fi
# End automatically added section
