#!/bin/bash

# poem-sync  -  This shell script enables cronjob for POEM_SYNC.
#
# Author:       eimamagi@srce.hr, babik@saske.sk
#
# chkconfig:    - 81 15
#
# description:  Enable a run of poem_sync cron.
# processname:  poem-sync


# source function library
. /etc/rc.d/init.d/functions

lockfile=/var/lock/subsys/poem_sync

RETVAL=0

start() {
        #echo -n $"Running poem_sync: "
        #( /usr/bin/poem_sync 2>&1 ) | gawk '{print strftime(), ":", $0}'  >> /var/log/poem/poem_sync.log
        #RETVAL=$PIPESTATUS
        #[ $RETVAL -eq 0 ] && success || failure
        echo
        echo -n $"Enabling poem-sync cron: "
        touch "$lockfile" && success || failure
        let "RETVAL+=$?"
        echo
}

stop() {
        echo -n $"Disabling poem-sync cron: "
        rm -f "$lockfile" && success || failure
        RETVAL=$?
        echo
        echo -n $"Killing all remaining processes: "
        PID=`pgrep -x poem_sync`;
        if [ -n "$PID" ]; then
                pkill -9 -P $PID; kill -9 $PID
        fi
        success
        echo
}

reload() {
        echo -n $"Running poem_sync: "
        /usr/bin/poem_sync > /dev/null 2>&1 && success || failure
        echo
}

restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|force-reload)
        restart
        ;;
  reload)
        reload
        ;;
  condrestart)
        [ -f "$lockfile" ] && restart
        ;;
  status)
        if [ -f $lockfile ]; then
                echo $"Cron poem-sync is enabled."
                RETVAL=0
        else
                echo $"Cron poem-sync is disabled."
                RETVAL=3
        fi
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|
condrestart}"
        exit 1
esac

exit $RETVAL
