#!/bin/sh
# postrm script for openmpi
#
# see: dh_installdeb(1)

set -e

xcatd_state_dir=/var/lib/xcat/xcatd-init-state
xcatd_context_file=$xcatd_state_dir/context

if [ -L "$xcatd_state_dir" ] || \
   { [ -e "$xcatd_state_dir" ] && [ ! -d "$xcatd_state_dir" ]; }; then
    echo "Invalid xcatd init state directory: $xcatd_state_dir" >&2
    exit 1
fi

if xcat_target_uses_systemd && \
   command -v dpkg-maintscript-helper >/dev/null 2>&1 && \
   dpkg-maintscript-helper supports rm_conffile; then
    dpkg-maintscript-helper rm_conffile /etc/init.d/xcatd -- "$@"
fi

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    purge)
        rm -f /etc/init.d/xcatd \
            /etc/init.d/xcatd.ucf-old \
            /etc/init.d/xcatd.ucf-new \
            /etc/init.d/xcatd.ucf-dist \
            /etc/init.d/xcatd.dpkg-bak \
            /etc/init.d/xcatd.dpkg-backup \
            /etc/init.d/xcatd.dpkg-remove \
            /var/lib/xcat/xcatd-systemd-mode
        rm -f "$xcatd_state_dir/state" \
            "$xcatd_state_dir/xcatd" \
            "$xcatd_state_dir/state.tmp."* \
            "$xcatd_state_dir/xcatd.tmp."*
        clear_xcatd_pending_state
        rm -f "$xcatd_context_file" "$xcatd_context_file.tmp."*
        rmdir "$xcatd_state_dir" 2>/dev/null || true
        if command -v ucf >/dev/null 2>&1; then
            ucf --purge /etc/init.d/xcatd
        fi
        if command -v ucfr >/dev/null 2>&1; then
            ucfr --purge xcat-server /etc/init.d/xcatd
        fi
    ;;

    abort-install)
        clear_xcatd_pending_state
        rm -f "$xcatd_context_file" "$xcatd_context_file.tmp."*
        rmdir "$xcatd_state_dir" 2>/dev/null || true
    ;;

    abort-upgrade|disappear)
        rm -f "$xcatd_context_file" "$xcatd_context_file.tmp."*
    ;;

    upgrade|failed-upgrade)
    ;;

    remove)
        rm -f "$xcatd_context_file" "$xcatd_context_file.tmp."*
        rm -f /usr/sbin/xcatd  #remove the symbolic

        rm -f /etc/apache2/conf-enabled/xcat-ws.conf
    ;;

    *)
        echo "postrm 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
