#!/bin/sh
#
# cups-browsed - Make remote CUPS printers available locally
#
# chkconfig:   2345 30 5
# description: Startup/shutdown script for cups-browsed
# processname: cups-browsed

# http://fedoraproject.org/wiki/FCNewInit/Initscripts
### BEGIN INIT INFO
# Provides:          cups-browsed
# Required-Start:    $network $local_fs $remote_fs $cups
# Required-Stop::    $network $local_fs $remote_fs $cups
# Should-Start:      $avahi-daemon
# Should-Stop:       $avahi-daemon
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: cups-browsed - Make remote CUPS printers available locally
# Description: This daemon browses Bonjour broadcasts of shared remote CUPS
#              printers and makes these printers available locally by creating
#              local CUPS queues pointing to the remote queues. This replaces
#              the CUPS browsing which was dropped in CUPS 1.6.1. For the end
#              the behavior is the same as with the old CUPS broadcasting/
#              browsing, but in the background the standard method for network
#              service announcement and discovery, Bonjour, is used.
### END INIT INFO

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

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

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

conf="/etc/cups/cups-browsed.conf"
exec="/usr/sbin/cups-browsed"
prog="cups-browsed"
[ -x ${exec} ] || exit 0

pidfile="${PIDFILE-/var/run/cups-browsed.pid}"
lockfile="${LOCKFILE-/var/lock/subsys/cups-browsed}"
args="--autoshutdown=Off -c ${conf} &"
pid="$(pidof ${exec})"

[ -e "/etc/sysconfig/${prog}" ] && source /etc/sysconfig/${prog}

RETVAL=0

# See how we were called.
case "$1" in
  start)
        [ -e "${lockfile}" ] && exit 0
        echo -n "Starting ${prog}: "
        daemon "${exec}" "${args}"
        RETVAL=$?
        echo
        [ ${RETVAL} -eq 0 ] && touch "${lockfile}"
        [ ! -z "${pid}" ] && echo "${pid}" > "${pidfile}"
        ;;
  stop)
        echo -n "Stopping ${prog}: "
        [ ! -z "${pid}" ] && echo "${pid}" > "${pidfile}"
        killproc -p "${pidfile}" "${exec}"
        RETVAL=$?
        echo
        [ ${RETVAL} -eq 0 ] && rm -f "${lockfile}"
        ;;
  status)
        [ ! -z "${pid}" ] && echo "${pid}" > "${pidfile}"
        status -p "${pidfile}" "${exec}"
        RETVAL=$?
        ;;
  reload)
        echo -n "Reloading ${prog}: "
        [ ! -z "${pid}" ] && echo "${pid}" > "${pidfile}"
        killproc -p "${pidfile}" "${exec}" -HUP
        RETVAL=$?
        echo
       ;;
  restart)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
  condrestart)
        [ ! -z "${pid}" ] && echo "${pid}" > "${pidfile}"
        if [ -f "${lockfile}" ] && [ -f "${pidfile}" ] ; then
        $0 stop
        $0 start
        RETVAL=$?
        fi
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload|condrestart|status}"
        exit 1
esac

exit ${RETVAL}

