#!/bin/sh
# $Id: template,v 1.3 2003/05/21 17:47:00 ldv Exp $
#
# havp	This shell script takes care of starting and stopping
#	a proxy with a anti-virus scanner
#
# chkconfig: - 91 9
# description:	The main aims are continuous, non-blocking downloads and smooth scanning
#		of dynamic and password protected HTTP traffic. Havp antivirus proxy has
#		a parent and transparent proxy mode. It can be used with squid or standaloneof the service.
# processname: havp
# config: /etc/havp/havp.config
# pidfile: /var/run/havp/havp.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

SourceIfNotEmpty /etc/havp/spool_disk.config

: ${disk_size:=1g}
: ${use_tmpfs_disk:=1}

LOCKFILE=/var/lock/subsys/havp
PIDFILE=/var/run/havp/havp.pid
RETVAL=0

OPTION=
HAVP_CONFIG=/etc/havp/havp.config
if [ -f $HAVP_CONFIG ] ; then
OPTION="-c $HAVP_CONFIG"
fi

HAVP_BIN=/usr/sbin/havp
test -x $HAVP_BIN || exit 5

start()
{
       if [ ! "x$manual_mount" = "x1" ]; then
           if [ "x$use_tmpfs_disk" = "x1" ]; then
	        if ! grep -q "[[:space:]]/var/spool/havp[[:space:]]" /proc/mounts; then 
		    echo -n $"Mounting tmpfs filesystem in /var/spool/havp for havp:"
		    mount tmpfs /var/spool/havp -t tmpfs  -o mand,size=$disk_size
		    RETVAL=$?
		    if [ $RETVAL = 0 ]; then
			echo_success
	    		echo
		    else
	    		echo_failure
	    		echo
			return $RETVAL
		    fi
    		fi
	    else
    		echo -n $"Making ramdisk for havp:"
		head -c $disk_size < /dev/zero > $ram_disk
		mke2fs -F -q -m0 $ram_disk
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
		    echo_success
	    	    echo
		else
	    	    echo_failure
	    	    echo
		    return $RETVAL
		fi
		if ! grep -q "[[:space:]]/var/spool/havp[[:space:]]" /proc/mounts; then 
        	    echo -n $"Mounting ramdisk in /var/spool/havp for havp:"
		    mount -o loop,mand,rw $ram_disk /var/spool/havp
		    chown havp:havp /var/spool/havp
		    RETVAL=$?
		    if [ $RETVAL = 0 ]; then
			echo_success
	    		echo
		    else
	    		echo_failure
	    		echo
			return $RETVAL
		    fi
		fi
	    fi
	fi
    	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user havp -- havp $OPTION
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user havp -- havp
	RETVAL=$?
	if [ ! "x$manual_mount" = "x1" ]; then
	    if grep -q "[[:space:]]/var/spool/havp[[:space:]]" /proc/mounts; then 
        	echo -n $"Unmounting tmpfs filesystem in /var/spool/havp for havp:"
		umount /var/spool/havp
		RETVAL=$?
		if [ $RETVAL = 0 ]; then
		    echo_success
	    	    echo
		else
	    	    echo_failure
	    	    echo
		    return $RETVAL
		fi
    		if [ "x$use_tmpfs_disk" = "x1" ]; then
		    rm -f $ram_disk
		fi
    	    fi
	fi
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading havp
	stop_daemon --pidfile "$PIDFILE" --expect-user havp -HUP -- havp
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	reload-lists)
		if [ -e "$PIDFILE" ]; then
			kill -1 $(<$PIDFILE)
		fi
	        ;;
	status) 
		status	--pidfile "$PIDFILE" --expect-user havp -- havp
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|reload-lists|status}"
		RETVAL=1
esac

exit $RETVAL
