#!/bin/sh
#
# webmin A web-based administration interface for Unix systems. 
#
# chkconfig: - 99 10
# description: Start or stop the Webmin server

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

start=/etc/webmin/start
stop=/etc/webmin/stop
confFile=/etc/webmin/miniserv.conf
pidFile=/var/webmin/miniserv.pid
prog='webmin'

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile=/var/lock/subsys/webmin

start() {
       $start >/dev/null 2>&1 </dev/null
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
                touch $lockfile >/dev/null 2>&1
        fi
        return $RETVAL
}

stop() {
        $stop
        RETVAL=$?
        if [ "$RETVAL" = "0" ]; then
                rm -f $lockfile
        fi
        pidfile=`grep "^pidfile=" $confFile | sed -e 's/pidfile=//g'`
        if [ "$pidfile" = "" ]; then
                pidfile=$pidFile
        fi
        rm -f $pidfile
        return $RETVAL
}

status() {
        pidfile=`grep "^pidfile=" $confFile | sed -e 's/pidfile=//g'`
        if [ "$pidfile" = "" ]; then
                pidfile=$pidFile
        fi
        if [ -s $pidfile ]; then
                pid=`cat $pidfile`
                kill -0 $pid >/dev/null 2>&1
                if [ "$?" = "0" ]; then
                        echo "$prog (pid $pid) is running"
                        RETVAL=0
                else
                        echo "$prog is stopped"
                        RETVAL=1
                fi
        else
                echo "$prog is stopped"
                RETVAL=1
        fi
        return $RETVAL
}

restart() {
        $stop ; $start
        RETVAL=$?
	return $RETVAL
}

reload() {
        $stop ; $start
        RETVAL=$?
	return $RETVAL
}


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
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
        exit 2

esac
exit $?

