#!/bin/bash

abort () {
    exit 1
}

cd /usr/fb

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

#DEBUG=1

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
}

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
            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 "***************************************************************************************"
        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

test -e /usr/lib/libssl.so.0.9.8 && export LD_PRELOAD=/usr/lib/libssl.so.0.9.8

# remove old logfiles
if test $DEBUG ; then
    rm -f /tmp/JC-*
    rm -f /tmp/D*
else
    rm -f /tmp/JC-* > /dev/null 2>&1
    rm -f /tmp/D* > /dev/null 2>&1
fi

if test -f /data/config.lua  -a  -x /usr/fb/bin/lua ; then
    if ! /usr/fb/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
export PATH=$PATH:/usr/fb/bin

# 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

# load modules
omgstartilid

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
    sleep 5
done


# Start nhttpd
/etc/init.d/nhttpd.omg start

#
# check mode
#
BOXMODE="gate"


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

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 60
    
    #
    # main loop
    #
    while true; do
        sleep 20
        if ! check_callp_tasks; then
	       break
    	fi
        checkFpgaVersion

        pidofwdhole=$(ps ax | grep callp | grep -v grep | head -1 | sed -e 's/^ *\([0-9]*\).*/\1/')  
        if test -z "$pidofwdhole"; then
            echo "start: wdhole is missing. Restarting"
            logger "wdhole is missing. Restarting"
            /usr/bin/wdhole &
        fi        
    done
    killcallp
#
    logger -s  start "performing restart"
    sleep 2
    
done
