#!/bin/bash
#
# chkconfig: 235 98 40
# description: Symantec AntiVirus Scanner
#
### BEGIN INIT INFO
# Provides:      smcd
# Required-Start: symcfgd
# Required-Stop: symcfgd
# Should-Start: 
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Symantec endpoint for Linux client daemon
### END INIT INFO

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

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

  if [ -n "$RC_LANG" ]; then
    export LANG="$RC_LANG"
  fi

  if [ -n "$RC_LC_ALL" ]; then
    export LC_ALL="$RC_LC_ALL"
  fi

  if [ -n "$RC_LC_CTYPE" ]; then
    export LC_CTYPE="$RC_LC_CTYPE"
  fi
fi

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

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

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

prog="SMCD"
symconf=/etc/Symantec.conf
symbasedir=`grep BaseDir= /etc/Symantec.conf | awk -F"=" '{print $2}'`

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

# read config to get basedir
if [ -f $symconf ]
then
    ndir=`/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/smcd ] && SMCD=$savdir/smcd
[ -z "$SMCD" ] && echo -e $failure_msg && exit 1

ret=0

start()
{
    if [ ! -d /var/run/symantec ] ; then
        mkdir -p /var/run/symantec
    fi
 
    # If smcd is already running, then do nothing.
    status > /dev/null
    if [ $? -eq 0 ] ; then
        echo "SMC daemon 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

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

    echo -n $"Starting smcd: "
    $SMCD $SMCD_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 $SMCD_SHUTDOWN_TIMEOUT ]; then
             echo -e $failure_msg
             echo
             return 1
          fi
          sleep 2 && echo -n "."
          timeout=$((timeout+2))
       done
       touch /var/lock/subsys/smcd
       echo -e $success_msg
    else
       echo -e $failure_msg
    fi

    echo
    return $ret
}

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

    # 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
       $SMCD $SMCD_OPTS -k shutdown
       rm -f /var/lock/subsys/smcd
       timeout=0
       while : ; do
          status >/dev/null 2>&1
          [ $? -eq 0 ] || break
          if [ $timeout -ge $SMCD_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()
{
    $SMCD $SMCD_OPTS -k check
}

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

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

restart()
{
    stop
    start
}

condrestart()
{
    [ -e /var/lock/subsys/smcd ] && 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 $?
