Sophie

Sophie

distrib > Mageia > 2 > i586 > by-pkgid > b0128867e46fac0ffcbb2c38e87b9c6e > files > 13

cups-1.5.2-5.mga2.i586.rpm

#!/bin/bash

# Init file for the CUPS server daemon
#
# chkconfig: 2345 15 60
# 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 Mandriva \
#              Linux.
#
# 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
#
### BEGIN INIT INFO
# Provides: cups
# Required-Start:
# Required-Stop:
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
# Short-Description: CUPS printing server daemon
# 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 Mandriva
#              Linux.
### END INIT INFO

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

# default printing auto admin settings
ENABLE_QUEUES_ON_SPOOLER_START=no

# source printing auto admin configuration
if [ -r /etc/sysconfig/printing ]; then
    . /etc/sysconfig/printing
fi

# Source an auxiliary options file if we have one, and pick up OPTIONS
# CUPS can now play nice (use NICELEVEL=<number> in /etc/sysconfig/cups)
if [ -r /etc/sysconfig/cups ] ; then
	. /etc/sysconfig/cups
fi

# Don't use TMPDIR environment variable from init script, as that can
# cause cupsd to set TempDir to a user's temporary directory instead
# of the default...
#

unset TMPDIR

RETVAL=0

case "$1" in
  start)
        # Do some checks, I hope they make CUPS really idiot-proof now ...

	# CUPS needs the loopback device to run correctly, make sure that
	# it is running and abort the CUPS startup when the loopback device
	# cannot be started
	if ! (export LC_ALL=C; /sbin/ifconfig | /bin/egrep "^lo +[^ ]+.*Loopback" > /dev/null 2>&1); then
	    gprintf "Loopback device ('lo', 127.0.0.1) needed by CUPS, starting it ...\n"
	    /sbin/ifconfig lo 127.0.0.1 > /dev/null 2>&1
	    RETVAL=$?
	    if [ $RETVAL -ne 0 ]; then
		echo
		gprintf "Cannot start loopback device, start of CUPS aborted"
	    fi
	fi
	if [ $RETVAL -eq 0 ]; then
	    # Add the "route" entry for the loopback device if it is missing
	    if ! (export LC_ALL=C; /sbin/route -n | /bin/egrep "^127\.0\.0\.0 +.* +lo$" > /dev/null 2>&1); then
		gprintf "Adding loopback device to routing table ...\n";
		/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
		if [ $? -ne 0 ]; then
		    echo
		    gprintf "WARNING: Could not add loopback device to routing table,\n"
		    gprintf "         CUPS may not work properly.\n"
		    echo
		fi
	    fi
	    # Check, if we have a real network, if not, let the hostname be
	    # "localhost"
	    #if ! (export LC_ALL=C; /sbin/ifconfig | /bin/egrep -v "^lo +[^ ]+.*Loopback" | /bin/egrep "^[a-zA-Z0-9]" > /dev/null 2>&1); then
		#/bin/hostname localhost
	    #fi
	    # Check whether the loopback device entry (127.0.0.1) in the
	    # /etc/hosts file is correct
	    # Is there an /etc/hosts file?
	    #if (! [ -f /etc/hosts ]); then
	    #    gprintf "Creating /etc/hosts ...\n";
	    #    touch /etc/hosts
	    #fi
	    # Is there a correct "localhost" line?
	    #if ! (/bin/egrep "^127\.0\.0\.1[[:space:]]+localhost\.localdomain[[:space:]]+localhost" /etc/hosts > /dev/null 2>&1); then
	    #    gprintf "Correcting 'localhost' line in /etc/hosts ...\n";
	    #    # Correct "localhost" line with wrong IP
	    #    perl -p -i -e "s/^\s*[\d\.]+\s+.*localhost.*$/127.0.0.1\t\tlocalhost.localdomain\tlocalhost/" /etc/hosts
	    #    # Correct line with address "127.0.0.1" but wrong name
	    #    perl -p -i -e "s/^\s*127.0.0.1\s+.*$/127.0.0.1\t\tlocalhost.localdomain\tlocalhost/" /etc/hosts
	    #    # Add "localhost" line if missing
	    #    if ! (/bin/egrep "^127\.0\.0\.1" /etc/hosts > /dev/null 2>&1); then
	    #        echo -en "127.0.0.1\t\tlocalhost.localdomain\tlocalhost\n" >> /etc/hosts
	    #    fi
	    #fi
	    # Turn off the CUPS-LPD mini-daemon when LPD is running
	    if [ -x /etc/rc.d/init.d/lpd ]; then
		if (export LC_ALL=C; /sbin/service lpd status | /bin/egrep "running" > /dev/null 2>&1); then
		    if (export LC_ALL=C; /sbin/chkconfig --list cups-lpd | /bin/egrep "on$" > /dev/null 2>&1); then
			gprintf "Turning off CUPS-LPD mini daemon ...\n"
			/sbin/chkconfig --del cups-lpd
			if [ -x /usr/sbin/xinetd ]; then
			    /sbin/service xinetd condrestart
			fi
		    fi
		fi
	    fi
	    # Check whether a parallel printer is configured and if
	    # so, but if the parallel printer kernel module not being
	    # loaded, load the module.
	    if (/bin/egrep "^[^#]*/dev/lp" /etc/cups/printers.conf > /dev/null 2>&1); then
		if (! (export LC_ALL=C; /sbin/lsmod | /bin/egrep "^lp +" > /dev/null 2>&1) || \
		    ! (export LC_ALL=C; /sbin/lsmod | /bin/egrep "^parport_pc +" > /dev/null 2>&1)); then
		    gprintf "Loading parallel port printer kernel modules ...\n"
		    modprobe parport_pc > /dev/null 2>&1;
		    RET=$?
		    if [ $RET -eq 0 ]; then
			modprobe lp > /dev/null 2>&1;
			RET=$?
		    fi
		    if [ $RET -ne 0 ]; then
			echo
			gprintf "WARNING: Parallel printer kernel modules could not be loaded, your parallel\n"
			gprintf "         printer may not work.\n"
			echo
		    fi
		fi
	    fi
	    # Check whether a USB printer is configured and if so, but
	    # if the USB printer kernel module not being loaded, load
	    # the module.
	    # DEPRECATED: the usblp module does not exist anymore
	    #if (/bin/egrep "^[^#]*DeviceURI.*usb" /etc/cups/printers.conf > /dev/null 2>&1); then
		#if ! (export LC_ALL=C; /sbin/lsmod | /bin/egrep "^(printer|usblp) +" > /dev/null 2>&1); then
		    #gprintf "Loading USB printer kernel module ...\n"
		    #modprobe printer usblp > /dev/null 2>&1;
		    #if [ $? -ne 0 ]; then
			#echo
			#gprintf "WARNING: USB printer kernel module could not be loaded, your USB printer may\n"
			#gprintf "         not work.\n"
			#echo
		    #fi
		#fi
	    #fi
	    # Check whether an HP printer is configured with HPLIP and
	    # if so, but if the HPLIP daemons not being running, start
	    # the daemons.
	    #if (/bin/egrep "^[^#]*hp:/" /etc/cups/printers.conf > /dev/null 2>&1); then
		#if ! (/bin/ps auxwww | /bin/grep -v "grep" | /bin/grep "hpssd" > /dev/null 2>&1); then
		    #gprintf "Starting HPLIP daemons ...\n"
		    #chkconfig --add hplip
		    #/sbin/service hplip start
		#fi
	    #fi
	    # Check whether an HP multifunction device is configured
	    # with HPOJ and if so, but if the HPOJ daemons not being
	    # running, start the daemons.
	    #if (/bin/egrep "^[^#]*/dev/ptal-" /etc/cups/printers.conf > /dev/null 2>&1 ||
	        #/bin/egrep "^[^#]*ptal:/" /etc/cups/printers.conf > /dev/null 2>&1); then
		#if ! (/bin/ps auxwww | /bin/grep -v "grep" | /bin/grep "ptal-" > /dev/null 2>&1); then
		    #gprintf "Starting HPOJ daemons ...\n"
		    #chkconfig --add hpoj
		    #/sbin/service hpoj start
		#fi
	    #fi
	    # Check whether an OKI winprinter is configured with and if so,
	    # but if the oki4daemon not being running, start the oki4daemon.
	    if (/bin/egrep "^[^#]*/dev/oki4drv" /etc/cups/printers.conf > /dev/null 2>&1); then
		if ! (/bin/ps auxwww | /bin/grep -v "grep" | /bin/grep "oki4daemon" > /dev/null 2>&1); then
		    gprintf "Starting oki4daemon ...\n"
		    chkconfig --add oki4daemon
		    /sbin/service oki4daemon start
		fi
	    fi

	    # Do automatic correction of CUPS configuration to avoid
	    # /etc/printcap from LPD/LPRng being overwritten and also
	    # to avoid printer info with hostname "localhost" being
	    # broadcasted. Can be turned off in printerdrake
	    if [ -x /usr/sbin/correctcupsconfig ]; then
	        /usr/sbin/correctcupsconfig
	    fi
	    gprintf "Starting CUPS printing system: "
	    portrelease cups
	    daemon cupsd
	    RETVAL=$?
	    
	    echo
	    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/cups

	    # Do the rest only if printerdrake or autosetupprintqueues
	    # is NOT currently running
	    ps auxwww | grep 'perl /usr/sbin/autosetupprintqueues' | grep -vq grep && exit $RETVAL
	    ps auxwww | grep 'perl /usr/sbin/printerdrake' | grep -vq grep && exit $RETVAL

	    # CUPS daemon must be listening for the following steps
	    if (ps auxwww | grep cupsd | grep -v grep > /dev/null 2>&1); then
		if ! (lpstat -r > /dev/null 2>&1); then
		    gprintf "Waiting for the CUPS daemon getting ready\n"
		    for ((i=0; i < 30; i ++)); do
			if (lpstat -r > /dev/null 2>&1); then 
			    break; 
			fi;
			sleep 1;
			gprintf ".\n";
		    done;
		    gprintf "\n"
		fi
		if ! (lpstat -r > /dev/null 2>&1); then
		    gprintf "WARNING: CUPS daemon still not listening after 30 sec, aborting auto-admin tasks.\n"
		    exit $RETVAL
		fi;
	    fi

	    if [ "$ENABLE_QUEUES_ON_SPOOLER_START" == "yes" ]; then
	        # Re-enable disabled print queues
		gprintf "Re-enabling disabled print queues:\n"
		for printer in `lpstat -p | grep disabled | cut -d ' ' -f 2`; do
		    if cat /etc/cups/printers.conf | egrep -q "<(Default|)Printer $printer>"; then
		        #if (( ! `lpstat -p $printer | grep -c Paused` )); then 
			gprintf "  Printer: $printer\n";
			/usr/bin/enable $printer
		        #fi
		    fi
		done
	    fi

	else
	    echo_failure
	    echo
	fi
        ;;
  stop)
        gprintf "Stopping CUPS printing system: "
        killproc cupsd
        RETVAL=$?

        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/cups
        ;;
  restart)
        $0 stop
        $0 start
	RETVAL=$?
        ;;
  status)
        status cupsd
	RETVAL=$?
        ;;
  condrestart)
        if [ -f /var/lock/subsys/cups ]; then
	    $0 stop
	    $0 start
	    RETVAL=$?
	fi
	;;
  reload)
        gprintf "Reinitializing CUPS printing system: "
        killproc cupsd -HUP
        RETVAL=$?

        echo
        ;;
  *)
	gprintf "Usage: cups {start|stop|restart|reload|status|condrestart}\n"
	exit 1
esac

exit $RETVAL