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

#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"
# because 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 -v -q ptal-init ) &
     timebombPID=$!
     ptal-init setup-usb &>/dev/null || { echo "failed: ptal-init setup-usb" 1>&2 ; exit 2 ; }
else echo "cannot execute /usr/sbin/ptal-init" 1>&2
     exit 1
fi
kill -9 $timebombPID

# 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

exit 0

