#!/bin/sh
#
# On Eichrecht locked systems, where /etc/mdev.conf can't be
# changed anymore and thus mdev_router can't be started via
# mdev events, start a daemon that checks for new and vanished
# devices at regular intervals (every 3 seconds) and invokes
# mdev_router when they are detected. With this the hotplug
# system also works on these systems.


# Do nothing on newer systems where /etc/mdev.conf directly
# invokes /opt/ebee/etc/mdev.action and thus mdev_router.

if grep -q '/opt/ebee/etc/mdev\.action' /etc/mdev.conf 2>/dev/null
then
    exit 0
fi

PIDFILE=/var/run/dev_checker.pid

case "$1" in
    start)
        echo -n "Starting dev_checker: "
        start-stop-daemon -S -q -m -b -p $PIDFILE -x /opt/ebee/usr/sbin/dev_checker
        [ "$?" = "0" ] && echo "done" || echo "failed"
        ;;

    stop)
        echo -n "Stopping dev_cheker: "
        start-stop-daemon -K -q -s SIGTERM -p $PIDFILE
        [ "$?" = "0" ] && echo "done" || echo "failed"
        rm -f $PIDFILE
        ;;

    restart|reload)
        "$0" stop
        "$0" start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac
