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