#!/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
SERVER_CONF_FILE=${SPOOLDIR}/server_name
CONFIG_FILE=/etc/default/torque
MKDIR="/usr/sbin/pbs_mkdirs"


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


# Source debconf library.
. /usr/share/debconf/confmodule


# 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() {
    set_conf_options() {
        RESULT=""
        db_get torque/$1 || true
        if [ $? -eq 0 ]; then
            RESULT=$RET
        fi
    }

    PBS_SERVER=0
    PBS_MOM=0
    PBS_SCHED=0
    PBS_MOM_OPTS=""
    PBS_SCHED_OPTS=""
    PBS_SERVER_OPTS=""

    db_get torque/daemon_list

    for daemon in $RET; do
        daemon=${daemon%,}

        # this sets $RESULT
        set_conf_options ${daemon}_opts

        case "$daemon" in
            pbs_server)
                PBS_SERVER=1
                PBS_SERVER_OPTS=$RESULT
                ;;
            pbs_mom)
                PBS_MOM=1
                PBS_MOM_OPTS=$RESULT
                ;;
            pbs_sched)
                PBS_SCHED=1
                PBS_SCHED_OPTS=$RESULT
                ;;
        esac
    done

  cat > $CONFIG_FILE << EOF
#
# Author: Bas van der Vlies & Jaap Dijkshoorn
# created with install of torque
#
# Do not edit this file by hand, use
#
#     dpkg-reconfigure -p high torque
#
# to change which daemons to start or
#
#     dpkg-reconfigure -p medium torque
#
# to even change startup options (PBS_{MOM,SCHED,SERVER}_OPTS).

PBS_MOM=$PBS_MOM
PBS_SCHED=$PBS_SCHED
PBS_SERVER=$PBS_SERVER

PBS_MOM_OPTS="$PBS_MOM_OPTS"
PBS_SCHED_OPTS="$PBS_SCHED_OPTS"
PBS_SERVER_OPTS="$PBS_SERVER_OPTS"

EOF
}


if [ "$1" = configure -o ! -f $CONFIG_FILE ]
then
	create_default_torque_file
fi


#
# Generate server_name
#
if [ "$1" = configure -o ! -f $SERVER_CONF_FILE ]; then
    db_get torque/server_name || true
    NEW_SERVER_NAME="$RET"
    OLD_SERVER_NAME="`cat $SERVER_CONF_FILE`"

    if [ $OLD_SERVER_NAME != $NEW_SERVER_NAME ]; then
        # backup
	mv $SERVER_CONF_FILE $SERVER_CONF_FILE.`date +"%Y%m%d-%H%M%S"`
    fi

    echo $NEW_SERVER_NAME > $SERVER_CONF_FILE
fi


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


# pbs_sched needs a subdir not created by pbs_mkdirs.
if [ ! -d $SPOOLDIR/sched_priv/accounting ]
then
    mkdir $SPOOLDIR/sched_priv/accounting
fi

#
# create an empty config file for pbs_mom if missing
#
if [ ! -f $SPOOLDIR/mom_priv/config ]
then
    touch $SPOOLDIR/mom_priv/config
fi


#DEBHELPER#


# 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
        if [ "$1" = configure ]; then
            invoke-rc.d torque stop || true
        fi
		invoke-rc.d torque start || exit 0
	else
        if [ "$1" = configure ]; then
            /etc/init.d/torque stop || true
        fi
		/etc/init.d/torque start || exit 0
	fi
fi
# End automatically added section


