#!/bin/bash
#
# chkconfig: 2345 22 45
# description: Symantec AntiVirus Configuration Server
#
### BEGIN INIT INFO
# Provides:      symcfgd
# Required-Start: $syslog $remote_fs 
# Required-Stop: $syslog $remote_fs
# Should-Start: autoprotect splash_late xdm nss novell-ncs novell-tomcat5 smdr
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Description: Symantec AntiVirus Configuration Server
### 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 /etc/sysconfig/symcfgd options file
if [ -f /etc/sysconfig/symcfgd ]; then
  . /etc/sysconfig/symcfgd

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

prog="symcfgd"
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/symcfgd ] && SYMCFGD=$savdir/symcfgd
[ -z "$SYMCFGD" ] && echo -e $failure_msg && exit 1

ret=0

start()
{
    if [ ! -d /var/run/symantec ]; then
        mkdir -p /var/run/symantec
    fi
 
    # If symcfgd is already running, then do nothing.
    status > /dev/null
    if [ $? -eq 0 ] ; then
        echo "Already running"
        return $ret
    fi

    # If smcd is running, stop it before starting symcfgd.
    /etc/init.d/smcd status > /dev/null
    if [ $? -eq 0 ] ; then
        /etc/init.d/smcd stop
    fi

    # If rtvscand is running, stop it before starting symcfgd.
    /etc/init.d/rtvscand status > /dev/null
    if [ $? -eq 0 ] ; then
        /etc/init.d/rtvscand stop
    fi

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

    echo
    return $ret
}

stop()
{
    # If smcd is running, stop it before stopping symcfgd.
    /etc/init.d/smcd status > /dev/null
    if [ $? -eq 0 ] ; then
        /etc/init.d/smcd stop
    fi

    # If rtvscand is running, stop it before stopping symcfgd.
    /etc/init.d/rtvscand status > /dev/null
    if [ $? -eq 0 ] ; then
        /etc/init.d/rtvscand stop
    fi

    echo -n $"Stopping symcfgd: "

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

report()
{
    # 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
        $SYMCFGD $SYMCFGD_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/symcfgd ] && 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 $?
