#!/bin/sh
# prerm script for firebird2.5-classic
#
# see: dh_installdeb(1)

# summary of how this script can be called:
#        * <prerm> `remove'
#        * <old-prerm> `upgrade' <new-version>
#        * <new-prerm> `failed-upgrade' <old-version>
#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
#        * <deconfigured's-prerm> `deconfigure' `in-favour'
#          <package-being-installed> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

set -e
. /usr/share/debconf/confmodule
set -u

FB_VER="2.5"
FB_FLAVOUR=classic
THIS="firebird$FB_VER-$FB_FLAVOUR"
. /usr/share/firebird$FB_VER-common/functions.sh

#---------------------------------------------------------------------
# stop server if it is running

checkIfClassicRunning()
{
    # Check to see who is holding locks if any
    # fb_lock_mgr naturally holds a lock and should be excluded
    LOCK_PIDS=`fb_lock_print | egrep "Process id:" | sed -e 's/.*Process id: *//' | sed -e 's/, UID:.*//'` || true

    if [ -n "$LOCK_PIDS" ] ;
    then
        Q=shared/firebird/server_in_use
        db_subst "$Q" FB_VER $FB_VER
        db_subst "$Q" FB_FLAVOUR $FB_FLAVOUR

        if [ "${1:-}" = "loud" ]; then
            db_input critical "$Q" || true
            db_go || true
        else
            db_metaget "$Q" Description

            echo "$RET"
        fi

        exit 1
    fi
}


case "$1" in
    remove|deconfigure|upgrade)
        checkIfClassicRunning loud
    ;;
    failed-upgrade)
        checkIfClassicRunning
    ;;
esac


#DEBHELPER#

exit 0


