#!/bin/bash

set -e -u

LOG=log.txt
LOG2=log2.txt
QUEUE=testprinter_cpdb_libs
CPDBDEMO=/usr/bin/print_frontend
FILE_TO_PRINT=/usr/share/cups/data/default-testpage.pdf
CUPS_SPOOL_DIR=/var/spool/cups

# Create temporary work directory
WORKDIR=$(mktemp -d)
cd $WORKDIR

cleanup() {
    # Remove the log files
    rm -f $LOG $LOG2
    # Remove the temporary directory
    rm -rf $WORKDIR
    # Delete the printer
    lpadmin -x $QUEUE 2>/dev/null || :
}

trap cleanup 0 EXIT INT QUIT ABRT PIPE TERM

# Create the log files
rm -f $LOG
touch $LOG
rm -f $LOG2
touch $LOG2

# Create a test print queue (disabled, but accepting jobs)
#cupsctl --debug-logging
lpadmin -p $QUEUE -v file:/dev/null -m drv:///sample.drv/laserjet.ppd -o PageSize=Env10
cupsaccept $QUEUE

# Run the demo with a session D-Bus and feed in commands, in parallel
# do a kill on the demo process after a timeout, for the case that the
# commands take too long or stopping the demo does not work. Ignore the
# error of the kill command if the demo gets stopped correctly.
( \
  sleep 5; \
  echo get-all-options $QUEUE CUPS; \
  sleep 2; \
  echo print-file $FILE_TO_PRINT $QUEUE CUPS; \
  sleep 1; \
  echo stop \
) | dbus-run-session -- $CPDBDEMO >> $LOG 2>&1 & \
PID=$!; sleep 30; kill -9 $PID > /dev/null 2>&1 && \
    ( echo "$CPDBDEMO did not terminate in time! Killed." >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Does the printer appear in the initial list of available printers?
echo "Initial listing of the printer:" >> $LOG2
( grep '\*\* Message:.*: *Printer '$QUEUE'$' $LOG >> $LOG2 ) || \
    ( echo "CUPS queue $QUEUE not listed!" >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Does the attribute "printer-resolution" appear in the list of options?
echo "Attribute listing of \"printer-resolution\":" >> $LOG2
( grep '\*\* Message:.*: *printer-resolution' $LOG >> $LOG2 ) || \
    ( echo "Attribute \"printer-resolution\" not listed!" >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Does the attribute "print-quality" appear in the list of options?
echo "Attribute listing of \"print-quality\":" >> $LOG2
( grep '\*\* Message:.*: *print-quality' $LOG >> $LOG2 ) || \
    ( echo "Attribute \"print-quality\" not listed!" >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Does the setting "na_number-10_4.125x9.5in" appear as a default setting?
echo "\"na_number-10_4.125x9.5in\" as a default setting:" >> $LOG2
( grep '\*\*\*\*DEFAULT: *na_number-10_4.125x9.5in' $LOG >> $LOG2 ) || \
    ( echo "Setting \"na_number-10_4.125x9.5in\" not listed as default!" >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Did the successful submission of a print job get confirmed?
echo "Confirmation message for job submission:" >> $LOG2
( grep -i 'File printed successfully' $LOG >> $LOG2 ) || \
    ( echo "No confirmation of job submission!" >> $LOG2; \
      cat $LOG $LOG2; \
      exit 1 )

echo >> $LOG2

# Find the job
sleep 10
grep '\[Job [0-9]' /var/log/cups/error_log >> $LOG2
echo "Job submitted:" >> $LOG2
echo lpstat -o >> $LOG2
lpstat -o >> $LOG2
echo "not-completed" >> $LOG2
lpstat -W not-completed -o >> $LOG2
echo "completed" >> $LOG2
lpstat -W completed -o >> $LOG2
echo "wait" >> $LOG2
sleep 5
lpstat -o >> $LOG2
echo "not-completed" >> $LOG2
lpstat -W not-completed -o >> $LOG2
echo "completed" >> $LOG2
lpstat -W completed -o >> $LOG2
echo >> $LOG2
JOBNUM=`lpstat -o $QUEUE | tail -1 | cut -d ' ' -f 1 | cut -d - -f 2`
echo "Job number: $JOBNUM" >> $LOG2
if [ -n "$JOBNUM" ]; then
    SPOOLFILE=`printf "%s/d%05d-001" $CUPS_SPOOL_DIR $JOBNUM`
    echo "Job spool file: $SPOOLFILE" >> $LOG2
    if [ -f "$SPOOLFILE" ]; then
	echo "diff $FILE_TO_PRINT $SPOOLFILE" >> $LOG2
	diff $FILE_TO_PRINT $SPOOLFILE >> $LOG2 2>&1 || ( \
	    echo "Submitted job data and spooled job data differ!" >> $LOG2; \
	    cat $LOG $LOG2; \
	    exit 1; \
	)
    else
	echo "Spool file does not exist!" >> $LOG2
	cat $LOG $LOG2
	exit 1
    fi
else
    echo "Job number not found!" >> $LOG2
    cat $LOG $LOG2
    exit 1
fi

