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

clear_xcatd_pending_state()
{
    rm -f "$xcatd_state_dir/pending-xcatd" \
        "$xcatd_state_dir/pending-deleted" \
        "$xcatd_state_dir/pending-enabled" \
        "$xcatd_state_dir/pending-xcatd.tmp."*
}

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

xcat_systemd_os_release_status()
{
    [ -r /etc/os-release ] || return 2

    os_id=
    os_version=
    while IFS='=' read -r key value; do
        value=${value#\"}
        value=${value%\"}
        value=${value#\'}
        value=${value%\'}
        case "$key" in
            ID) os_id=$value ;;
            VERSION_ID) os_version=$value ;;
        esac
    done < /etc/os-release

    if [ "$os_id" = "debian" ] && [ -z "$os_version" ]; then
        return 0
    fi

    os_major=${os_version%%.*}
    case "$os_major" in
        ''|*[!0-9]*) return 2 ;;
    esac

    case "$os_id" in
        rhel|centos|rocky|almalinux|ol|scientific)
            [ "$os_major" -ge 7 ] && return 0
            return 1
            ;;
        fedora|ubuntu)
            [ "$os_major" -ge 15 ] && return 0
            return 1
            ;;
        debian)
            [ "$os_major" -ge 8 ] && return 0
            return 1
            ;;
        sles|sled|opensuse|opensuse-leap|opensuse-tumbleweed)
            [ "$os_major" -ge 12 ] && return 0
            return 1
            ;;
        *)
            return 2
            ;;
    esac

}

xcat_target_uses_systemd()
{
    if [ -d /run/systemd/system ]; then
        return 0
    fi

    if [ -L /sbin/init ]; then
        init_target=$(readlink /sbin/init 2>/dev/null || true)
        case "$init_target" in
            *systemd*) return 0 ;;
            *) return 1 ;;
        esac
    fi

    if [ -e /sbin/init ]; then
        return 1
    fi

    if xcat_systemd_os_release_status; then
        return 0
    else
        os_release_status=$?
        if [ "$os_release_status" -eq 1 ]; then
            return 1
        fi
    fi

    if [ -x /usr/lib/systemd/systemd ] || [ -x /lib/systemd/systemd ]; then
        return 0
    fi

    return 1
}

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
