#!/bin/sh

### BEGIN INIT INFO
# Provides:          irda
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Infrared port support
### END INIT INFO

# irda-utils       Init script for irda-utils: manage start and stop of
#                  irattach and setup some other items.
#
# Authors:         Sebastian Henschel <shensche@debian.org>
#                  Alberto Gonzalez Iniesta <agi@inittab.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"
SYSCTL="/sbin/sysctl"
SMCINIT="/usr/sbin/smcinit"

test -x $DAEMON || exit 0
test -x $SYSCTL || 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"

    # Running smcinit is needed in some laptops prior to port use
    # You should set this variable in /etc/default/irda-utils
    if [ "$USE_SMCINIT" = "yes" ]; then
        $SMCINIT
    fi

    # Needed for some machines in FIR-mode
    if [ -n "$SETSERIAL" ]; then
        test -x /bin/setserial || exit 0
        /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 2> /dev/null || true
                ;;
            esac
        ;;
    esac

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

    if [ -n "$DISCOVERY" ]; then
	$SYSCTL -e -q -w net.irda.discovery=1
    else
	$SYSCTL -e -q -w net.irda.discovery=0
    fi
	
    $SYSCTL -e -q -w net.irda.max_baud_rate=115200
    echo "."
    ;;
stop)
    echo -n "Stopping $DESC: $NAME"

    if [ -n "$DISCOVERY" ]; then
	$SYSCTL -e -q -w net.irda.discovery=0
    fi

    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
    echo "."
    ;;
#reload)
#   ;;
restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;
*)
    N=/etc/init.d/$PACKAGE
    echo "Usage: $N {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
