#!/bin/sh
#
# @package      hubzero-submit-condor
# @file         postinst
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2015 HUBzero Foundation, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

# postinst script for hubzero-submit-condor
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
    	CONDORVERSION=8.2.9
		adduser --system --group --force-badname --home /var/condor --shell /bin/bash --disabled-password --gecos Condor condor
		HUBHOSTNAME=`hostname --fqdn`
		CONDORUID=`id --user condor 2>/dev/null`
		CONDORGID=`id --group condor 2>/dev/null`
		RANDOMLOCKID=`tr -dc "[:digit:]" < /dev/urandom | head -c 15`
		chown condor:condor /var/condor
		chown condor:condor /var/condor/Condor
		chown -R condor:condor /var/condor/Condor/${CONDORVERSION}
		chown condor:condor /var/condor/Grid/etc
		chown condor:condor /var/condor/Grid/etc/sysconfig
		chown condor:condor /var/condor/Grid/execute
		chown condor:condor /var/condor/Grid/log
		chown condor:condor /var/condor/Grid/spool
		chown condor:condor /var/condor/Grid/tmp
		sed -e "s/@CONDORVERSION@/${CONDORVERSION}/" /var/condor/condor.in > /var/condor/Grid/etc/sysconfig/condor
		if [ ! -f /var/condor/Grid/etc/condor_config ]; then \
		sed -e "s/@CONDORVERSION@/${CONDORVERSION}/" /var/condor/condor_config.in > /var/condor/Grid/etc/condor_config ; \
		else \
		sed -e "s/8.0.7/${CONDORVERSION}/" \
		-e "s/8.0.6/${CONDORVERSION}/" \
		-e "s/7.6.6/${CONDORVERSION}/" --in-place /var/condor/Grid/etc/condor_config ; \
		fi
		if [ ! -f /var/condor/Grid/etc/condor_config.local ]; then \
		sed -e "s/@CONDORVERSION@/${CONDORVERSION}/" \
		-e "s/@HUBHOSTNAME@/${HUBHOSTNAME}/" \
		-e "s/@CONDORUID@/${CONDORUID}/" \
		-e "s/@CONDORGID@/${CONDORGID}/" \
		-e "s/@RANDOMLOCKID@/${RANDOMLOCKID}/" /var/condor/condor_config.local.in > /var/condor/Grid/etc/condor_config.local ; \
		else \
		sed -e "s/8.0.7/${CONDORVERSION}/" \
		-e "s/8.0.6/${CONDORVERSION}/" \
		-e "s/7.6.6/${CONDORVERSION}/" --in-place /var/condor/Grid/etc/condor_config.local ; \
		fi
		chown condor:condor /var/condor/Grid/etc/sysconfig/condor
		chown condor:condor /var/condor/Grid/etc/condor_config
		chown condor:condor /var/condor/Grid/etc/condor_config.local
		chmod 1777 /var/condor/Grid/execute
		chmod 1777 /var/condor/Grid/log
		chmod 1777 /var/condor/Grid/tmp
    ;;

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

    *)
        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#

exit 0
