#!/bin/sh
#
# @package      hubzero-submit-condor
# @file         postinst
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/Apache-2.0 Apache2
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# 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.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

# 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.6.10
		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.2.9/${CONDORVERSION}/" \
                -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.2.9/${CONDORVERSION}/" \
                -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
