#! /bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/sbin/ipacset
CONFIG=/etc/ipac.conf
PROC20=/proc/net/ip_acct
PROC22=/proc/net/ip_fwchains
IPFWADM=/sbin/ipfwadm
IPCHAINS=/sbin/ipchains

# Abort immediately if the configuration file or daemon has been removed
# (this may occur if you have removed the package but not purged it.)
test -f $DAEMON && test -f $CONFIG || exit 0

# Check that our pre-requisites are available

check() {

    	case `uname -r` in

        2.2* | 2.3* | 2.4*)
		if ! [ -f $PROC22 ]; then
			echo " kernel does not have IP firewalling."
			exit 0
		fi

		if ! [ -f $IPCHAINS ]; then
			echo " ipchains is missing."
			exit 0
		fi
		;;

	2.0* | *)
		if ! [ -f $PROC20 ]; then
			echo " kernel does not have IP accounting."
			exit 0
		fi

		if ! [ -f $IPFWADM ]; then
			echo " ipfwadm is missing."
			exit 0
		fi
		;;

	esac
}

flush() {
    	case `uname -r` in

        2.2* | 2.3* | 2.4*)	

		# this wasn't meant to be easy
		ipchains -D input -j ipac_bth
		ipchains -D input -j ipac_in
		ipchains -D output -j ipac_bth
		ipchains -D output -j ipac_out
		ipchains -F ipac_bth
		ipchains -F ipac_in
		ipchains -F ipac_out
		ipchains -X ipac_bth
		ipchains -X ipac_in
		ipchains -X ipac_out
		;;

        2.0* | *)
		ipfwadm -A -f
		;;

    	esac
}

case "$1" in
  	start)
		echo -n Starting IP accounting...
		check
		$DAEMON
		echo done.
		;;

  	stop)
		echo -n Stopping IP accounting...
		check
		fetchipac
		flush
		echo done.
    		;;

  	restart | force-reload)
		echo -n Restarting IP accounting...
		check
		fetchipac
		flush
		$DAEMON
		echo done.
		;;

	reload)
		exit 1
		;;

 	*)
    		echo "Usage: /etc/init.d/ipac {start|stop|restart|force-reload}"
    		exit 1
    		;;

esac

exit 0


