b1d80f
#!/bin/bash
b1d80f
b1d80f
## Copyright (C) 2010 Red Hat, Inc.
b1d80f
## Authors:
b1d80f
##  Tim Waugh <twaugh@redhat.com>
b1d80f
b1d80f
## This program is free software; you can redistribute it and/or modify
b1d80f
## it under the terms of the GNU General Public License as published by
b1d80f
## the Free Software Foundation; either version 2 of the License, or
b1d80f
## (at your option) any later version.
b1d80f
b1d80f
## This program is distributed in the hope that it will be useful,
b1d80f
## but WITHOUT ANY WARRANTY; without even the implied warranty of
b1d80f
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b1d80f
## GNU General Public License for more details.
b1d80f
b1d80f
## You should have received a copy of the GNU General Public License
b1d80f
## along with this program; if not, write to the Free Software
b1d80f
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
b1d80f
b1d80f
## Purpose: Update hpcups PPDs when necessary.
b1d80f
b1d80f
sock=/var/run/cups/cups.sock
b1d80f
running=$(LC_ALL=C lpstat -h "$sock" -r 2>/dev/null)
b1d80f
if [ "$?" -ne 0 ]
b1d80f
then
b1d80f
    # No lpstat in path
b1d80f
    exit 0
b1d80f
fi
b1d80f
b1d80f
if [ -z "${running##*not*}" ]
b1d80f
then
b1d80f
    # scheduler is not running
b1d80f
    exit 0
b1d80f
fi
b1d80f
b1d80f
trap 'rm -f "$tmpdir"/models; rmdir "$tmpdir"; exit 0' \
b1d80f
    0 HUP INT QUIT ILL ABRT PIPE TERM
b1d80f
b1d80f
debug=true
b1d80f
tmpdir="$(mktemp -d)"
b1d80f
for ppd in /etc/cups/ppd/*.ppd
b1d80f
do
b1d80f
    [ -r "$ppd" ] || continue
b1d80f
    queue="${ppd#/etc/cups/ppd/}"
b1d80f
    queue="${queue%.ppd}"
b1d80f
    lpstat -h "$sock" -p "$queue" &>/dev/null || continue
b1d80f
b1d80f
    # We have PPD associated with a queue.  Find out its NickName
b1d80f
    $debug && echo "Examining $queue"
b1d80f
    nickname="$(grep '^\*NickName:' "$ppd")"
b1d80f
    nickname="${nickname#*\"}" # strip text up to and incl first double quote
b1d80f
    nickname="${nickname%\"*}" # strip final double quote
b1d80f
    $debug && echo "NickName is: $nickname"
b1d80f
b1d80f
    # Is it an hpcups PPD?
b1d80f
    [ -z "${nickname##*, hpcups*}" ] || continue
b1d80f
    $debug && echo "hpcups: true"
b1d80f
b1d80f
    # No: need to regenerate the PPD.
b1d80f
    if [ ! -f "$tmpdir/models" ]
b1d80f
    then
b1d80f
	# Get list of driver URIs and NickNames
b1d80f
	lpinfo -h "$sock" --include-schemes=drv -m 2>/dev/null >"$tmpdir/models"
b1d80f
    fi
b1d80f
b1d80f
    # Strip hpcups version from NickName
b1d80f
    nickname="${nickname%, hpcups*}"
b1d80f
    $debug && echo "Stripped NickName: $nickname"
b1d80f
    while read line
b1d80f
    do
b1d80f
	uri=${line%% *}
b1d80f
	nn="${line#$uri }"
b1d80f
	[ -z "${nn##*, hpcups*}" ] || continue
b1d80f
b1d80f
	nn="${nn%, hpcups*}"
b1d80f
	if [ "$nn" == "$nickname" ]
b1d80f
	then
b1d80f
	    $debug && echo "Match found, URI: $uri"
b1d80f
b1d80f
	    # Unfortunately CUPS will reset the page size when we
b1d80f
	    # change the PPD, due to the weird page size names that
b1d80f
	    # HPLIP uses.  Try to maintain the existing page size.
b1d80f
	    size="$(grep '^\*DefaultPageSize:' "$ppd")"
b1d80f
	    size="${size##* }" # strip until after first ' '
b1d80f
	    size="${size%% *}" # strip after any ' '
b1d80f
	    $debug && echo "PageSize is $size"
b1d80f
b1d80f
	    if [ -z "${size#*Duplex}" ]
b1d80f
	    then
b1d80f
		# Special handling for duplex sizes because HPLIP
b1d80f
		# broke backwards compatibility with *that* too!
b1d80f
		size="${size%Duplex}.Duplex"
b1d80f
	    fi
b1d80f
b1d80f
	    null=/dev/null
b1d80f
	    $debug && null=/dev/stdout
b1d80f
	    lpadmin -h "$sock" -p "$queue" -m "$uri" &>"$null" || :
b1d80f
	    $debug && echo "PPD regenerated"
b1d80f
b1d80f
	    lpadmin -h "$sock" -p "$queue" -o PageSize="$size" &>"$null" || :
b1d80f
	    $debug && echo "PageSize restored to $size"
b1d80f
	    break
b1d80f
	fi
b1d80f
    done <"$tmpdir/models"
b1d80f
done
b1d80f
exit 0