#!/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)
        # 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 [ -f /tmp/xCAT_upgrade.tmp ]
        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
            mkdir -p /var/log/xcat
            date >> /var/log/xcat/upgrade.log
            xcatconfig -u -V >> /var/log/xcat/upgrade.log
            rm /tmp/xCAT_upgrade.tmp
        else
            xcatconfig -i -d -s
        fi

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

        /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#

exit 0
