#!/bin/bash
#
# chkconfig: 235 98 40
# description: Symantec AntiVirus Scanner
#
### BEGIN INIT INFO
# Provides:      rtvscand
# Required-Start: symcfgd
# Required-Stop: symcfgd
# Should-Start: $all autoprotect
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Symantec AntiVirus Scanner
### END INIT INFO

set_locale()
{
    for file in /etc/environment /etc/default/locale; do
        [ -r "$file" ] || continue
        [ -s "$file" ] || continue

        for var in LANG LANGUAGE LC_ALL LC_CTYPE; do
            value=`egrep "^${var}=" "$file" | tail -n1 | cut -d= -f2`
            [ -n "$value" ] && eval export $var=$value
        done
    done
}

set_locale

# Check permissions
if [ "`id -u`" != '0' ]; then
  echo 'Error: you must be superuser.'
  exit 1
fi

# Source rtvscand options file
if [ -f /etc/sysconfig/rtvscand ]; then
  . /etc/sysconfig/rtvscand

  # don't raise an error if the config file is incomplete
  # set defaults instead:
  RTVSCAND_OPTS=${RTVSCAND_OPTS:-""}
  RTVSCAND_SHUTDOWN_TIMEOUT=${RTVSCAND_SHUTDOWN_TIMEOUT:-120}
else
  RTVSCAND_OPTS="-l info"
  RTVSCAND_SHUTDOWN_TIMEOUT=120
fi

prog="SAVCE"
symconf=/etc/Symantec.conf
symbasedir=/opt/Symantec

if [ -f /etc/redhat-release ]; then
    success_word="OK"
    failure_word="FAILED"
else
    success_word="done"
    failure_word="failed"
fi
    
success_msg="\033[70G\033[32m\033[1m$success_word\033[m"
failure_msg="\033[70G\033[31m\033[1m$failure_word\033[m"

# read config to get basedir
if [ -f $symconf ]
then
    ndir=`/usr/bin/awk -v IGNORECASE=1 -F= '/^basedir=/ {print $2}' < $symconf`
    if [ -n "$ndir" -a -d "$ndir" ]
    then
    symbasedir=$ndir
    fi
fi

savdir=$symbasedir/symantec_antivirus

# Make sure that binary executable exists.  If no, then exit.
[ -x $savdir/rtvscand ] && RTVSCAND=$savdir/rtvscand
[ -z "$RTVSCAND" ] && echo -e $failure_msg && exit 1

ret=0

start()
{
    if [ ! -d /var/run/symantec ] ; then
        mkdir -p /var/run/symantec
    fi
    if [ ! -d /var/lock/subsys ] ; then
        mkdir -p /var/lock/subsys
    fi

    # If rtvscand is already running, then do nothing.
    status > /dev/null
    if [ $? -eq 0 ] ; then
        echo "Already running"
        return $ret
    fi

    # Make sure symcfgd is running first.
    /etc/init.d/symcfgd status > /dev/null
    if [ $? -ne 0 ] ; then
        /etc/init.d/symcfgd start
        if [ $? -ne 0 ] ; then
            echo $failure_msg
            exit 1
        fi
    fi

    echo -n $"Starting rtvscand: "
    $RTVSCAND $RTVSCAND_OPTS > /dev/null 2>&1
    ret=$?
    # If running, wait for pid file to be created.
    if [ $ret -eq 0 ] ; then
       timeout=0
       while : ; do
          status >/dev/null 2>&1
          [ $? -ne 0 ] || break
          if [ $timeout -ge $RTVSCAND_SHUTDOWN_TIMEOUT ]; then
             echo -e $failure_msg
             echo
             return 1
          fi
          sleep 2 && echo -n "."
          timeout=$((timeout+2))
       done
       touch /var/lock/subsys/rtvscand
       echo -e $success_msg
    else
       echo -e $failure_msg
    fi

    echo
    return $ret
}

stop()
{
    echo -n $"Stopping rtvscand: "

    # See if process is running.
    status >/dev/null 2>&1
    ret=$?

    # If running, send shutdown and wait for pid file to be deleted.
    if [ $ret -eq 0 ] ; then
       $RTVSCAND -k shutdown $RTVSCAND_OPTS
       rm -f /var/lock/subsys/rtvscand
       timeout=0
       while : ; do
          status >/dev/null 2>&1
          [ $? -eq 0 ] || break
          if [ $timeout -ge $RTVSCAND_SHUTDOWN_TIMEOUT ]; then
             echo -e $failure_msg
             echo
             return 1
          fi
          sleep 2 && echo -n "."
          timeout=$((timeout+2))
       done
       echo -e $success_msg
       echo
    else
       echo -e $failure_msg
       echo
    fi
    return $ret
}

status()
{
   $RTVSCAND -k check $RTVSCAND_OPTS
}

report()
{
    # See if process is running.
    status >/dev/null 2>&1
    ret=$?

    # If running, generate report.
    if [ $ret -eq 0 ] ; then
        $RTVSCAND -k report $RTVSCAND_OPTS
        echo "Status report appended to report file."
    else
        echo "process not running, cannot generate report"
    fi
}

restart()
{
    stop
    start
}

condrestart()
{
    [ -e /var/lock/subsys/rtvscand ] && restart || :
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status
    ;;
  report)
    report
    ;;
  restart)
    restart
    ;;
  condrestart)
    condrestart
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|report|restart|condrestart}"
    exit 1
esac

exit $?
