#! /bin/bash

# You may start it with a command like:
# nohup nice ./makePPDtest 1>makePPDtest.stdout 2>makePPDtest.stderr &

# Enable for debugging:
#set -x

# Abort if another makePPDtest process is running at the same time:
if test "$( pgrep makePPDtest )" != "$$"
then echo "Aborting: Another makePPDtest is already running."
     exit 1
fi

# Abort if not root runs it:
if test "$( id -u )" != "0"
then echo "Aborting: You must be root to run makePPDtest."
     exit 1
fi

# Function to restart cupsd with new log and spool files:
restartcupsd()
{ rccups stop &>/dev/null
  sleep 1
  lpstat -r &>/dev/null && { echo "cupsd not stopped" ; exit 1 ; } 
  rm -f /var/log/cups/* /var/spool/cups/c* /var/spool/cups/d* /var/spool/cups/tmp/*
  rccups start &>/dev/null
  for i in $( seq 60 )
  do sleep 1
     lpstat -r &>/dev/null && break
  done
  lpstat -r &>/dev/null || { echo "cupsd not available" ; exit 1 ; }
}

# Function to test a PPD:
testppd()
{ if file $1 | grep -q 'gzip compressed data'
  then catprogram="zcat"
  else catprogram="cat"
  fi
  if $catprogram $1 | cupstestppd - &>/dev/null
  then restartcupsd
       lpadmin -p PPDtest -v file:/dev/null -E -P $1
       lp -d PPDtest $PStestpage &>/dev/null
       lp -d PPDtest /etc/SuSE-release &>/dev/null
       for i in $( seq 60 )
       do sleep 1 
          lpstat -o PPDtest | grep -q '^PPDtest' || break
       done
       if lpstat -o PPDtest | grep -q '^PPDtest'
       then echo "printjob not finished: $1"
            echo "printjob not finished: $1" >>makePPDtest.failed
            cancel -a PPDtest
            sleep 1
       fi
       if grep '^E' /var/log/cups/error_log
       then echo "testppd failed: $1"
            echo "testppd failed: $1" >>makePPDtest.failed
       else echo "o.k.: $1"
            echo "o.k.: $1" >>makePPDtest.ok
       fi
       cp /var/log/cups/error_log makePPDtest.error_log.last
       lpadmin -x PPDtest
       sleep 1
  else echo "cupstestppd failed: $1"
       echo "cupstestppd failed: $1" >>makePPDtest.failed
  fi
}

# Function to test all PPDs in a package:
testpackage()
{ for ppd in $( rpm -ql $1 | egrep '\.ppd\.gz$|\.ppd$' | egrep "$2" )
  do testppd $ppd
  done
}

# Empty the output files:
>makePPDtest.packageversions
>makePPDtest.failed
>makePPDtest.ok
>makePPDtest.error_log.last

# Report the version of the important RPMs:
for p in cups-libs cups-client cups cups-drivers cups-drivers-stp manufacturer-PPDs hplip foomatic-filters ghostscript-library
do rpm -q $p | tee -a makePPDtest.packageversions
done

# Save cupsd.conf and set "LogLevel info" and "Browsing Off"
# and disable log rotating via "MaxLogSize 0":
test -f /etc/cups/cupsd.conf.makePPDtest.save || cp -p /etc/cups/cupsd.conf /etc/cups/cupsd.conf.makePPDtest.save
sed -i -e 's/^LogLevel.*/LogLevel info/' /etc/cups/cupsd.conf
sed -i -e 's/^#*Browsing.*/Browsing Off/' /etc/cups/cupsd.conf
sed -i -e 's/^#*MaxLogSize.*/MaxLogSize 0/' /etc/cups/cupsd.conf

# Remove the PPDtest queue:
restartcupsd
lpadmin -x PPDtest &>/dev/null

# Deterimne the PostScript testpage file according to the installed packages:
PStestpage="$( rpm -ql yast2-printer | grep 'testpg\.ps$' || rpm -ql ghostscript-library | grep 'colorcir\.ps$' || echo 'no_PostScript_testpage_found' )"

# Depending on the command line parameters:
# - Test a single PPD with 'LogLevel debug' if the first parameter ends with .ppd or .ppd.gz
# - Test all PPDs in a package if the first parameter is an installed package
#   and use an optional second parameter as 'egrep' search pattern for the PPD file names in the package
# - Fall back to test all PPDs in all installed packages which contain PPDs
#   and use an optional first parameter as 'egrep' search pattern for the PPD file names in the packages
if echo "$1" | egrep -q '\.ppd\.gz$|\.ppd$'
then sed -i -e 's/^LogLevel.*/LogLevel debug/' /etc/cups/cupsd.conf
     testppd $1
else if rpm -q "$1" &>/dev/null
     then testpackage $1 $2
     else for p in ghostscript-library cups cups-drivers-stp hplip manufacturer-PPDs cups-drivers
          do echo "Testing package $p"
             rpm -q $p && testpackage $p $1
          done
     fi
fi