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