#!/bin/bash
# /etc/init.d/remote_syslog
#
# remote_syslog This shell script takes care of starting and stopping
#               remote_syslog daemon
#
# chkconfig: - 58 74
# description: papertrail/remote_syslog \
#              https://github.com/papertrail/remote_syslog2/blob/master/examples/remote_syslog.init.d

### BEGIN INIT INFO
# Provides: remote_syslog
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named ntpdate
# Should-Stop: $syslog $named
# Short-Description: start and stop remote_errolog
# Description: papertrail/remote_syslog2
#              https://github.com/papertrail/remote_syslog2/blob/master/examples/remote_syslog.init.d
### END INIT INFO

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network
 
prog="/usr/local/bin/remote_syslog"   
config="/etc/log_files.yml"
pid_dir="/var/run"

EXTRAOPTIONS=""

pid_file="$pid_dir/remote_syslog.pid"

PATH=/sbin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

RETVAL=0

is_running(){
    # Do we have PID-file?
    if [ -f "$pid_file" ]; then
        # Check if proc is running
        pid=`cat "$pid_file" 2> /dev/null`
        if [[ $pid != "" ]]; then
            exepath=`readlink /proc/"$pid"/exe 2> /dev/null`
            exe=`basename "$exepath"`
            if [[ $exe == "remote_syslog" ]]; then
                # Process is running
                return 0
            fi
        fi
    fi
    return 1
}

start(){
    echo -n $"Starting $prog: "
    unset HOME MAIL USER USERNAME
    $prog -c $config --pid-file=$pid_file $EXTRAOPTIONS
    RETVAL=$?
    echo
    return $RETVAL
}

stop(){
    echo -n $"Stopping $prog: "
    if (is_running); then
      kill `cat $pid_file`
      RETVAL=$?
      echo
      return $RETVAL
    else
      echo "$pid_file not found"
    fi
}

status(){
    echo -n $"Checking for $pid_file: "

    if (is_running); then
      echo "found"
    else
      echo "not found"
    fi
}

reload(){
    restart
}

restart(){
    stop
    start
}

condrestart(){
    is_running && restart
    return 0
}

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

exit $RETVAL
