#!/bin/sh

# chkconfig: - 98 02
# description: filebeat daemon
# processname: filebeat

PIDFILE=/var/run/filebeat.pid

case "$1" in
        start)
		if [ -f $PIDFILE ] ; then
			/bin/kill -0 `/bin/cat $PIDFILE`
			if [ "$?" = "0" ] ; then
				echo "Already active!"
				exit
			fi
			/bin/rm $PIDFILE
		fi

		umask 0027
		echo -n "Starting."
		/usr/share/filebeat/bin/filebeat --environment default -c /etc/filebeat/filebeat.yml --path.home /usr/share/filebeat --path.config /etc/filebeat --path.data /var/lib/filebeat --path.logs /var/log/filebeat &
		echo "$!" > $PIDFILE
		echo " done!"
        ;;

        stop)
		if [ ! -f $PIDFILE ] ; then
			echo "Not active!"
			exit
		fi

		/bin/kill -0 `/bin/cat $PIDFILE` 2>/dev/null >/dev/null
		if [ "$?" != "0" ] ; then
			echo "Not active!"
			/bin/rm $PIDFILE
			exit
		fi

		echo -n "Stopping."
		while /bin/kill `/bin/cat $PIDFILE` 2>/dev/null >/dev/null ; do
			echo -n "."
			sleep 1
		done
		echo " done!"
		/bin/rm $PIDFILE
        ;;

        restart)
                $0 stop
                /bin/sleep 1
                $0 start
        ;;

        *)
                echo "Usage: $0 start | stop | restart" >&2
                exit 1
        ;;
esac
