#!/bin/sh

# Test PPD conformance
#
# Copyright 2006-2017 Robert Krawitz (rlk@alum.mit.edu)
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

################################################################
#                 TEST FOR PPD FILE COMPLIANCE
#
# Keeping this up to date with changing CUPS versions is a real headache.
# This test, however, is particularly important; PPD conformance failures
# are one of the most frequently observed problems, and a lot of apps
# (and CUPS itself) are very sensitive -- arguably overly so -- to
# violations of the spec.
#
# We can't skip the translated PPD files either; sometimes translations
# blow out token maximum lengths.

PPD_PARALLEL=200
echo "GENERATING PPD FILES:"
if [[ -n $STP_FAST_TEST ]] ; then
    make EXTRA_GENPPD_OPTS='-b -Z -S' ppd-clean ppd-nonls
    PPD_PARALLEL=20
else
    make EXTRA_GENPPD_OPTS='-b -Z' ppd-clean ppd-global-a ppd-nls-a ppd-nonls-a 
fi

ppd_count=$(find ppd -name '*.ppd' -print | wc -l)

# Setting a limit on the number of files per invocation improves
# performance by about a minute by reducing the tail at the end.
[[ -n $STP_PARALLEL ]] && PARALLEL="-P $STP_PARALLEL -L $PPD_PARALLEL"

# Most non-Macintosh systems won't have the Macintosh-specific profiles
# installed in Macintosh-specific locations.
#
# Also, a number of our media sizes aren't named correctly, but we'll
# accept those issues rather than cluttering the namespace further and/or
# changing tag names.
cupstestppdopts='-I profiles -W sizes -I filters'

# This lets us report progress
testcmd="echo -n . 1>&2; cupstestppd $cupstestppdopts "'"$@"'

echo -n "TESTING PPD FILES: "
failures="$(find ppd -name '*.ppd*' -print | sort -t/ -k3 -k2 | xargs $PARALLEL sh -c "$testcmd" |grep 'FAIL$' | awk -F: '{print $1}')"

if [[ -z "$failures" ]] ; then
    echo "All $ppd_count PPD files pass"
    exit 0
fi

# Use this with "normal" PPD files without translated numbers.
#echo $failures | xargs cupstestppd $cupstestppdopts 2>&1 | egrep -v 'Missing "[a-z][a-z](_[A-Z][A-Z])?" translation string for option Stp((Brightness|Contrast|Saturation), choice None|([a-zA-Z0-9]+, choice (-?[0-9]+)))!$' |egrep -v 'Missing cupsICCProfile file'

# Use this with PPD files with translated numbers (genppd -N)
# With normal globalized PPD files this will yield hundreds of megabytes
# of errors.
echo $failures | xargs cupstestppd -r $cupstestppdopts 2>&1

fail_count=`echo "$failures" | wc -l`
echo "$fail_count/$ppd_count fail"
exit 1
