#!/bin/sh

PROG="${0##*/}" #program name

exit_handler()
{
	local rc=$?
	trap - 0
	rm -rf -- $TEMPORARY_PPD
	exit $rc
}

trap exit_handler 0 1 2 3 9 13


print_version()
{
	cat <<EOF
$PROG version @VERSION@

Written by Stanislav Ievlev

Copyright (C) 2003-2004 ALT Linux Team
EOF
	exit
}


print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] 

alterator backend to configure CUPS

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
  -l, --list	get list of available options
  -t, --type	check for existance of some object
  -d, --delete	delete some printer from configuration
  -r, --read	read driver information
  -w, --write	modify some scheduller information

Report bugs to <inger@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

LPSTAT="/usr/bin/lpstat"
LPADMIN="/usr/sbin/lpadmin"
LPR="/usr/bin/lpr"
FOOMATIC_PPD_GENERATOR="/usr/bin/foomatic-ppdfile"

wait_cups()
{
	while ! "$LPSTAT" -r >/dev/null 2>&1 ; do sleep 1 ;done
}

printer_name()
{
	echo "$1"|sed 's,^printers/,,'
}

is_printer()
{
	echo "$1"|/bin/grep -qs "^printers/"
}

is_option()
{
	echo $1|/bin/egrep "^$2:$3\$"
}

are_printer_exists()
{
	"$LPSTAT" -a|/bin/grep -qs "^$1"
}

TEMP=`getopt -n $PROG -o h,v,l:,d:,r:,w:,t: -l help,version,list:,delete:,read:,write:,type: -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-v|--version) print_version
			;;
		-l|--list)
			shift;dir=$1;
			if [ $dir = "/" ] ;then
				echo "server r"
				echo "test_page r"
				echo "printers d"
			else
				#NOTE: recheck in new versions for spaces in printer names
				#      current cups version doesn't allow spaces in printer names
				"$LPSTAT" -a|cut -f1 -d' '|/bin/sed 's,.*,& r,'
			fi
			;;
		-d|--delete)
			#ignore
			shift;object=$1;
			if is_printer $object;then
				prn_name=$(printer_name $object)
				"$LPADMIN" -x "$prn_name"
				are_printer_exists "$prn_name" || exit 0
				exit 1
			fi
			;;
		-t|--type)
			shift;object=$1;
			if [ "$object" = "test_page" ];then
				echo "test_page r"
			elif [ "$object" = "server" ];then
				echo "server r"
			elif [ "$object" = "printers" ];then
				echo "printers d"
			elif is_printer $object;then
				prn_name=$(printer_name $object)
				are_printer_exists "$prn_name" && echo "$prn_name r"
			fi
			;;
		-r|--read)
			shift;object=$1;
			if [ "$object" = "server" ];then
				#print server related options
				#I have no chances to guess current printer mode: izvrat or not
				#I can only change printer status via "restart/admrestart"
				:
			elif is_printer $object;then
				#printer options
				prn_name=$(printer_name $object)
				#is a default printer
				if "$LPSTAT" -d|/bin/grep -qs "[[:space:]]$prn_name\$";then
					echo "default:yes"
				else
					echo "default:no"
				fi
				if "$LPSTAT" -p $prn_name|/bin/grep -qs 'enabled';then
					echo "state:started"
				else
					echo "state:stopped"
				fi
				"$LPSTAT" -v $prn_name|
					/bin/sed 's,device for[^:]*:[[:space:]]*,,'|
					/bin/sed 's,.*,uri:&,'
				cat /etc/cups/ppd/$prn_name.ppd|
					grep '^*NickName:'|
					sed 's,^*NickName[[:space:]]*:[[:space:]]*,,'|
					sed 's,^\",,'|sed 's,\"$,,'|
					sed 's,.*,info:&,'
			fi
			;;
		-w|--write)
			#ignore
			shift;object=$1;
			if [ "$object" = "test_page" ];then
				read n
				printer_name=$(echo "$n"|sed 's,^printer:,,')
				"$LPR" -P $printer_name /usr/share/printer-testpages/testprint.ps
			elif [ "$object" = "server" ];then
				#configure server related options
				while read l; do
					option=${l%%:*}
					value=${l#*:}
					case $option in
						mode)
							if [ "$value" = "admin" ];then
								/sbin/service cups admrestart
							else
								/sbin/service cups restart
							fi
							wait_cups
							;;
						*)
							;;
					esac
				done
			elif is_printer $object;then
				prn_name=$(printer_name $object)
				cmd1=
				cmd2=
				while read l;do
					option=${l%%:*}
					value=${l#*:}
					case $option in
						#to second queue
						default)
							if [ "$value" = "yes" ];then
								cmd2="$cmd2 /usr/sbin/lpadmin -d $prn_name;"
							fi
							;;
						state)	if [ "$value" = "started" ];then
								cmd2="$cmd2 /usr/bin/enable $prn_name;"
							elif [ "$value" = "stopped" ];then
								cmd2="$cmd2 /usr/bin/disable $prn_name;"
							fi
							;;
						ppd)    if echo $value|grep '^/';then
								ppdfile="$value"
							else
								TEMPORARY_PPD=`mktemp -t $PROG.ppd.XXXXXX`
								ppdfile="$TEMPORARY_PPD"
								"$FOOMATIC_PPD_GENERATOR" -p "$value" >$ppdfile
							fi
							cmd2="$cmd2 /usr/sbin/lpadmin -p $prn_name -P $ppdfile;"
							;;
						uri)	cmd1="/usr/sbin/lpadmin -p $prn_name -v $value"
							;;
						*)
							;;
					esac
				done
				[ -n "$cmd1" ] && eval "$cmd1"
				[ -n "$cmd2" ] && eval "$cmd2"
				are_printer_exists "$prn_name" || exit 1
				exit 0
			fi
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done
