#! /bin/sh
# Copyright (c) 1995-2005 SUSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Klaus Singvogel.
# Please send feedback to http://www.suse.de/feedback/
#
#    This program is free software; you can redistribute it and/or modify 
#    it under the terms of the GNU General Public License as published by 
#    the Free Software Foundation; either version 2 of the License, or 
#    (at your option) any later version. 
# 
#    This program is distributed in the hope that it will be useful, 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
#    GNU General Public License for more details. 
# 
#    You should have received a copy of the GNU General Public License 
#    along with this program; if not, write to the Free Software 
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
### BEGIN INIT INFO
# Provides:          cupsrenice
# Required-Start:    cupsd
# Should-Start:      $ALL
# Default-Start:     5
# Default-Stop:      0 1 2 3 6
# Short-Description: renice cupsd after the kde ist running
# Description:       bring cups daemon back to normal IO process level
#                    after the graphical system is up and running. 
#                    This should result in a faster boot time
### END INIT INFO
# 
#FOO_BIN=/usr/sbin/FOO
#test -x $FOO_BIN || { echo "$FOO_BIN not installed"; 
#	if [ "$1" = "stop" ]; then exit 0;
#	else exit 5; fi; }
#
## Check for existence of needed config file and read it
#FOO_CONFIG=/etc/sysconfig/FOO
#test -r $FOO_CONFIG || { echo "$FOO_CONFIG not existing";
#	if [ "$1" = "stop" ]; then exit 0;
#	else exit 6; fi; }
#
## Read config	
#. $FOO_CONFIG

. /etc/rc.status

# Reset status of this service
rc_reset

find_and_renice_cups_procs()
{
	#    ...find process pid of cupsd and remember as parent pid
	ppid=`/bin/ps -A | awk '/cupsd/{print $1}'`
	#    if no cupsd is running, don't do further processing
	if [ -z "$ppid" ]; then
		return
	fi
	#    ...find sibling processes from daemon
	list1=`/bin/ps --ppid $ppid | /usr/bin/awk '/[0-9]/{printf "%s ", $1}'`
	#    ...find processes owned by "lp"
	list2=`/bin/ps -A -fl | /usr/bin/awk '{if ($3 ~ "lp") printf "%s ",$4}'`
	#    ...and renice them
	for i in `echo $list1 $list2 $ppid | awk '{for (i=1;i<=NF;i++){print $i}}'| sort -u`
	do
		/usr/bin/ionice -c 1 "$i"
	done
}

case "$1" in
    start)
	( sleep 40; find_and_renice_cups_procs ) &
	rc_status -v
	;;
    stop)
	rc_exit
	;;
    try-restart|condrestart)
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	rc_failed 3
	rc_status -v
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|status}"
	exit 1
	;;
esac
rc_exit
