#!/bin/bash
# prerm script for webcontentcontrol
#
# see: dh_installdeb(1)

set -e

############################
#ADDED FUNCTIONS
############################

function check_fh {
    ## FireHol
    if ( test -f /var/lock/firehol || test -f /var/lock/subsys/firehol );
    then fh_running=1 && echo "FireHol is running";
    else fh_running=0 && echo "FireHol is stopped";
    fi
}

function check_tp {
    ## TinyProxy
    pid_tp="`pidof tinyproxy | tr -d [:space:]`"
    if (test -z $pid_tp);
    then tp_running=0 && echo "TinyProxy is stopped"; 
    else tp_running=1 && echo "TinyProxy is running";
    fi
}

function check_dg {
    ## DansGuardian
    pid_dg="`pidof dansguardian | tr -d [:space:]`"
    if (test -z $pid_dg);
    then dg_running=0 && echo "DansGuardian is stopped";
    else dg_running=1 && echo "DansGuardian is running";
    fi
}

function check_all {
    check_fh
    check_tp
    check_dg
}

function stop_all_nogui {
    check_all
    ## stops what it is not running
    ## Firehol
    if (test -z $fh_running);
    then echo "FireHol already stopped";
    else /etc/init.d/firehol stop;
    fi

    ## TinyProxy
    if (test -z $tp_running);
    then echo "TinyProxy already stopped";
    else /etc/init.d/tinyproxy stop;
    fi

    ## DansGuardian
    if (test -z $dg_running);
    then echo "DansGuardian already stopped";
    else /etc/init.d/dansguardian stop;
    fi
}

function stopcheck {
    check_all
    if (test $fh_running -eq 0) && (test $tp_running -eq 0) && (test $dg_running -eq 0);
    then
	echo "All OK! Everything was stopped"
	return 0
    else
	echo "There was an error"
	return 1
    fi
}

function import_all {
    if [ $# -ne 1 ]
    then
	    echo "usage : $0 <filename>"
	    exit 0
    fi

    DIR=$(dirname "$1")
    filename=$DIR/$(basename "$1" .dgs.full).dgs.full

    echo "Running from $(pwd)"
    echo "===>importing $filename"
    tar -Pxzvf "$filename"
    echo "Running from $(pwd)"
    echo "===>imported $filename"
}

############################

# 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

echo "===>prerm:$1"

############################
#VARIABLES
MAINDIR=/usr/share/webcontentcontrol/
SCRIPTSDIR=$MAINDIR/scripts
############################

case "$1" in
    upgrade)
        echo "prerm upgrade called"
    ;;

    remove|deconfigure)

        echo "prerm remove|deconfigure called"
        echo "Unconfiguring dansguardian+firehol+tinyproxy"
        stop_all_nogui
        stopcheck
        import_all "$MAINDIR/backup"
        rm -v $MAINDIR/configured
    ;;

    failed-upgrade)
    ;;

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