#! /bin/sh

# ppd-inst.sh: smart (well, nearly) installation of PPD files.
#
# Yves Arrouye <Yves.Arrouye@marin.fdn.fr>, 1995-1996.

# This file has been generated from "$in". Edit the original preferrably!

names_size=auto		# Change if needed to long or short (if set to
			# auto, this script will try to guess the correct
			# length).

subst_rev_string=0	# You should not change that to one. Its effect
			# is to replace the PSVersion property of the
			# PPD files to match your version of Ghostscript.
			# This is bad as you can end up generating files
			# with a version number greater than the one really
			# needed.

# Don't change!

me=`basename $0`

sedscript=/tmp/$me.$$.sed
psfile=/tmp/$me.$$.ps
tmpdest=/tmp/$me.$$.tmp

trap "rm -f $sedscript $psfile $tmpdest; >&2 echo Interrupted..."

usage() {
    >&2 echo "usage: $me [ --dontlocalize ] [ --dontchange ] [ -l, --locales localespath ] [ -v, --verbose ] [ -p, --paper papersize ] [ -t, --testdir test-dir ] [ -d, --destdir install-dir ] ppd-file ..."
    exit 1
}

while test $# -ne 0
do
    case "$1" in
	-v|--verbose)
	    verbose=y
	    ;;
	-p|--paper)
	    test $# -gt 1 || usage
	    paper="`uppername $2`"
	    shift
	    ;;
	-d|--destdir)
	    test $# -gt 1 || usage
	    where="$2"
	    shift
	    ;;
	-l|--locales)
	    test $# -gt 1 || usage
	    locales="$2"
	    shift
	    ;;
	-t|--testdir)
	    test $# -gt 1 || usage
	    tdir="$2"
	    shift
	    ;;
	--dontlocalize)
	    dontlocalize=y
	    ;;
	--dontchange)
	    dontchange=y
	    ;;
	-*)
	    usage
	    ;;
	*)
	    files="$files $1"
	    ;;
    esac
    shift
done

if [ -z "$files" ]
then
    usage
fi

#

which() {
    prog=$1
    save_ifs="$IFS"; IFS=:
    set - ..:$PATH
    for d
    do
	if [ -f $d/$prog ]
	then
	    echo $d/$prog
	    break
	fi
    done
    IFS="$save_ifs"
    unset save_ifs prog
}

dirname() {
     _dir=`echo $1 | sed 's,/[^/]*$,,'`
    if [ "$_dir" = "$1" ]
    then
	echo .
    else
	echo $_dir
    fi
}

makeppddirs() {
    mkppddir=`which mkppddir`
 
    if [ ! -z "$mkppddir" ]
    then
	for d in $ppddirs
	do
	    $mkppddir "$d"
	done
    fi
}

#

if [ ! -z "$dontchange" ]
then
    for f in $files
    do
	dir="`dirname $f`"
	if [ "`echo \"$ppddirs\" | grep \" $dir \"`" = "" ]
	then
	    ppddirs="$ppddirs $dir"
	fi
    done
    makeppddirs
    exit 0
fi

nextstep=0

uname=`which uname`
if [ ! -z "$uname" ]
then
    case "`uname -s | tr '[a-z]' '[A-Z]'`" in
        *NEXT*)
	    nextstep=1;;
    esac
else
    hostinfo=`which hostinfo`
    if [ ! -z "$hostinfo" ]
    then
	if [ ! -z "`hostinfo | grep -i next`" ]
	then
	    nextstep=1
	fi
    fi
fi

ppdname() {
    if [ "$1" = "-b" ]
    then
	_base=yes
	shift
    fi
    for i
    do
	if [ ! -z "$_base" ]
	then
	    basename "$i" .ppd | sed -e 's,^[0-9],_,' -e 's,[^A-Za-z0-9],_,g'
	else
	    echo "$i" | sed -e 's,[ .],_,g' -e 's,/,%,g'
	fi
    done
    unset _base
}

uppername() {
    for i
    do
	case "$i" in
	    ??) echo $i | tr '[a-z]' '[A-Z]'
		;;
	    *)
		echo `echo $i | sed 's/^\(.\).*$/\1/' | tr '[a-z]' '[A-Z]'``
	    	    echo $i | sed 's/^.//' | tr '[A-Z]' '[a-z]'`
		;;
	esac
    done
}

papersubst() {
    kind=`basename "$2" .ppd`
    from=`dirname "$2"`
    if [ ! -z "$psplpr" ]
    then
        for p in `(PPDPATH=${from}:${PPDPATH}; export PPDPATH; $psplpr -z -k $kind | awk '
/^\*PageSize$/	{ pgseen = 1; next; }
pgseen && /^\*/	{ exit; }
pgseen > 1	{ print; }
pgseen		{ ++pgseen; }
')`
        do
	    if [ "$p" = "$1" ]
	    then
	        subst=$p
	        break
	    else
	        hashpagesize=yes
	    fi
        done
    else
	subst="$1"
    fi
    if [ ! -z "$subst" ]
    then
	sed -e 's,^\(\*Default.*: \)Letter$,\1'"$subst"',' \
	    <$3 >$tmpdest
        test -s $tmpdest && rm -f $3 && cp $tmpdest $3 && rm -f $tmpdest
    else
	if [ ! -z "$haspagesize" ]
	then
	    >&2 echo $me: cannot substitute default paper size \`$1\' in \`"$2"\'
	fi
    fi
    unset kind subst
}

mkdirs() {
    for i
    do
	case "$i" in
	    /*)
		root=;;
	    *)
		root=.;;
	esac
	for p in `echo "$i" | sed 's,/, ,g'`
	do
	    if test -d "$i"
	    then
		break
	    fi
	    if test ! -d $root/$p
	    then
		mkdir $root/$p || break
	    fi
	    root=$root/$p
	done
    done
}

gsdevice() {
    _gsdev=`grep '^\*% GhostscriptDevice:' $dest`
    if [ ! -z "$_gsdev" ]
    then
        _dev=`echo $_gsdev |
	    sed 's/^\*% GhostscriptDevice: \(.*[^ 	]\)[ 	]*/\1/'`

    	if [ "$_dev" =  "$_gsdev" ]
	then
	    _dev=unknown
	fi
	echo $_dev
    fi
}


papersize_gs=/tmp/$me.$$.ps

gspapersize() {
    if [ ! -f $papersize_gs ]
    then
        cat <<EOPS >$papersize_gs
%!

% papersize.ps: try to determine the paper size actually in use.
%
% Yves Arrouye <Yves.Arrouye@marin.fdn.fr>, 1996

% If we're lucky enough, PAGESIZE is defined under gs...

userdict /*no-papersize-check* known not {
    systemdict /PAPERSIZE known {
        PAPERSIZE print (\n) print flush quit
    } if
} if

% Now, determine the page size in points and round it to the next
% integer value.

deviceinfo /PageSize {
    get dup 0 get ceiling exch 1 get ceiling
} stopped {
    pop pop
    clippath pathbbox
    exch 4 1 roll exch sub ceiling
    3 1 roll sub ceiling exch
} if

% Now build an array of known paper sizes along with their physical
% dimensions (in points).
%   If we don't use gs, a built-in table is used (corresponding,
% actually, to the paper types defined in gs 3.68, with some
% exotic sizes).

statusdict /.pagetypenames { get } stopped {
    pop pop
    [
        [ /a3 [ 842 1190 ] ]
        [ /a4 [ 595 842 ] ]
        [ /letter [ 612 792 ] ]
        [ /note [ 612 792 ] ]
        [ /legal [ 612 1008 ] ]
        [ /executive [ 522 756 ] ]
        [ /halfletter [ 396 612 ] ]
        [ /halfexecutive [ 378 522 ] ]
        [ /11x17 [ 792 1224 ] ]
        [ /statement [ 396 612 ] ]
        [ /folio [ 612 936 ] ]
        [ /quarto [ 610 780 ] ]
        [ /10x14 [ 720 1008 ] ]
        [ /ledger [ 1224 792 ] ]
        [ /tabloid [ 792 1224 ] ]
        [ /a0 [ 2380 3368 ] ]
        [ /a1 [ 1684 2380 ] ]
        [ /a2 [ 1190 1684 ] ]
        [ /a5 [ 421 595 ] ]
        [ /a6 [ 297 421 ] ]
        [ /a7 [ 210 297 ] ]
        [ /a8 [ 148 210 ] ]
        [ /a9 [ 105 148 ] ]
        [ /a10 [ 74 105 ] ]
        [ /b0 [ 2836 4008 ] ]
        [ /b1 [ 2004 2836 ] ]
        [ /b2 [ 1418 2004 ] ]
        [ /b3 [ 1002 1418 ] ]
        [ /b4 [ 709 1002 ] ]
        [ /b5 [ 501 709 ] ]
        [ /c5 [ 459 659 ] ]
        [ /DL [ 312 624 ] ]
        [ /Comm10 [ 297 684 ] ]
        [ /Monarch [ 279 540 ] ]
        [ /archE [ 2592 3456 ] ]
        [ /archD [ 1728 2592 ] ]
        [ /archC [ 1296 1728 ] ]
        [ /archB [ 864 1296 ] ]
        [ /archA [ 648 864 ] ]
        [ /flsa [ 612 936 ] ]
        [ /flse [ 612 936 ] ]
        [ /csheet [ 1224 1584 ] ]
        [ /dsheet [ 1584 2448 ] ]
        [ /esheet [ 2448 3168 ] ]
    ]
} {
    [
	exch {
	    dup load dup 0 get exch 1 get [ 3 1 roll ] [ 3 1 roll ]
        } forall
    ]
} ifelse

% Now look for the paper name.

() exch {
    dup 1 get dup 0 get exch 1 get
    4 index eq exch 5 index eq and {
	0 get exch pop 20 string cvs exit
    } {
	pop
    } ifelse
} forall

print (\n) print flush

pop pop

quit
EOPS
     fi
     gs -q -sDEVICE="$1" $papersize_gs
}
rm -f $papersize_gs

localesrc() {
    if [ $nextstep -eq 1 ]
    then
	case $1 in
	    French*) echo fr;;
	    English*) echo en;;
	    German*) echo de;;
	    Italian*) echo it;;
	    Spanish*) echo es;;
	    Swedish*) echo sv;;
	esac
    else
	echo $1
    fi
}

localedest() {
    if [ $nextstep -eq 1 ]
    then
	case $1 in
	    fr*) echo French.lproj;;
	    en*) echo English.lproj;;
	    de*) echo German.lproj;;
	    it*) echo Italian.lproj;;
	    es*) echo Spanish.lproj;;
	    sv*) echo Swedish.lproj;;
	esac
    else
	echo $1
    fi
}


localelist() {
    echo 'en fr '
}

localetranslated() {
    case "$1" in
	en*) echo en;;
	fr*) echo fr;;
    esac
}

localesubst() {
    locsedscript=/tmp/$me.$$.loc.sed
    case "$1" in
	en*)
	    cat <<EOSED >$locsedscript
EOSED
	    sed -f $locsedscript <"$2"
	    rm -f $locsedscript
	    ;;

	fr*)
	    cat <<EOSED >$locsedscript
s,\(^\*LanguageVersion:\) English,\1 French,
s,\(^\*.*/\)Yes:,\1Oui:,
s,\(^\*.*/\)No:,\1Non:,
s,\(/[0-9][0-9]*\)\(dpi\),\1 \2,g
s,\([0-9][0-9]*\)\(%\),\1\2/\1 \2,g
s,\(/[0-9][0-9]*\)x\([0-9][0-9]*\),\1 x \2,g
s,\(^\*OpenUI \*Resolution\):,\1/R<e9>solution:,
s,\(^\*OpenUI .*/\)Page Size:,\1Taille de la page:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Letter:,\1Lettre am<e9>ricaine:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Note:,\1Lettre am<e9>ricaine:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Half Letter:,\1Demi lettre am<e9>ricaine:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Executive:,\1Courrier am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Half Executive:,\1Demi courrier am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Statement:,\1Missive am<e9>ricaine:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Quarto:,\1Quarto am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Folio:,\1Folio am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Legal:,\1L<e9>gal am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US 10x14:,\110" par 14":,
s,\(^\*[A-Z][A-Za-z]* .*/\)US 11x17:,\111" par 17":,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Tabloid:,\1Tablo<64>de am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)US Foolscap:,\1Foolscap am<e9>ricain:,
s,\(^\*[A-Z][A-Za-z]* .*/\)European Foolscap:,\1Foolscap europ<e9>en:,
s,\(^\*[A-Z][A-Za-z]* .*/\)C5 Envelope:,\1Enveloppe C5:,
s,\(^\*[A-Z][A-Za-z]* .*/\)DL Envelope:,\1Enveloppe DL:,
s,\(^\*[A-Z][A-Za-z]* .*/\)Comm10 Envelope:,\1Enveloppe commerciale:,
s,\(^\*[A-Z][A-Za-z]* .*/\)Monarch Envelope:,\1Enveloppe Monarch:,
s,\(^\*OpenUI .*/\)Page Region:,\1Zone de la page:,
s,\(^\*OpenUI .*/\)Media Source:,\1Alimentation papier:,
s,\(^\*InputSlot .*/\)Paper Tray:,\1Bac d'alimentation:,
s,\(^\*OpenUI .*/\)Manual Feed:,\1Alimentation manuelle:,
s,\(^\*OpenUI .*/\)Print Depth:,\1Profondeur d'impression:,
s,\(^\*OpenUI .*/\)Media Type:,\1Type de media:,
s,\(^\*MediaType .*/\)Card:,\1Carte:,
s,\(^\*MediaType .*/\)Coated Paper:,\1Papier couch<e9>:,
s,\(^\*MediaType .*/\)Envelope:,\1Enveloppe:,
s,\(^\*MediaType .*/\)Other:,\1Autre:,
s,\(^\*MediaType .*/\)Plain Paper:,\1Papier normal:,
s,\(^\*MediaType .*/\)Transparency Film:,\1Transparent:,
s,\(^\*OpenUI .*/\)Color Processing:,\1Mod<e8>le de couleur:,
s,\(^\*ColorModel .*/\)Gray:,\1Monochromie:,
s,\(^\*ColorModel .*/\)RGB:,\1Trichromie:,
s,\(^\*ColorModel .*/\)CMYK:,\1Quadrichromie:,
s,\(^\*OpenUI .*/\)Thick Media:,\1Media <e9>pais:,
s,\(^\*OpenUI .*/\)Print Colors:,\1Couleurs d'impression:,
s,\(^\*PrintColors .*/\)None:,\1Aucune:,
s,\(^\*PrintColors .*/\)Cyan:,\1Cyan:,
s,\(^\*PrintColors .*/\)Magenta:,\1Magenta:,
s,\(^\*PrintColors .*/\)Yellow:,\1Jaune:,
s,\(^\*PrintColors .*/\)Black:,\1Noir:,
s,\(^\*PrintColors .*/\)All:,\1Toutes:,
s,\(^\*OpenUI .*/\)Monochrome Print:,\1Impression monochrome:,
s,\(^\*OpenUI .*/\)Print Quality:,\1Qualit<e9> d'impression:,
s,\(^\*OutputMode Draft\):,\1/Epreuve:,
s,\(^\*OutputMode Low\):,\1/Inf<e9>rieure:,
s,\(^\*OutputMode Normal\):,\1/Normale:,
s,\(^\*OutputMode High\):,\1/Haute:,
s,\(^\*ScreenProc Dot\):,\1/Point:,
s,\(^\*ScreenProc Ellipse\):,\1/Ellipse:,
s,\(^\*ScreenProc Line\):,\1/Ligne:,
s,\(^\*Transfer Normalized\):,\1/Normalis<e9>:,
s,\(^\*Transfer Null\):,\1/Nul:,
s,\(^\*Transfer Null.Inverse\):,\1/Nul invers<e9>:,
s,\(^\*ParamCustomGamma Gamma/\)Gamma Correction:,\1Correction gamma:,
s,\(^\*ParamCustomNumCopies NumCopies/\)Number of Copies:,\1Nombre de copies:,
s,\(^\*ParamCustomPageSize Width\):,\1/Largeur:,
s,\(^\*ParamCustomPageSize Height\):,\1/Hauteur:,
s,\(^\*ParamCustomPageSize Orientation\):,\1:,
s,\(^\*ParamCustomPageSize WidthOffset\):,\1D<29>calage horizontal:,
s,\(^\*ParamCustomPageSize HeightOffset\):,\1D<29>calage vertical:,
s,\(^\*ParamCustomPrintColors Colors/\)Print Colors:,\1Couleurs d'impression:,
s,\(^\*OpenUI .*/\)Black Correction:,\1Correction du noir:,
s,\(^\*CustomBlackCorrect BlackCorrect\):,\1/Facteur de noir:,
s,\(^\*OpenUI .*/\)Dot Removal:,\1Suppression de points:,
s,\(^\*DotRemoval .*Smooth\):,\1/L<e9>g<e8>re:,
s,\(^\*DotRemoval .*Heavy\):,\1/Importante:,
s,\(^\*OpenUI .*/\)Interlaced Print:,\1Impression entrelac<e9>e:,
s,\(^\*InterlacedPrint .*Simple\):,\1:,
s,\(^\*InterlacedPrint .*Optimized\):,\1/Optimis<e9>e:,
s,\(^\*OpenUI .*/\)Rendering Type:,\1Rendu mat<e9>riel:,
s,\(^\*RenderType None\):,\1Aucun:,
s,\(^\*RenderType .*/\)Snap to Primaries:,\1Couleurs primaires:,
s,\(^\*RenderType .*/\)Snap to White and Black:,\1Blanc et noir:,
s,\(^\*RenderType .*/\)Error Diffusion:,\1Diffusion d'erreurs:,
s,\(^\*RenderType .*/\)Ordered Dithering:,\1Lissage ordonn<e9>:,
s,\(^\*RenderType .*/\)Cluster Ordered Dithering:,\1Lissage ordonn<e9> par blocs:,
s,\(^\*RenderType .*/\)Monochrome Error Diffusion:,\1Diffusion d'erreurs monochrome:,
s,\(^\*RenderType .*/\)Monochrome Ordered Dithering:,\1Lissage ordonn<e9> monochrome:,
s,\(^\*RenderType .*/\)Monochrome Cluster Ordered Dithering:,\1Lissage ordonn<e9> par blocs monochrome:,
EOSED
	    sed -f $locsedscript <"$2"
	    rm -f $locsedscript
	    ;;

	*)
	    cat "$2"
	    ;;
    esac
}

localeok() {
    _loc="$1"

    _theloc="`localetranslated \"$_loc\"`"
    if [ ! -z "$_theloc" ]
    then
	echo "$_theloc"
    fi

    if [ -z "$_theloc" ]
    then
    	_newloc="`echo \"$_loc\" | sed 's/,..*$//'`"
        if [ "$_newloc" != "$_loc" ]
    	then
	    _loc="$_newloc"
    	    _theloc="`localetranslated \"$_loc\"`"
            if [ ! -z "$_theloc" ]
            then
	    	echo "$_theloc"
	    fi
        fi
    fi

    if [ -z "$_theloc" ]
    then
        _newloc="`echo \"$_loc\" | sed 's/\...*$//'`"
        if [ "$_newloc" != "$_loc" ]
    	then
	    _loc="$_newloc"
    	    _theloc="`localetranslated \"$_loc\"`"
            if [ ! -z "$_theloc" ]
            then
	    	echo "$_theloc"
	    fi
        fi
    fi

    if [ -z "$_theloc" ]
    then
        _newloc="`echo \"$_loc\" | sed 's/_[A-Z][A-Z]$//'`"
        if [ "$_newloc" != "$_loc" ]
    	then
	    _loc="$_newloc"
    	    _theloc="`localetranslated \"$_loc\"`"
            if [ ! -z "$_theloc" ]
            then
	    	echo "$_theloc"
	    fi
        fi
    fi

    unset _loc _newloc _theloc
}

#

if [ -z "$where" ]
then
    if [ $nextstep -eq 1 ]
    then
    	where=/NextLibrary/PrinterTypes
    else
        where=/usr/share/ppd
    fi
fi

if [ -z "$locales" ]
then
if [ $nextstep -eq 1 ]
then
    nslocales=" English.lproj "
    others="`2>/dev/null cd /NextLibrary/Receipts && ls -d *Essential*`"
    if [ ! -z "$others" ]
    then
	for o in $others
	do
	    nslocales="$nslocales `echo $o | sed 's/Essential.*//'`"
	done
    fi
    for n in $nslocales
    do
	locales="$locales `localesrc $n`"
    done
else
    : ${locales:=" $LANGUAGE "}
    : ${locales:=" $LC_ALL "}
    : ${locales:=" $LC_MESSAGES "}
    : ${locales:=" $LANG "}

    if [ -z "$LINGUAS" ]
    then
    locale=`which locale`
    if [ -z "$locale" ]
    then
    for d in /usr/share /usr/lib /usr/local/share /usr/local/lib
    do
        if [ -d $d/locale ]
        then
	    localesdirs="`(2>/dev/null cd $d/locale && ls -d *)`"
	    if [ ! -z "$localesdirs" ]
	    then
	        for l in $localesdirs
	        do
		    if [ "`echo \"$locales\" | grep \" $l \"`" = "" ]
		    then
	                locales="$locales $l "
		    fi
	        done
	    fi
        fi
    done
    else
	for l in `$locale -a | grep -v '^POSIX$' | grep -v '^C$'`
        do
	    if [ "`echo \"$locales\" | grep \" $l \"`" = "" ]
	    then
                locales="$locales $l "
	    fi
        done
    fi
    else
	locales="$LINGUAS"
    fi
fi

for l in $locales
do
    cl="`localeok $l`"
    if [ "$cl" != '' -a "`echo \"$uselocales\" | grep \" $cl \"`" = "" ]
    then
	uselocales="$uselocales $cl "
    fi
done

locales="$uselocales"

fi

#

for l in $locales
do
    l=`localedest "$l"`
    if [ ! -d "$where/$l" ]
    then
        if 2>/dev/null mkdirs "$where/$l"
        then
	    :
        else
	    >&2 echo $me: cannot create \`$where/$l\'
	    exit 2
	fi
    fi
done

# Determine names size.

: ${tdir:=$where}

if [ "$names_size" != long -a "$names_size" != short ]
then
    if [ ! -d $tdir ]
    then
	names_size=long
    else
        touch $tdir/___this_is_a_long_name
        if [ `ls $tdir/___this_is_a_long_name` = $tdir/___this_is_a_long_name ]
        then
	    names_size=long
        else
	    names_size=short
	fi
    fi
    rm -f $tdir/___this_is_a_long_name
fi

# Build rename `table'.

rm -f $sedscript

for ppd in $files
do
    if [ ! -r $ppd ]
    then
	>&2 echo $me: cannot open \`$ppd\'
	exit 2
    fi

    if [ $names_size = long ]
    then
        name=`grep '^\*ModelName:' $ppd | sed 's/.*"\(.*\)".*/\1/'`
        if [ -z "$name" ]
        then
    	    name=`grep '^\*Product:' $ppd | sed 's/.*(\(.*\)).*/\1/'`
    	    if [ -z "$name" ]
	    then
		name=`grep '^\*NickName:' $ppd | sed 's/.*(\(.*\)).*/\1/'`
		if [ -z "$name" ]
		then
		    name=`grep '^\*ShortNickName:' $ppd | sed 's/.*(\(.*\)).*/\1/'`
		else
	            name=`basename $ppd .ppd`
		fi
	    fi
        fi
    else
	name=`basename $ppd .ppd`
    fi
    name=`ppdname "$name"`
    pcname=`grep '^\*PCFileName:' $ppd | sed 's/.*"\(.*\)".*/\1/'`
    if [ -z "$pcname" ]
    then
	pcname=`basename $ppd .ppd | tr '[A-Z]' '[a-z]'`
    else
	pcname=`echo $pcname | tr '[A-Z]' '[a-z]' | sed 's/\.ppd$//'`
    fi
    altpcname=`echo $pcname | tr '[a-z]' '[A-Z]'`

    eval "file_`ppdname -b "$ppd"`=$name"

    echo "s/^\(\*Include:\)[ 	][ 	]*\"$pcname\"/\\1 \"$name\"/" >>$sedscript
    echo "s/^\(\*Include:\)[ 	][ 	]*\"$altpcname\"/\\1 \"$name\"/" >>$sedscript
done

psplpr=`which psplpr`

# If we have gs, get its version/revision once just in case.

gs=`which gs`

if [ ! -z "$gs" ]
then
    cat <<EOF >$psfile
statusdict begin
version print (.) print revision ==
end
EOF
    revstr="`2>/dev/null $gs -dNODISPLAY -q - <$psfile`"
    if [ ! -z "$revstr" ]
    then
        revision=`echo $revstr | sed 's/\..*//'`
        version=`echo $revstr | sed 's/.*\.//'`

        revision=`expr 0$revision + 0`
        version=`expr 0$version + 0`

        if [ $subst_rev_string -eq 1 ]
        then
            echo 's/^\(\*PSVersion: \)"(.*)"/\1'$revstr'/' >>$sedscript
        fi
    fi
    rm -f $psfile
fi

for ppd in $files
do
    pname=`ppdname -b "$ppd"`
    if [ ! -z "$dontlocalize" ]
    then
        # The code that follows is used to determine if a file is localized,
        # in which case no localization will happen.
        case "$ppd" in
    	    ??/*.ppd|??_??/*.ppd|??.*/*.ppd|??_??.*/*.ppd|??,*/*.ppd|??_??,*/*.ppd|??_??.*,*/*.ppd)
	        thelocales="`echo \"$ppd\" | sed 's,/.*$,,'`"
	        ;;
    	    */??/*.ppd|*/??_??/*.ppd|*/??.*/*.ppd|*/??_??.*/*.ppd|*/??,*/*.ppd|*/??_??,*/*.ppd|*/??_??.*,*/*.ppd)
	        thelocales="`echo \"$ppd\" | sed 's,.*/\(.*\)/.*$,\1,'`"
	        ;;
	    *)
	        thelocales="$locales"
        esac
    else
        thelocales="$locales"
    fi

bad=0

for locale in $thelocales
do
    dest=$where/`localedest $locale`/`eval echo '$file_'$pname`.ppd
    if [ "`echo \"$installedppds\" | grep \" $dest \"`" = "" ]
    then
	installedppds="$installedppds $dest "
    if (localesubst $locale $ppd | sed -f $sedscript >$tmpdest) && test "`head -1 $tmpdest`" != "" && (2>/dev/null rm -f $dest) && cp $tmpdest $dest && rm -f $tmpdest
    then
        if [ ! -z "$verbose" ]
        then
    	    echo $dest
        fi

	dir="`dirname $dest`"
	if [ "`echo \"$ppddirs\" | grep \" $dir \"`" = "" ]
	then
	    ppddirs="$ppddirs $dir"
	fi

	device="`gsdevice $dest`"

    	if [ "$paper" != "" -o "$device" != "" ]
    	then
	    if [ "`egrep 'DefaultPage(Size|Region):' \"$ppd\"`" != "" ]
	    then
	    	if [ ! -z "$paper" ]
	        then
                    papersubst "$paper" $ppd $dest
	        else
		    	if [ "$device" = unknown ]
		    	then
        		    >&2 echo $me: cannot determine gs device for \`$dest\', no paper size substitution
		    	else
			    if [ "$device" != unspecified ]
			    then
				case "$device" in
				    *default)
					devname="$device"
					device=nullpage
					devname="$devname ($device)"
					;;
				    *)
					devname="$device"
					;;
				esac
				if [ -z "$gs" ]
				then
				    if [ -z "$nogssaid" ]
				    then
					>&2 echo $me: gs not found, cannot use it to determine default paper sizes
					nogssaid=y
				    fi
				else
			            devpaper=`gspapersize $device`
			            if [ -z "$devpaper" ]
			            then
        		                >&2 echo $me: cannot determine default paper size for device \`$device\'
			            else
					if [ -z "$psplpr" ]
					then
				    	    if [ -z "$nopsplprsaid" ]
				    	    then
						>&2 echo $me: psplpr not found, cannot use it to check paper sizes
						nopsplprsaid=y
					    fi
					fi
			                papersubst "`uppername $devpaper`" $ppd $dest
				    fi
			        fi
			    fi
		    fi
		fi
	    fi
	fi
        if [ ! -z "$revstr" -a "`gsdevice $dest`" != " " ]
        then
    	    instrev="`grep '^*PSVersion: \"([0-9]' $dest`"
	    if [ ! -z "$instrev" ]
	    then
	        instrevision=`echo $instrev | sed 's/.*(\([0-9]*\)\..*/\1/'`
	        instversion=`echo $instrev | sed 's/.*\.\([0-9]*\)).*/\1/'`
    
    	        instrevision=`expr 0$instrevision + 0`
    	        instversion=`expr 0$instversion + 0`
    
	        if [ $instrevision -gt $revision ]
	        then
		    >&2 echo "$me: \`$pname' (\`$dest') asks for PostScript" \
		         "revision $instrevision, current is $revision"
		    bad=1
	        fi
	        if [ $instversion -gt $version ]
	        then
		    >&2 echo "$me: \`$pname' (\`$dest') asks for Ghostscript" \
		        "version $instversion, current is $version"
		    bad=1
	        fi
	    fi
        fi
    else
	>&2 echo "$me: error trying to install \`$dest'"
    fi
    fi
done
done

if [ 0$bad -eq 1 ]
then
    >&2 echo "$me: please consider updating your Ghostscript interpreter"
fi

makeppddirs

rm -f $sedscript $psfile $tmpdest

