#!/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 "***************************************************************************************"
 
}
MINVERSIONTOFLASH=0104
checkFpgaVersion () {
    for i in $(getchannels -a 2>&1| grep '^0\.[a-z]/X/CFG');do
        version=$(echo fpga_version | confTool /dev/ilid ${i} | head -1 | cut -f 3 -d ' ')
        if [ -z "${version}" -o ${version} -lt ${MINVERSIONTOFLASH} ];then
			fpgaisold=true
			logFpgaError
       fi
    done
}

. /tmp/cmdline

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

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


# source check_callp
if test -f /usr/fb/check_callp.sh ; then
    . /usr/fb/check_callp.sh
else
    touser "Error: check_callp.sh is missing"
    logger -t start "Error: check_callp.sh is missing"
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 PATH=$PATH" >> /tmp/cmdline
    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
    sleep 5
done


#
# check mode
#
BOXMODE="gate"


ulimit -c 100000
ulimit -n 32768
ulimit -s unlimited

checkFpgaVersion
while test 1 -eq 1; do
#
# 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 test "$DEVELOP" = "" ; then
        /usr/fb/bin/sipisdn/callp 2>&1 | logger -t callp -p local5.notice &
    else
        /usr/fb/bin/sipisdn/callp 2>&1 | tee /smbshare/callp.0 &
    fi
    
    sleep 10

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

    sleep 50
    
    #
    # main loop
    #
    while true; do
        sleep 20
        if ! check_callp_tasks; then
	    break
	fi
	if $fpgaisold; then
	    logFpgaError
	fi
    done
    killcallp
#
    logger -s  start "performing restart"
    sleep 2
    
done
