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