#!/bin/bash
#
# xinetd        This starts and stops xinetd.
#
# chkconfig: 345 65 50
# description: xinetd is a powerful replacement for inetd. \
#	       xinetd has access control machanisms, extensive \
#              logging capabilities, the ability to make services \
#              available based on time, and can place \
#              limits on the number of servers that can be started, \
#              among other things.
#
# processname: /usr/sbin/xinetd
# config: /etc/sysconfig/network
# config: /etc/xinetd.conf
# pidfile: /var/run/xinetd.pid

PATH=/sbin:/bin:/usr/bin:/usr/sbin
EXEEXT=.exe

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

# Get config.
test -f /etc/sysconfig/network && . /etc/sysconfig/network

# More config

test -f /etc/sysconfig/xinetd && . /etc/sysconfig/xinetd

RETVAL=0

prog="xinetd"

start(){
    [ -f /usr/sbin/xinetd${EXEEXT} ] || exit 5
    [ -f /etc/xinetd.conf ] || exit 6
    if [ x`type -t account_has_necessary_privileges` == "xfunction" ]
    then
    	account_has_necessary_privileges || exit 4
    fi

    echo -n $"Starting $prog: "

# Localization for xinetd is controlled in /etc/synconfig/xinetd
    if [ -z "$XINETD_LANG" -o "$XINETD_LANG" = "none" -o "$XINETD_LANG" = "NONE" ]; then
        unset LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE
    else
        LANG="$XINETD_LANG"
        LC_TIME="$XINETD_LANG"
        LC_ALL="$XINETD_LANG"
        LC_MESSAGES="$XINETD_LANG"
        LC_NUMERIC="$XINETD_LANG"
        LC_MONETARY="$XINETD_LANG"
        LC_COLLATE="$XINETD_LANG"
        export LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE
    fi
    unset HOME MAIL USER USERNAME
    daemon $prog -stayalive -pidfile /var/run/xinetd.pid "$EXTRAOPTIONS"
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/xinetd
    return $RETVAL
}

stop(){
    [ -f /usr/sbin/xinetd${EXEEXT} ] || exit 5
    [ -f /etc/xinetd.conf ] || exit 6
    if [ x`type -t account_has_necessary_privileges` == "xfunction" ]
    then
    	account_has_necessary_privileges || exit 4
    fi

    echo -n $"Stopping $prog: "
    killproc $prog
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/xinetd
    return $RETVAL
}

reload(){
    [ -f /usr/sbin/xinetd${EXEEXT} ] || exit 5
    [ -f /etc/xinetd.conf ] || exit 6

    echo -n $"Reloading configuration: "	
    killproc $prog -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart(){
    stop
    start
}

condrestart(){
    if [ -e /var/lock/subsys/xinetd ]
    then
        restart
        RETVAL=$?
        return $RETVAL
    fi
    RETVAL=0
    return $RETVAL
}

dump(){
    echo -n $"Dumping configuration: "	
    killproc $prog -USR1
    RETVAL=$?
    echo
    return $RETVAL
}

check(){
    echo -n $"Performing Consistency Check: "	
    /bin/kill -s ABRT $prog
    RETVAL=$?
    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
	start
        RETVAL=$?
	;;
    stop)
	stop
        RETVAL=$?
	;;
    status)
	status $prog
        RETVAL=$?
	;;
    restart)
	restart
        RETVAL=$?
	;;
    reload|force-reload)
	reload
        RETVAL=$?
	;;
    condrestart|try-restart)
	condrestart
        RETVAL=$?
	;;
    dump)
        dump
        RETVAL=$?
        ;;
    check)
        check
        RETVAL=$?
        ;;
    *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|reload|dump|check}"
	RETVAL=2
esac

exit $RETVAL
