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