#!/bin/sh

# Init file for the CUPS server daemon
#
# chkconfig: 2345 60 40
# description: The Common UNIX Printing System (CUPS), an \
#              advanced printer spooling system which \
#              allows setting of printer options and \
#              automatic availability of a printer \
#              configured on one server in the whole \
#              network. Default printing system of Linux \
#              Mandrake.
#
# processname: cupsd
# config: /etc/cups/cupsd.conf
# config: /etc/cups/client.conf
# config: /etc/cups/classes.conf
# config: /etc/cups/printers.conf
# config: /etc/cups/mime.types
# config: /etc/cups/mime.convs
# config: /etc/cups/ppds.dat

WITHOUT_RC_COMPAT=1

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

LOCKFILE=/var/lock/subsys/cups
RETVAL=0

[ -f /sbin/ifconfig ] || exit

prepare_loopback()
{
	if !(/sbin/ifconfig | /bin/egrep "^lo +[^ ]+.*Loopback" > /dev/null 2>&1); then
	    echo "Loopback device (\"lo\", 127.0.0.1) needed by CUPS, starting it ..."
	    /sbin/ifconfig lo 127.0.0.1 > /dev/null 2>&1
	    RETVAL=$?
	    if [ $RETVAL -ne 0 ]; then
		echo
		echo -n "Cannot start loopback device, start of CUPS aborted"
	    fi
	fi
}

start()
{
	prepare_loopback
	msg_starting $"CUPS"
	start_daemon --lockfile "$LOCKFILE" --no-announce -- cupsd
        RETVAL=$?
	unset LD_PRELOAD
        return $RETVAL
}


stop()
{
	msg_stopping $"CUPS"
        stop_daemon --lockfile "$LOCKFILE" --no-announce -- cupsd
        RETVAL=$?
        return $RETVAL
}

restart()
{
	stop
	start
}


reload()
{
	msg_reloading $"CUPS"
	stop_daemon --pidfile "$PIDFILE" --expect-user root -HUP -- cupsd
	RETVAL=$?
	return $RETVAL
} 


case "$1" in
    start)
	    start
	    ;;
    stop)
	    stop
    	    ;;
    restart)
    	    restart
	    ;;
    reload)
    	    reload
	    ;;
    condstop)
    	    if [ -e "$LOCKFILE" ]; then
    	    stop
    	    fi                                                                                
    	    ;;  
    condrestart)
	    if [ -e "$LOCKFILE" ]; then
	    restart
	    fi
	    ;;
    condreload)
	    if [ -e "$LOCKFILE" ]; then
	    reload
	    fi
	    ;;
    status)
    	    status cupsd
	    RETVAL=$?
    	    ;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
	RETVAL=1
esac

exit $RETVAL
