#!/bin/sh
#
# irda-utils       Init script for irda-utils: manage start and stop of
#                  irattach and setup some other items.
#
# Author:          Sebastian Henschel <shensche@debian.org>

set -e

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
DAEMON="/usr/sbin/irattach"
NAME="irattach"
PIDFILE="/var/run/$NAME.pid"
PACKAGE="irda-utils"
DESC="IrDA service"

test -x $DAEMON || exit 0

# Handle configuration
if [ -f /etc/default/$PACKAGE ]; then
    . /etc/default/$PACKAGE
fi
test "$ENABLE" = "false" && exit 0
if [ -z "$DEVICE" ]; then
    DEVICE="/dev/ttyS1"
fi
if [ -z "$DONGLE" ]; then
    DONGLE=""
else
    if [ "$DONGLE" != "none" ]; then
        DONGLE="-d $DONGLE"
    fi
fi
if [ "$DISCOVERY" = "true" ]; then
    DISCOVERY="-s"
else
    DISCOVERY=""
fi



case "$1" in
start)
    echo -n "Starting $DESC: $NAME"

    # Needed for some machines in FIR-mode
    if [ -n "$SETSERIAL" ]; then
        /bin/setserial $SETSERIAL uart none port 0x0 irq 0
    fi

    # Needed for pmac_zilog
    case $(uname -r) in
        2.6.*)
            case "$DEVICE" in
                /dev/ttyS*) modprobe irtty-sir
                ;;
            esac
        ;;
    esac

    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
        -- $DEVICE $DONGLE $DISCOVERY
    sleep 1

    echo "115200" > /proc/sys/net/irda/max_baud_rate
    echo "."
    ;;
stop)
    echo -n "Stopping $DESC: $NAME"
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    echo "."
    ;;
#reload)
#   ;;
restart|force-reload)
    echo -n "Restarting $DESC: $NAME"
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    sleep 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON \
        -- $DEVICE $DARG $DISCOVERY
    echo "."
    ;;
*)
    N=/etc/init.d/$PACKAGE
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
