#!/bin/sh
# postinst script for xCAT
#
# 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)
        state_dir=/var/lib/xcat/configure-state
        if [ -L "$state_dir" ] || [ -L "$state_dir/context" ] ||
           { [ -e "$state_dir" ] && [ ! -d "$state_dir" ]; }; then
            echo "Invalid xCAT configuration state: $state_dir" >&2
            exit 1
        fi
        if [ -f "$state_dir/context" ]; then
            context=$(cat "$state_dir/context")
            case "$context" in
                fresh|upgrade) ;;
                *) echo "Invalid xCAT configuration context in $state_dir/context: $context" >&2; exit 1 ;;
            esac
        elif [ -f /tmp/xCAT_upgrade.tmp ]; then
            context=upgrade
        else
            context=fresh
        fi
        # Make dir for the current pid
        mkdir -p /var/run/xcat
        cp -f /opt/xcat/share/xcat/scripts/xHRM /install/postscripts/
        . /etc/profile.d/xcat.sh
        if [ "$context" = upgrade ]
        then
            if [ -r "/tmp/xcat/installservice.pid" ]; then
                mv /tmp/xcat/installservice.pid /var/run/xcat/installservice.pid
            fi
            if [ -r "/tmp/xcat/udpservice.pid" ]; then
                mv /tmp/xcat/udpservice.pid /var/run/xcat/udpservice.pid
            fi
            if [ -r "/tmp/xcat/mainservice.pid" ]; then
                mv /tmp/xcat/mainservice.pid /var/run/xcat/mainservice.pid
            fi
            xcat_run_upgrade xcatconfig -u -V
        else
            xcatconfig -i
        fi

        ln -s -f /etc/apache2/conf-available/xcat.conf.apach24 /etc/apache2/conf-enabled/xcat.conf

        # xcat.conf sets security response headers via mod_headers, which is
        # not enabled by default on Debian/Ubuntu.  Enable it so the headers
        # take effect (the directives are wrapped in <IfModule mod_headers.c>
        # so a missing module never breaks the apache restart below).
        if command -v a2enmod >/dev/null 2>&1; then
            a2enmod -q headers >/dev/null 2>&1 || :
        fi

        /etc/init.d/apache2 restart

        # Let rsyslogd perform close of any open files
        if [ -e /var/run/rsyslogd.pid ]; then
            kill -HUP $(</var/run/rsyslogd.pid) >/dev/null 2>&1 || :
        elif [ -e /var/run/syslogd.pid ]; then
            kill -HUP $(</var/run/syslogd.pid) >/dev/null 2>&1 || :
        fi
    ;;

    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#

if [ "$1" = configure ]; then
    rm -f "$state_dir/context" /tmp/xCAT_upgrade.tmp
    rmdir "$state_dir" 2>/dev/null || :
fi

exit 0
