#!/bin/sh
LPOPTIONS=/etc/cups/lpoptions
export TET_ROOT=`pwd`
export TET_EXECUTE=`pwd`
export LC_ALL=C
export PATH=$PATH:/opt/lsb-tet3-lite/bin

check_requires() {
  for lib in libcups.so.2 libcupsimage.so.2;do
    if [ ! -f "/usr/lib/$lib" -a ! -f "/usr/lib64/$lib" ];then
      echo "install the package that provides $lib and then re-run the tests"
      echo "test run aborted"
      exit 1
    fi
  done
  for app in foomatic-rip gs hpijs;do
    if [ ! -x "/usr/bin/$app" ];then
      echo "install the package that provides $app and then re-run the tests"
      echo "test run aborted"
      exit 1
    fi
  done
}

check_requires

if [ -f "$LPOPTIONS" ];then
  echo "Preserving system lpoptions file..."
  mv -f "$LPOPTIONS" "$LPOPTIONS".LSB
fi
cp ./cups/etc/cups/printers.conf.dist ./cups/etc/cups/printers.conf

echo "Starting cupsd (sleep 5 seconds)..."
./cups/sbin/cupsd
sleep 5
CUPSDPID=`pidof $TET_ROOT/cups/sbin/cupsd`
if [ -z "$CUPSDPID" ];then
  echo "test cupsd failed to start"
  echo "test run aborted"
  exit 1
fi

# Cause the client library to pick up our custom client.conf.
HOME=$TET_ROOT/cups/client
export HOME

touch .test_start
tcc -e -p -t 300 .
JOURNAL=`find results -cnewer .test_start -name journal`
if [ -n "$JOURNAL" ];then
  tjreport $JOURNAL
fi

rm -f .test_start
echo "Stopping cupsd..."
CUPSDPID=`pidof $TET_ROOT/cups/sbin/cupsd`
if [ -n "$CUPSDPID" ];then
  kill $CUPSDPID
fi
rm -f ./cups/etc/cups/printers.conf
if [ -f "$LPOPTIONS".LSB ];then
  echo "Restoringing system lpoptions file..."
  mv -f "$LPOPTIONS".LSB "$LPOPTIONS"
fi


