#!/bin/sh
#
# Copyright 2006-2008 Meru Networks
# All rights reserved.
#
# icrd configuration
#
# chkconfig: 45 95 5
# description: Meru icrd

PROC=/opt/meru/bin/icrd

START_DELAY=.2

MNAME=`basename $PROC`
PIDFILE=/opt/wlan/var/run/${MNAME}.pid
LOG=/opt/wlan/var/log/${MNAME}.log

if [ -f /etc/init.d/meru-functions ]; then
        . /etc/init.d/meru-functions
else
        echo "/etc/init.d/meru-functions must exist to use $0"; exit 1
fi

start() {

    vrfy_combo `cat $PIDFILE 2>/dev/null` $PROC && {
        return
    }

    echo "============ $0 start =================" >> $LOG
    modprobe ipip > /dev/null 2>&1
    echo 1 > /proc/sys/net/ipv4/ip_forward
    $PROC >> $LOG 2>&1 &

    sleep $START_DELAY
    write_pid
    vrfy `cat $PIDFILE 2>/dev/null` || warning "$PROC did not start. Will try again."
}

stop() {
    echo "============ $0 stop =================" >> $LOG
    die "$PROC"
    die `cat $PIDFILE 2>/dev/null`

    rmmod ipip >/dev/null 2>&1
    rm -f $PIDFILE
}

case "$1" in
    start)
        if vrfy monit; then
            start_with_monit $MNAME
        else
            start
        fi
        ;;
    stop)
        if vrfy monit; then
            stop_with_monit $MNAME
        else
            stop
        fi
        ;;
    restart)
        if vrfy monit; then
            restart_with_monit $MNAME
        else
            stop
            start
        fi
        ;;
    mstart)
        start
        ;;
    mstop)
        stop
        ;;
    *)
        /bin/echo $"Usage: $prog {start|stop|restart}"
        exit 1
esac

exit 0
