#!/bin/bash

abort () {
    exit 1
}

cd /usr/fb

if [ -e /usr/bin/omgiptables ];then
    /usr/bin/omgiptables
fi

#DEBUG=1

fpgaisold=false

tty > /dev/null
noninteractive=$?

touser() {
    if type usplash_write > /dev/null 2>&1; then
       usplash_write "TEXT $@" || true
    else
        if [ ${noninteractive} = 1 ] ; then
            # we're running non interactive
            echo  -e "${@}\r" > /dev/console
        else
            echo  -e "${@}"
        fi
    fi
}
logFpgaError (){
            logger "***************************************************************************************"
            logger "*** WARNING: The whole system needs a powercycle to activate the new FPGA firmware. ***"
            logger "***          The system has guaranteed malfunctions until you do a powercycle.      ***"
            logger "***************************************************************************************"

}

checkFpgaVersion () {

    card=$(i2crdtype)
    if [ "${card}" != "02"  -a "${card}" != "03" -a "${card}" != "04" ]; then
        # Hardware without FPGA.
        return
    fi
    for i in $(getchannels -a 2>&1| grep '^0\.[a-z]/X/CFG');do
        let version=0x$(echo fpga_version | confTool /dev/ilid ${i} | head -1 | cut -f 3 -d ' ')
        if i2cinfo -l | head | grep -q OMGA; then
            let minversiontoflash=0x010b
        elif [ "${card}" = "02" ]; then
            let minversiontoflash=0x0106
        elif [ "${card}" = "03" ]; then
            let minversiontoflash=0x106
        elif [ "${card}" = "04" ]; then
            let minversiontoflash=0x0106
        else
            # Wrong hardware.
            return
        fi

        if [ -z "${version}" -o ${version} -lt ${minversiontoflash} ];then
	    fpgaisold=true
	    logFpgaError
        fi
    done
}

startEyeSDN() {
    if [ -r /etc/init.d/eyesdn ]; then
        if ! service eyesdn status; then
            service eyesdn start
        fi
    fi
    if [ -r /etc/init.d/eyesdn-cols ]; then
        if ! service eyesdn-cols status; then
            service eyesdn-cols start
        fi
    fi
}

. /tmp/cmdline

# now put /usr/fb/bin into path
if ! echo  "$PATH" | grep -q /usr/fb/bin ;then
    export PATH=$PATH:/usr/fb/bin
fi

if [ "$HWFOUND" != "1" ] ; then
    logger "start: didn't start due to no HW found"
    exit 0
fi

for i in $(cat /proc/cmdline) ; do
    if test "$i" = "single" ; then
	logger "start: system is in recovery mode, will not run"
	exit 1
    fi
done

#checkFpgaVersion

# remove old logfiles
rm -f /tmp/JC-* > /dev/null 2>&1
rm -f /tmp/D* > /dev/null 2>&1


if test -f /data/config.lua  -a  -x /usr/bin/lua ; then
    if ! /usr/bin/lua /data/config.lua; then
	touser "start: config.lua ist corrupt!"
	logger -t start "config.lua ist corrupt!"
	mv /data/config.lua /data/config.lua.corrupt
	touser "start: config.lua renamed to config.lua.corrupt"
	logger -t start "config.lua renamed to config.lua.corrupt"
    fi
fi

if test "$MACHINE" = "" ; then
    MACHINE=$(uname -m)
    if test $DEBUG ; then echo "start: MACHINE is $MACHINE"; fi
fi

STARTPORT=3217

# tell user what services we provide

export FAX=1
export SMS=1
export VOICE=1

if test "$NATIVE" = "" ; then
    echo "export FAX=1" >> /tmp/cmdline
    echo "export SMS=1" >> /tmp/cmdline
    echo "export VOICE=1" >> /tmp/cmdline
fi

# Wait for ip to become ready

while test 1 -eq 1; do
    if ifconfig | grep -q eth0 ; then
        ipaddr=$(ifconfig  eth0 | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
        if [ ! -z "${ipaddr}" ] ; then
            break
        fi
    fi
    if ifconfig | grep -q eth1 ; then
        ipaddr=$(ifconfig  eth1 | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
        if [ ! -z "${ipaddr}" ] ; then
            break
        fi
    fi
    if ifconfig | grep -q bond0 ; then
        ipaddr=$(ifconfig  bond0 | grep 'inet addr:\|inet Adresse:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
        if [ ! -z "${ipaddr}" ] ; then
            break
        fi
    fi
    sleep 5
done


#
# check mode
#
BOXMODE="gate"

ulimit -c 1000000
ulimit -n 32768
ulimit -s unlimited

checkFpgaVersion

touch /tmp/callp.started

#
# start the jobcontrols
#
if ! test -x /usr/fb/bin/sipisdn/callp ; then
    logger -t start "sipisdn not found, exit"
    abort
fi

if test $DEBUG ; then echo "start: starting OMG as Gate"; fi

logger "start: /usr/fb/bin/sipisdn/callp"
cd /usr/fb/bin/sipisdn/


if [ "$HWTYPE" = "AtomBox" ]; then
    if [ ! -f /tmp/noled ] ;then echo "led 0 1008300" | tee /tmp/led | /usr/bin/confTool /dev/ilid 0.a/X >/dev/null 2>&1; fi
fi

export CALLPDEBUGLEVEL=$(if grep -q  string_callp_dbg /data/config.lua; then grep string_callp_dbg /data/config.lua | sed -e 's/[^"]*"//' -e 's/",*//'; else echo "0"; fi)
export CALLPDNSDEBUGLEVEL=$(if grep -q  string_dns_dbg /data/config.lua; then grep string_dns_dbg /data/config.lua | sed -e 's/[^"]*"//' -e 's/",*//'; else echo "0"; fi)

nohup bash -c "(while true; do /usr/fb/bin/sipisdn/callp --debug=${CALLPDEBUGLEVEL} --dnsdebug=${CALLPDNSDEBUGLEVEL} 2>&1 | logger -t callp -p local5.notice; sleep 2; done)" </dev/null >/dev/null 2>/dev/null &

startEyeSDN

echo "omgate started"
echo




