#!/bin/sh
#
# lpd           This shell script takes care of starting and stopping
#               lpd (printer daemon).
#
# chkconfig: 2345 60 60
# description: lpd is the print daemon required for lpr to work properly. \
#   It is basically a server that arbitrates print jobs to printer(s).
# processname: lpd
# config: /etc/printcap

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

# Source networking configuration.
#. /etc/sysconfig/network

# Check that networking is up.
#[ ${NETWORKING} = "no" ] && exit 0

[ -f /usr/sbin/lpd ] || exit 0

[ -f /etc/printcap ] || exit 0

RETVAL=0

# Override locales which break the output of the network tools,
# To satisfy Perl all variables have to be set and not only
# LC_MESSAGES
export LANG=C
export LANGUAGE=C
export LC_CTYPE=C
export LC_NUMERIC=C
export LC_TIME=C
export LC_COLLATE=C
export LC_MONETARY=C
export LC_MESSAGES=C
export LC_PAPER=C
export LC_NAME=C
export LC_ADDRESS=C
export LC_TELEPHONE=C
export LC_MEASUREMENT=C
export LC_IDENTIFICATION=C
export LC_ALL=

# See how we were called.
case "$1" in
  start)
    # Do some checks, I hope they make LPD really idiot-proof now ...

    # LPD needs the loopback device to run correctly, make sure that
    # it is running and abort the LPD startup when the loopback device
    # cannot be started
    if !(/sbin/ifconfig | /bin/egrep "^lo +[^ ]+.*Loopback" > /dev/null 2>&1); then
	echo "Loopback device (\"lo\", 127.0.0.1) needed by LPD, 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 LPD aborted"
	fi
    fi
    if [ $RETVAL -eq 0 ]; then
	# Add the "route" entry for the loopback device if it is missing
	if !(/sbin/route -n | /bin/egrep "^127\.0\.0\.0 +.* +lo$" > /dev/null 2>&1); then
	    echo "Adding loopback device to routing table ...";
	    /sbin/route add -net 127.0.0.0 netmask 255.0.0.0 dev lo
	    if [ $? -ne 0 ]; then
		echo
		echo "WARNING: Could not add loopback device to routing table,"
		echo "         LPD may not work properly."
		echo
	    fi
	fi
	# Check, if we have a real network, if not, let the hostname be
        # "localhost"
	#if !(/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
	#    echo "Creating /etc/hosts ...";
	#    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
	#    echo "Correcting \"localhost\" line in /etc/hosts ...";
	#    # 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
	# Check if CUPS is running and if it automatically creates
	# an /etc/printcap file, restart CUPS so that it reconfigures itself
	# to not create an /etc/printcap file
	if [ -x /usr/sbin/cupsd -a -r /etc/cups/cupsd.conf ]; then
	    if ((!((/bin/egrep "^[[:space:]]*Printcap$" /etc/cups/cupsd.conf > /dev/null 2>&1) ||
                  (/bin/egrep "^[[:space:]]*Printcap[[:space:]]+" /etc/cups/cupsd.conf > /dev/null 2>&1))) ||
		(/bin/egrep "^[[:space:]]*Printcap[[:space:]]+/etc/printcap[[:space:]]*$" /etc/cups/cupsd.conf > /dev/null 2>&1)); then
		 echo "Making CUPS not overwriting /etc/printcap ..."
		 /sbin/service cups condrestart
	    fi
	    if (/sbin/chkconfig --list cups-lpd | /bin/egrep "on$" > /dev/null 2>&1); then
		echo "Turning off CUPS-LPD mini daemon ..."
		/sbin/chkconfig --del cups-lpd
		if [ -x /usr/sbin/xinetd ]; then
		    /sbin/service xinetd condrestart
		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/printcap > /dev/null 2>&1); then
	    if (!(/sbin/lsmod | /bin/egrep "^lp +" > /dev/null 2>&1) || \
		!(/sbin/lsmod | /bin/egrep "^parport_pc +" > /dev/null 2>&1)); then
	        echo "Loading parallel port printer kernel modules ..."
		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
		    echo "WARNING: Parallel printer kernel modules could not be loaded, your parallel"
		    echo "         printer may not work."
		    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.
	if (/bin/egrep "^[^#]*/dev/usb" /etc/printcap > /dev/null 2>&1); then
	    if !(/sbin/lsmod | /bin/egrep "^printer +" > /dev/null 2>&1); then
		echo "Loading USB printer kernel module ..."
		modprobe printer > /dev/null 2>&1;
		if [ $? -ne 0 ]; then
		    echo
		    echo "WARNING: USB printer kernel module could not be loaded, your USB printer may"
		    echo "         not work."
		    echo
		fi
	    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/printcap > /dev/null 2>&1); then
	    if !(/bin/ps auxwww | /bin/grep -v "grep" | /bin/grep "ptal-" > /dev/null 2>&1); then
		echo "Starting HPOJ daemons ..."
		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/printcap > /dev/null 2>&1); then
	    if !(/bin/ps auxwww | /bin/grep -v "grep" | /bin/grep "oki4daemon" > /dev/null 2>&1); then
		echo "Starting oki4daemon ..."
		chkconfig --add oki4daemon
		/sbin/service oki4daemon start
	    fi
	fi
        # Start daemons.
        echo -n "Starting lpd: "
        daemon lpd
	RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/lpd
    else
	echo_failure
	echo
    fi
    ;;
  stop)
    # Stop daemons.
    echo -n "Shutting down lpd: "
    killproc lpd
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/lpd
    ;;
  status)
    status lpd
    RETVAL=$?
    ;;
  restart|reload)
    $0 stop
    $0 start
    RETVAL=$?
    ;;
  condrestart)
    # only restart if it is already running
    if [ -f /var/lock/subsys/lpd ]; then
	$0 stop
	$0 start
	RETVAL=$?
    fi
    ;;
  *)
    echo "Usage: lpd {start|stop|restart|condrestart|reload|status}"
    exit 1
esac

exit $RETVAL
