#!/bin/sh
LPOPTIONS=/etc/cups/lpoptions
CUPS_PORT=632
SLEEP_TIME=5
export TET_ROOT=`pwd`
export TET_EXECUTE=`pwd`
export LC_ALL=C
export LC_MESSAGES=C
export PATH=$PATH:/opt/lsb-tet3-lite/bin:/opt/lsb/test/printing/BIN
CUPSD_CONFIG=$TET_ROOT/cups/etc/cups/cupsd.conf

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

check_for_cups_11 () {
  # libcups from 1.1 cannot use a port other than 631 (bug 2483)
  # much of this routine is not LSB compliant, but we are kind of
  # stuck trying to identify which libcups.so is present
  # SLES10 bundles cups-config with the libs, try this first
  if [ -x /usr/bin/cups-config ];then
    cups-config --version | grep -q ^1.1
    if [ "$?" -eq 0 ];then
      CUPS_PORT=631
    fi
  else
    # no cups-config, try rpm/dpkg query
    CUPS_LIB=`ldd convenience/cupsConvenience | grep libcups | awk '{print $3}'`
    if [ -n "$CUPS_LIB" ];then
      if [ -x /usr/bin/rpm ];then
        if [ -f "$CUPS_LIB" ];then
          rpm -qf $CUPS_LIB | grep -q 1.1.
          if [ "$?" -eq 0 ];then
            CUPS_PORT=631
          fi
        fi
      fi
      if [ -x /usr/bin/dpkg-query ];then
        if [ -f "$CUPS_LIB" ];then
          dpkg-query -s `dpkg-query -S $CUPS_LIB | awk -F: '{print $1}'` | grep Version | grep -q 1.1.
          if [ "$?" -eq 0 ];then
            CUPS_PORT=631
          fi
        fi
      fi
    fi
  fi
  if [ "$CUPS_PORT" -eq 631 ];then
    CUPSD_CONFIG=$TET_ROOT/cups/etc/cups/cupsd.conf.631
  fi
}

clean_spool () {
  # spool doesn't always get flushed, make sure it's clean
  echo "Cleaning test cupsd spool..."
  rm -f cups/var/spool/cups/*
}

clean_logs () {
  # helps troubleshooting if these just have the last run
  echo "Cleaning test cupsd logs..."
  rm -f cups/var/log/cups/*
}

check_requires
check_for_cups_11

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

# using std port, stop the system daemon if present
if [ "$CUPS_PORT" = 631 ];then
  echo "Stopping system cupsd (if running)..."
  SYSCUPSDPID=`pidof cupsd`
  if [ -n "$SYSCUPSDPID" ];then
    kill $SYSCUPSDPID
    if [ "$?" -eq 0 ];then
      STOPPED_SYS_CUPS=1
    fi
  fi
fi

echo "Starting test cupsd (sleep $SLEEP_TIME seconds)..."
clean_spool
clean_logs
./cups/sbin/cupsd -c $CUPSD_CONFIG
sleep $SLEEP_TIME
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.
# but only if we're using the alternate port method
if [ "$CUPS_PORT" = 632 ];then
  HOME=$TET_ROOT/cups/client
  export HOME
fi

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 test cupsd..."
CUPSDPID=`pidof $TET_ROOT/cups/sbin/cupsd`
if [ -n "$CUPSDPID" ];then
  kill $CUPSDPID
fi
rm -f ./cups/etc/cups/printers.conf
clean_spool
if [ -f "$LPOPTIONS".LSB ];then
  echo "Restoring system lpoptions file..."
  mv -f "$LPOPTIONS".LSB "$LPOPTIONS"
fi

# using std port, re-start the system daemon if stopped 
if [ "$CUPS_PORT" = 631 -a -n "$STOPPED_SYS_CUPS" ];then
  echo "Restarting system cupsd..."
  cupsd
fi

