#! /bin/sh -e
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

# postinst script for condor
#
# see: dh_installdeb(1)

set -e


CONFIGFILE=/etc/condor/condor_config
FQHN=`hostname -f`
FQDN=`hostname -d`

. /usr/share/debconf/confmodule
db_version 2.0

do_configure()
{
	# add daemon user
	if ! /usr/bin/getent passwd condor > /dev/null; then 
		adduser --quiet --no-create-home --system --gecos "Condor Daemon"  condor
	fi

	# condor_init does not take care of the directory permissions if they exist, so we have to do it
	chown condor /var/log/condor
	chmod 755 /var/log/condor
	chown -R condor /var/spool/condor
	chmod 755 /var/spool/condor
	chmod 1777 /var/spool/condor/execute
	chown condor /etc/condor
	chmod 0700 /var/spool/condor/cred

	# create config file, if needed
	if [ ! -e $CONFIGFILE ]; then
		gunzip -c /usr/share/doc/condor/examples-conf/condor_config.debian.gz > /etc/condor/condor_config
	fi

	# store admin email address in any case
	db_get condor/admin
	ADMIN="$RET"
	sed -e "s,^[ \t]*CONDOR_ADMIN[ \t]*=.*,CONDOR_ADMIN = $ADMIN," \
	   < $CONFIGFILE > $CONFIGFILE.tmp
	mv -f $CONFIGFILE.tmp $CONFIGFILE

	db_get condor/reservedmemory
	RESERVEDMEM="$RET"
	if [ "$RET" != "" ]
	then
		sed -e "s/^[ \t#]*RESERVED_MEMORY[ \t]*=.*/RESERVED_MEMORY = $RESERVEDMEM/" \
		< $CONFIGFILE > $CONFIGFILE.tmp
		mv -f $CONFIGFILE.tmp $CONFIGFILE
	else
		sed -e "s/^[ \t#]*RESERVED_MEMORY[ \t]*=.*/#RESERVED_MEMORY = 0/" \
		< $CONFIGFILE > $CONFIGFILE.tmp
		mv -f $CONFIGFILE.tmp $CONFIGFILE
	fi

	# configure personal condor if needed
	db_get condor/personal
	if [ "$RET" = "true" ]; then
		# set personal condor parameters
		# taken from condor_configure script, which we cannot use directly
		sed -e "s/^[ \t]*DAEMON_LIST[ \t]*=.*/DAEMON_LIST = MASTER, COLLECTOR, NEGOTIATOR, STARTD, SCHEDD/" \
		    -e "s/^[ \t]*START[ \t]*=.*/START = TRUE/" \
		    -e "s/^[ \t]*PREEMPT[ \t]*=.*/PREEMPT = FALSE/" \
		    -e "s/^[ \t]*SUSPEND[ \t]*=.*/SUSPEND = FALSE/" \
		    -e "s/^[ \t]*VACATE[ \t]*=.*/VACATE = FALSE/" \
		    -e "s/^[ \t]*CONDOR_HOST[ \t]*=.*/CONDOR_HOST = $FQHN/" \
		    -e "s/^[ \t]*UID_DOMAIN[ \t]*=.*/UID_DOMAIN = $FQDN/" \
		    -e "s/^[ \t]*FILESYSTEM_DOMAIN[ \t]*=.*/FILESYSTEM_DOMAIN = $FQDN/" \
		    < $CONFIGFILE > $CONFIGFILE.tmp
		mv -f $CONFIGFILE.tmp $CONFIGFILE

	else

		# configure normal node
		db_get condor/daemons
		DAEMONS=`echo $RET | sed -e "s/Job submission/SCHEDD/" \
					-e "s/Job execution/STARTD/" \
					-e "s/Central manager/COLLECTOR, NEGOTIATOR/" \
					-e "s/^/MASTER, /"`
		db_get condor/centralmanager
		CENTRALMANAGER="$RET"
		db_get condor/uiddomain
		UIDDOMAIN="$RET"
		db_get condor/filesystemdomain
		FSDOMAIN="$RET"
		sed -e "s/^[ \t]*START[ \t]*=.*/START = \$(UWCS_START)/" \
		    -e "s/^[ \t]*PREEMPT[ \t]*=.*/PREEMPT = \$(UWCS_PREEMPT)/" \
		    -e "s/^[ \t]*SUSPEND[ \t]*=.*/SUSPEND = \$(UWCS_SUSPEND)/" \
		    -e "s/^[ \t]*CONDOR_HOST[ \t]*=.*/CONDOR_HOST = $CENTRALMANAGER/" \
		    -e "s/^[ \t]*UID_DOMAIN[ \t]*=.*/UID_DOMAIN = $UIDDOMAIN/" \
		    -e "s/^[ \t]*FILESYSTEM_DOMAIN[ \t]*=.*/FILESYSTEM_DOMAIN = $FSDOMAIN/" \
		    -e "s/^[ \t]*DAEMON_LIST[ \t]*=.*/DAEMON_LIST = $DAEMONS/" \
		    -e "s/^[ \t]*RESERVED_MEMORY[ \t]*=.*/RESERVED_MEMORY = $RESERVEDMEM/" \
		    < $CONFIGFILE > $CONFIGFILE.tmp
		mv -f $CONFIGFILE.tmp $CONFIGFILE
	fi

	# work around Condor memory detection bug with Linux kernel 2.6
	# 85 is _SC_PHYS_PAGES, 30 is _SC_PAGESIZE (not available as constants in Perl)
	case `uname -r` in
		2.6*)
			AVAILMEM=`perl -e 'use POSIX;print int(POSIX::sysconf(85)*POSIX::sysconf(30)/1048576);'`
			sed -e "s/^[ \t#]*MEMORY[ \t]*=.*/MEMORY = $AVAILMEM/" \
			    < $CONFIGFILE > $CONFIGFILE.tmp
			mv -f $CONFIGFILE.tmp $CONFIGFILE
		;;
	esac

	# run condor_init to create missing files / directories
	/usr/sbin/condor_init 1> /dev/null 2> /dev/null
}

do_start()
{
	/etc/init.d/condor restart
}

case "$1" in
    configure)
	do_configure
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    reconfigure)
	do_configure
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#

db_stop
exit 0


