#!/bin/sh
#
# weblookerd the server component of the weblooker network.
#
# chkconfig:   2345 95 05
# description: weblookerd monitors for their status, up- and \
#              downtimes. This is the daemon which receives \
#              and stores the values
#
### BEGIN INIT INFO
# Provides: weblookerd
# Required-Start: $remote_fs $syslog $network 
# Required-Stop: $remote_fs $syslog $network
# Should-Start: mysql
# Should-Stop: mysql
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This daemon stores uptime-statistics into a database.
# Description:  weblookerd is necessary if you want to be able to store
#               the values collected by clients into a database.
### END INIT INFO

# File:     weblookerd.init
# Author:   Tobias C. Sutor <tobias@sutor-it.com>
# Version:  0.2.1.1 $Rev: 379 $
#
# Copyright (C) 2011  Christian Eppler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

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

retval=0
exec="/usr/sbin/weblookerd"
prog="weblookerd"
config="/etc/weblooker/weblookerd.conf"
pidfile="/var/run/weblookerd.pid"

lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon --pidfile=$pidfile $exec
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    # stop it here, often "killproc $prog"
    killproc -p $pidfile $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
