#!/bin/sh

###############################################################################
#
# /etc/init.d/syslog - System control script for the syslog daemon.
# Also accessed via a symbolic link named /usr/local/sbin/rcsyslog.
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Andre G Ancelin
# Company- Adtec Digital, Inc.
# Date-    2006-01-10
#
###############################################################################

readonly DAEMON="/sbin/syslogd"
readonly LOG_DIR="/var/log"
readonly SSD_BIN="/sbin/start-stop-daemon"
readonly USER="syslogd"
readonly LOG_FILE="${LOG_DIR}/all*"
readonly USERS="users"

# terse vrsn command outputs current app version
readonly VRSN_CMD="/usr/local/sbin/vn -t"
readonly BANNER="/etc/oem-banner.txt"
readonly LOG_TAG=$'\040\040[SYSLOG]'

# Indicate whether reloads are supported via signals (usually SIGHUP (1)).
# If so, define RELOAD_SIGNAL to the signal. If not, comment out.
#RELOAD_SIGNAL="1"

# Include the common script for all service scripts.
source /etc/init.d/rcservices

readonly ERR_FIFO="/var/log/error"

#-----------------------------------------------------------------------------
# set permission for log file
set_perm()
{
  chown ${USER}:${USERS} ${LOG_FILE} 
}

#-----------------------------------------------------------------------------
# make needed named pipes for syslog
make_fifo()
{
   if [ ! -e "$ERR_FIFO" ]; then
      mkfifo "$ERR_FIFO"
      chown syslogd "$ERR_FIFO"
   fi
}


#------------------------------------------------------------------------------
# Start the service.
do_start ()
{
  echo -n "Starting $DAEMON daemon "
  set_perm
  make_fifo
  $SSD_BIN --quiet --start --exec $DAEMON
  #MPG: removed chuid for DTA in Jaguar-2.  See me before merging this to head.
  #  chuid not needed DTA, but important in players.  May test out product for args.
  #$SSD_BIN --quiet --start --exec $DAEMON --chuid $USER 
  case $? in
    0) RESULT=$SUCCESS;;             # Started OK
    1) RESULT=$PROGRAM_IS_RUNNING;;  # Already running
    *) RESULT=$FAILED;;              # Trouble
  esac
  print_result
  
  # write the currently booted version to logs
  logger -t "$LOG_TAG" "==========================================================="
  if [ -e "$BANNER" ]; then   # if banner exists
     head -n 1 "$BANNER" | logger -t "$LOG_TAG" 
  else                        # no banner, use vrsn 
     $VRSN_CMD | logger -t "$LOG_TAG"
  fi     
  logger -t "$LOG_TAG" "==========================================================="
  
  local QUOTA_REP=$(quota -q -u syslogd 2>/dev/null)
  logger -t "$LOG_TAG" "${QUOTA_REP}"  

  logger -t "$LOG_TAG" "==========================================================="
  
  return $RESULT
}

#------------------------------------------------------------------------------
# Stop the service.
do_stop ()
{
  echo -n "Stopping $DAEMON daemon "
  $SSD_BIN --quiet --stop --exec $DAEMON
  case $? in
    0) RESULT=$SUCCESS;;              # Stopped OK
    1) RESULT=$PROGRAM_NOT_RUNNING;;  # No process running
    *) RESULT=$FAILED;;               # Trouble
  esac
  print_result
  return $RESULT
}

# Do the service.
dispatch_service $1
