#!/bin/bash
# postinst script for condor

set -e

condor_user=condor
condor_gecos="HTCondor Daemons"
# make this one fixed because 'condor_config_val -tilde' relies on the user
# home dir to exist
condor_home=/var/lib/condor


case "$1" in
    configure)
        # according to http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=621833#119
        # this should always work
        if ! adduser --system --group --gecos "$condor_gecos" --home $condor_home \
                --disabled-password --disabled-login $condor_user --quiet 2>/dev/null; then
            # the only time where it would fail, is when there is an existing
            # non-system 'condor' user. This could happen e.g. in a heterogenous
            # HTCondor pool (various OSes) where the adminstrative HTCondor user
            # comes from LDAP and the home dir is shared across machines. This
            # is a supported deployment scenario for HTCondor (see installation
            # manual section 3.2)
            # the only problem is the possibility to conflict with an actual
            # "human" user with the same name, so only proceed when the
            # respective user is locked down
            SH=$(getent passwd | egrep '^condor:'| cut -d : -f 7)
            if [ "$SH" = "/bin/true" -o "$SH" = "/bin/false" -o \
                 "$SH" = "/sbin/nologin" -o "$SH" = "/usr/sbin/nologin" -o \
                 "$SH" = "/bin/nosh" ]; then
                echo "WARNING: HTCondor will be running under an existing non-system user account 'condor'."
            else
                echo "ERROR: HTCondor cannot run under unlocked non-system account 'condor'" 1>&2
                exit 1
            fi
        fi
        # condor's home directory needs to be world readable for proper operation
        if [ -d $condor_home ]; then
            # Open up the permission, if the directory exists
            chmod o+rx $condor_home
        else
            # Create the home directory, if non-existant
            mkdir -p $condor_home
            chown $condor_user $condor_home
        fi
        # condor_master needs a log directory to start up
        dirname=$(condor_config_val LOG 2>/dev/null)
        mkdir -p $dirname
        chown $condor_user $dirname

        # create a configuration directory for packagers
        mkdir -p /usr/share/condor/config.d
        # tell systemd to create tmpfiles, but do no harm if not available
        systemd-tmpfiles --create --exclude-prefix=/dev || true
        # reload system-daemon as service unit may have changed
        systemctl daemon-reload >/dev/null || true
    ;;

    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
