#!/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


fix_perms()
{
	chown root:lp /etc/cups
	chmod 750 /etc/cups

	chown root:root /etc/cups/*
	
	chown root:lp /etc/cups/*.conf
	chmod 640 /etc/cups/*.conf
	
	chown root:lp /etc/cups/ppd
	chmod 750 /etc/cups/ppd
	
	chown root:lp /etc/cups/{ssl,certs}
	chmod 2770 /etc/cups/{ssl,certs}
	
	chown root:lp /var/spool/cups
	chmod 3770 /var/spool/cups
	
	chown -R root:lp /var/log/cups
	chmod 3770 /var/log/cups

	find /var/log/cups -type f -name \*_log -print0 |
	    xargs -r0 chmod 664 -- 
	find /var/log/cups -type f -name \*_log -print0 |
	    xargs -r0 chown root:lp -- 
}

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()
{
	fix_perms
	prepare_loopback
	msg_starting $"CUPS"
	export LD_PRELOAD=/usr/lib/libnoch.so
	start_daemon --lockfile "$LOCKFILE" --no-announce -- cupsd -u
        RETVAL=$?
	unset LD_PRELOAD
        return $RETVAL
}

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

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

restart()
{
	stop
	start
}

admrestart()
{
	stop
	admstart
}

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

exit $RETVAL
