#!/bin/sh
# Startup script for the CUPS(Common Unix Printing System)
#
# chkconfig: 235 80 00
# description: Startup/shutdown script for the Common UNIX \
#                Printing System (CUPS).
# processname: cupsd
# pidfile /var/run/cups.pid
# securlevel: 80

# Source function library
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
fi

if [ -f /etc/sysconfig/i18n ]; then
. /etc/sysconfig/i18n
fi

DAEMON=cupsd
CUPS_LOCK=/var/lock/subsys/cups
PPD_DATA=/etc/cups/ppds.dat
CONF=/etc/cups/cupsd.conf
LANGUAGE=`echo ${LANG} |sed -e "s/@.*//;s/\..*//;s/_.*//"`

# setLanguage(){
#   if test -f ${CONF} -a -n ${LANGUAGE} ; then
#     if test `grep -c "^#DefaultLanguage" ${CONF}` -gt 0; then
#       perl -pi -e "s/^#DefaultLanguage [a-z]+/DefaultLanguage ${LANGUAGE}/g" ${CONF}
#     fi
#   fi
# }

cups_start(){
  echov -n "Starting ${DAEMON}: "
  if [ ! -s ${PPD_DATA} ]; then
    ${DAEMON} < /dev/zero > /dev/null 2>&1 &
    usleep 1000
    ret=`pidof ${DAEMON}`
    if [ "${ret}" != "" ]; then
       action "" /bin/true
       touch ${CUPS_LOCK}
    fi
  else
    daemon ${DAEMON}
    [ "$?" = "0" ] && touch ${CUPS_LOCK}
    echov
  fi
}

cups_stop(){
  echov -n "Shutting down ${DAEMON}: "
  killproc ${DAEMON}
  [ "$?" = "0" ] && rm -f ${CUPS_LOCK}
  echov
}

cups_reload(){
  echo -n "Reloading ${DAEMON}: "
  killproc ${DAEMON} -HUP
  val=$?
  if [ $val -ne 0 ]; then
    ${DAEMON} --ppdsdat
  fi
}

# See how we are called
case $1 in
    start)
      cups_start
      ;;

    stop)
      cups_stop
      ;;

    status)
      status ${DAEMON}
      ;;

    restart)
      cups_stop
      cups_start
      ;;

    condrestart)
      if [ -f ${CUPS_LOCK} ]; then
        cups_stop
        cups_start
      fi
      ;;

    reload)
      cups_reload
      ;;

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