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

run_mentohust()
{
    local enable
    local username
    local password
    local ifname
    local cmd
    config_get_bool enable $1 enable
    config_get username $1 username
    config_get password $1 password
    config_get ifname $1 ifname

    if [ $enable ] && [ $username ] && [ $password ] && [ $ifname ]; then
        local pinghost
        local startmode
        local dhcpmode
        local ipaddr
        local mask
        local gateway
        local dns
        local timeout
        local echointerval
        local restartwait
        local shownotify
        local version
        local datafile
        local dhcpscript

        config_get pinghost $1 pinghost
        config_get startmode $1 startmode
        config_get dhcpmode $1 dhcpmode
        config_get ipaddr $1 ipaddr
        config_get mask $1 mask
        config_get gateway $1 gateway
        config_get dns $1 dns
        config_get timeout $1 timeout
        config_get echointerval $1 echointerval
        config_get restartwait $1 restartwait
        config_get shownotify $1 shownotify
        config_get version $1 version
        config_get datafile $1 datafile
        config_get dhcpscript $1 dhcpscript

        if [ "$ipaddr" != "" ]; then cmd=$cmd" -i"$ipaddr;fi
        if [ "$mask" != "" ]; then cmd=$cmd" -m"$mask;fi
        if [ "$pinghost" != "0.0.0.0" ] &&  [ "$pinghost" != "" ]; then cmd=$cmd" -o"$pinghost;fi
        if [ "$startmode" != "0" ] &&  [ "$startmode" != "" ]; then cmd=$cmd" -a"$startmode;fi
        if [ "$dhcpmode" != "0" ] &&  [ "$dhcpmode" != "" ]; then cmd=$cmd" -d"$dhcpmode;fi
        if [ "$gateway" != "0.0.0.0" ] &&  [ "$gateway" != "" ]; then cmd=$cmd" -g"$gateway;fi
        if [ "$dns" != "0.0.0.0" ] &&  [ "$dns" != "" ]; then cmd=$cmd" -s"$dns;fi
        if [ "$timeout" != "8" ] &&  [ "$timeout" != "" ]; then cmd=$cmd" -t"$timeout;fi
        if [ "$echointerval" != "30" ] &&  [ "$echointerval" != "" ]; then cmd=$cmd" -e"$echointerval;fi
        if [ "$restartwait" != "15" ] &&  [ "$restartwait" != "" ]; then cmd=$cmd" -r"$restartwait;fi
        if [ "$shownotify" != "5" ] &&  [ "$shownotify" != "" ]; then cmd=$cmd" -y"$shownotify;fi
        if [ "$version" != "0.00" ] &&  [ "$version" != "" ]; then cmd=$cmd" -v"$version;fi
        if [ "$datafile" != "/etc/mentohust/" ] &&  [ "$datafile" != "" ]; then cmd=$cmd" -f"$datafile;fi
        if [ "$dhcpscript" != "udhcpc -i" ] &&  [ "$dhcpscript" != "" ]; then cmd=$cmd" -c'"$dhcpscript"'";fi

        /bin/ash -c "mentohust -u$username -p$password -n$ifname -b3 -w $cmd"
    else
        /bin/ash -c "mentohust -k"
    fi
}

start()
{
    config_load mentohust
    config_foreach run_mentohust mentohust
}

stop()
{
    kill `pidof mentohust | sed "s/$$//g"`
}

restart()
{
    mentohust -k
    sleep 1
    config_load mentohust
    config_foreach run_mentohust mentohust
}
