#! /bin/bash
#
# Johannes Meixner <jsmeix@suse.de>, 2004, 2005, 2006

#set -x

export PATH="/sbin:/usr/sbin:/usr/bin:/bin"
export LC_ALL="POSIX"
export LANG="POSIX"
umask 022

# The ptal service and the hplip service exclude each other.
# Disable the hplip service if it exists:
if [ -x /etc/init.d/hplip ]
then /etc/init.d/hplip stop
     insserv -r hplip
fi

# Setup ptal USB devices:
MAXIMUM_WAIT="60"
# 'ptal-init setup-usb' exits with non-zero exit code when it is running in the background.
# Therefore a simple time bomb background process is started before it.
# This time bomb background process process is normally killed at the end.
# As a signal is processed not until a "sleep" has finished,
# it is crucial not to do simply "sleep $MAXIMUM_WAIT"
# but to do "for i in $( seq $MAXIMUM_WAIT ) ; do sleep 1 ; done"
# otherwise it would wait in any case until "sleep $MAXIMUM_WAIT" has finished.
if [ -x /usr/sbin/ptal-init ]
then ( for i in $( seq $MAXIMUM_WAIT ) ; do sleep 1 ; done ; killall -9 ptal-init &>/dev/null ) &
     timebombPID=$!
     ptal-init setup-usb &>/dev/null || { echo "failed: ptal-init setup-usb" 1>&2 ; exit 2 ; }
     # If the timebomb is still running, ptal-init has finished
     # and killing the timebomb will be successful.
     if kill -9 $timebombPID &>/dev/null
     then # Setup the ptal service:
          if [ -x /etc/init.d/ptal ]
          then insserv ptal || { echo "failed: insserv ptal" 1>&2 ; exit 4 ; }
               /etc/init.d/ptal restart || { echo "failed: /etc/init.d/ptal restart" 1>&2 ; exit 5 ; }
          else echo "cannot execute /etc/init.d/ptal" 1>&2
               exit 3
          fi
     else echo -e "\nAborted ptal-init after $MAXIMUM_WAIT seconds timeout." 1>&2
          exit 6
     fi
else echo "cannot execute /usr/sbin/ptal-init" 1>&2
     exit 1
fi

exit 0

