#!/bin/sh /etc/rc.common
START=99

boot() {
    #logger "email:boot"
    start
    return 0
}

APP="email"
NAME=emaild
PIDFILE=/var/run/$NAME.pid
#This is the command to be run, give the full pathname
DAEMON=/bin/emaild

lock() {
    until [[ -f /tmp/system.initialized && -f /tmp/system.evo.initialized ]]
    do
        #logger -p "user.debug" "$APP$1 $$ : waiting for system to be initialized"
        sleep 5
    done
    while true; do
        if mkdir /var/lock/$APP; then
            #logger -p "user.debug" "$APP$1 $$ : lock"
            break
        else
            #logger -p "user.debug" "$APP$1 $$ : wait"
            usleep 500000
        fi
    done
}

unlock() {
    #logger -p "user.debug" "$APP$1 $$ : unlock"
    rm -rf  /var/lock/$APP
}

start() {
    [ -f /proc/device-tree/lantronix/ftm_mode ] && {
	ftm_mode=`cat /proc/device-tree/lantronix/ftm_mode`
	[ $ftm_mode != "0" ] && return
    }
    enable=`uci get email.status.state`
    #logger "email:start $enable"
    if [[ "$enable" == "1" ]]; then
         #commands to launch application
        start-stop-daemon -S  -m -p $PIDFILE -x $DAEMON&
    fi
    lock
    ltrx_mhcfgupdate email
    unlock
}

stop() {
    #logger "email:stop"
    # commands to kill application
    pidof emaild | xargs kill -9 2>/dev/null
    rm -f  /var/run/emaild.pid
}

reload() {
    #logger "email:reload"
    stop
    start
}
