#! /bin/bash
#
# crond   Start/Stop the clam antivirus daemon.
#
# chkconfig: 2345 90 60
# description: clamd is a standard UNIX program that scans for Viruses.
# processname: clamd
# config: /etc/clamd.conf
# pidfile: /var/run/clamd.pid

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PREFIX=/usr
CONFIG=/etc/clamav.conf
PIDFILE=/var/run/clamd.pid
LOGFILE=/var/log/clamd.log
SOCKET=/tmp/clamd.sock

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
fi

RETVAL=0

# See how we were called.

prog="clamd"
progdir="/usr/sbin"
DAEMON="$progdir/$prog.exe"

test -f $DAEMON || exit 0

# Source configuration
if [ -f /etc/sysconfig/$prog ] ; then
    . /etc/sysconfig/$prog
fi

start() {
        echo -n $"Starting $prog: "
	# check if cygrunsrv process
        cygrunsrv --start $prog
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch $PIDFILE  && echo "done."
        return $RETVAL
}

stop() {
        echo -n $"Stopping $prog: "
	cygrunsrv --stop $prog
        if test -r $PIDFILE; then
            kill `cat $PIDFILE` 2> /dev/null || echo -n " failed"
        else
            echo -n " no PID file"
        fi
	# this really needs a long time to stop.
        RETVAL=$?
        echo "."
        [ $RETVAL -eq 0 ] && rm -f $PIDFILE $SOCKET && echo "done."
        return $RETVAL
}

rhstatus() {
        # service status
        cygrunsrv -Q $prog
        # connect(): No such file or directory
	/usr/bin/clamdscan /usr/sbin/clamd.exe
	#status clamd
}

restart() {
        echo -n $"Restarting $prog: "
        $0 stop
        sleep 1
        $0 start
        echo "done."
}

installcron() {
    ( crontab -l | grep freshclam >/dev/null 2>&1 ) || ( \
	echo "add something like this to your cronjob:"
	echo
	echo "*  */3 * * *	/usr/bin/freshclam >>/var/log/freshclam.log 2>&1"
	echo "#*  */3 * * *	/usr/bin/freshclam >>/var/log/freshclam.log 2>&1" >> /var/cron/tabs/$USER
	kill -s HUP `cat /var/run/cron.pid`
	crontab -e )
}
install() {
        freshclam
        echo -n $"Installing $prog daemon: "
	# some safety measures
	touch $LOGFILE $PIDFILE
	chgrp 18 $LOGFILE $PIDFILE
	chmod g+w $LOGFILE $PIDFILE
	rm -f $SOCKET
	#it was compiled with uid=18
	cygrunsrv --install $prog --path $DAEMON -e CYGWIN="$CYGWIN" --disp "CYGWIN `$DAEMON --version`"
        echo "done."
}
uninstall() {
        echo -n $"Uninstalling $prog daemon: "
        stop
	 cygrunsrv --remove $prog
        echo "done."
}
reload() {
        echo -n $"Reloading $prog daemon configuration: "
	echo "unsupported. $0 restart"
	return 1

	/usr/bin/kill -HUP `cat $PIDFILE`
        RETVAL=$?
        [ $RETVAL -eq 0 ] && echo "done."
        RETVAL=$?
        return $RETVAL
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  reload)
        reload
        ;;
  install)
        install
        ;;
  installcron)
        installcron
        ;;
  uninstall)
        uninstall
        ;;
  status)
        rhstatus
        ;;
  condrestart)
        [ -f $PIDFILE ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|install|uninstall|installcron|restart|condrestart}"
        exit 1
esac

exit $?
