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

#set -x
export PATH="/usr/sbin:/bin:/usr/bin:/sbin"
export LC_ALL="POSIX"
export LANG="POSIX"

# 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="120"
# 'ptal-init setup-usb' exits with non-zero exit code when it is running in the background.
# Therefore a simple time bomb is started before it:
if [ -x /usr/sbin/ptal-init ]
then ( sleep $MAXIMUM_WAIT ; 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

