#! /bin/sh
#
# udftools
#     Call pktsetup to set up packet device associations
#
# Written and Copyright 2003 Richard Atterer <atterer<at>debian.org>, GPLv2.
# Thanks to Aleksandar Topuzovic <aleksandar.topuzovic<at>fer.hr> for an
# initial version of the script.

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC="udftools packet writing"
PKTSETUP=/usr/bin/pktsetup
DEFAULTFILE=/etc/default/udftools
DEVICES=""
if test -e /dev/.devfsd; then
    PDEVICES="/dev/pktcdvd/0 /dev/pktcdvd/1 /dev/pktcdvd/2 /dev/pktcdvd/3"
else
    PDEVICES="/dev/pktcdvd0 /dev/pktcdvd1 /dev/pktcdvd2 /dev/pktcdvd3"
fi

if test -f "$DEFAULTFILE"; then
    . "$DEFAULTFILE"
fi
test -x "$PKTSETUP" || exit 0

dostart() {
    if test -z "$DEVICES"; then
        echo "Not starting $DESC: No devices listed in $DEFAULTFILE"
    else
        echo -n "Starting support for $DESC:"
        set $PDEVICES
        for DEVICE in $DEVICES; do
            echo -n " ${1#/dev/}=${DEVICE#/dev/}" || true
            $PKTSETUP "$1" "$DEVICE"
            shift
        done
        echo "."
    fi
}

dostop() {
    if test -z "$DEVICES"; then
        echo "Not stopping $DESC: No devices listed in $DEFAULTFILE"
    else
        echo -n "Stopping support for $DESC:"
        set $PDEVICES
        for DEVICE in $DEVICES; do
            echo -n " ${1#/dev/}=${DEVICE#/dev/}" || true
            $PKTSETUP -d "$1" "$DEVICE"
            shift
        done
        echo "."
    fi
}

case "$1" in
    start) dostart;;
    stop)  dostop;;
    restart|force-reload) dostop; dostart;;
    *)
        echo "Usage: /etc/init.d/udftools {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0
