#!/bin/sh

SMTPGW_BIN=/opt/kav/5.0/smtpgw/bin/smtpgw
SMTPGW_CONF=/etc/kav/5.0/smtpgw/smtpgw.conf
SMTPGW_PID=/var/run/smtpgw.pid
CONTROL_PATH=/var/db/kav/5.0/smtpgw/control
SMTPGW_USR=kavuser

start() {
    if [ -x $SMTPGW_BIN ]; then
        $SMTPGW_BIN --conf-file=$SMTPGW_CONF --pid-file=$SMTPGW_PID --user=$SMTPGW_USR
        ret=$?
    else
	ret=2
    fi

    case $ret in
    0)
        echo "smtpgw was started"
	;;
    2)
        echo "$SMTPGW_BIN was not found"
	;;
    *) 
        echo "smtpgw could not be started"
        ;;
    esac

    return $ret
}

stop_gw() {
    if [ -s $SMTPGW_PID ]; then 
	kill -TERM `cat $SMTPGW_PID` 2> /dev/null
	ret=$?

	while [ -f $SMTPGW_PID ];
	do
	    sleep 1
	done
    
	case $ret in
	0)
    	    echo "smtpgw was stopped"
    	    ;;
	*)
    	    echo "smtpgw could not be stopped" 
    	    ;;
	esac
    else
	ret=4
    fi
    
    return $ret
}

status() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -n "$PS" ]; then 
            echo "smtpgw is running"
            return 0
        else
            echo "smtpgw is not running"
            return 3
        fi
    else
        echo "smtpgw is not running"
        return 4
    fi        
}

restart() {
    stop_gw
    start
}

reload() {
    if [ -s $SMTPGW_PID ]; then 
	kill -HUP `cat $SMTPGW_PID` 2> /dev/null
	ret=$?

	case $ret in
	0)
    	    echo "smtpgw has been reloaded"
    	    ;;
	*)
    	    echo "smtpgw could not be reloaded" 
    	    ;;
	esac
    else
	ret=4
    fi
    
    return $ret
}

reload_bases() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_db_reload 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_db_reload ];
    do
	sleep 1
    done
    
    case $ret in
    0)
        echo "smtpgw: antiviral bases has been reloaded"
        ;;
    *)
        echo "smtpgw: antiviral bases could not be reloaded" 
        ;;
    esac

    return $ret
}

stats() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_stats 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_stats ];
    do
	sleep 1
    done
    
    case $ret in
    0)
        echo "smtpgw statistics has been flushed"
        ;;
    *)
        echo "smtpgw statistics could not be flushed" 
        ;;
    esac

    return $ret
}

recv_on() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_recv_on 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_recv_on ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Receiver tasks has been started"
        ;;
    *)
        echo "smtpgw Receiver tasks could not be started" 
        ;;
    esac

    return $ret
}

recv_off() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_recv_off 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_recv_off ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Receiver tasks has been stoped"
        ;;
    *)
        echo "smtpgw Receiver tasks could not be stoped" 
        ;;
    esac

    return $ret
}

send_on() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_send_on 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_send_on ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Sender tasks has been started"
        ;;
    *)
        echo "smtpgw Sender tasks could not be started" 
        ;;
    esac

    return $ret
}

send_off() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_send_off 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_send_off ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Sender tasks has been stoped"
        ;;
    *)
        echo "smtpgw Sender tasks could not be stoped" 
        ;;
    esac

    return $ret
}

avir_on() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_avir_on 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_avir_on ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Anti-Virus tasks has been started"
        ;;
    *)
        echo "smtpgw Anti-Virus tasks could not be started" 
        ;;
    esac

    return $ret
}

avir_off() {
    if [ -s $SMTPGW_PID ]; then 
        NAME=`basename $SMTPGW_BIN`	
        PS=`ps ax | grep $NAME | grep -v grep`
        if [ -s "$PS" ]; then 
            return 3
        fi
    else
        return 4
    fi        

    touch $CONTROL_PATH/.c_avir_off 2> /dev/null
    ret=$?

    while [ -f $CONTROL_PATH/.c_avir_off ];
    do
	sleep 1
    done

    case $ret in
    0)
        echo "smtpgw Anti-Virus tasks has been stoped"
        ;;
    *)
        echo "smtpgw Anti-Virus tasks could not be stoped" 
        ;;
    esac

    return $ret
}

case $1 in 
	start)
		start
		;;
	stop)
		stop_gw
		;;
	status)
		status
		;;
	restart)
		stop_gw
		start
		;;
	reload)
		reload
		;;
	reload-bases)
		reload_bases
		;;
	stats)
		stats
		;;
	recv-on)
		recv_on
		;;
	recv-off)
		recv_off
		;;
	send-on)
		send_on
		;;
	send-off)
		send_off
		;;
	avir-on)
		avir_on
		;;		
	avir-off)
		avir_off
		;;		
	*)
		echo "Usage: ${0##*/} {start|stop|restart|reload|reload-bases|status|stats|"
		echo "               recv-on|recv-off|send-on|send-off|avir-on|avir-off}"
		;;
esac

exit
