#!/bin/bash


ask () {
    read -p "$@ " yn
    if [ "$yn" = 'y' -o "$yn" = 'Y' -o "$yn" = 'j' -o "$yn" = 'J' -o "$yn" = '' ] ; then
        return 0
    else
	exit 1
    fi   
}



getlic () {

    type=${1}
    sn=${2}
    hwsn=${3}

    # Check for existing license
    liccnt=$(ls /data/*.flf 2>/dev/null|wc -l)
    if [ ${liccnt} -ne 0 ] ; then
        echo ""
        echo "Found license:"
        pushd /data > /dev/null
        ls *.flf
        popd > /dev/null
        echo "Warning! Saying 'yes' will remove this license"
        ask "Continue?"
        rm -f /data/*.flf
    fi
    
    rm -f getlicense.err
    
    wget.omg -q -N --content-disposition http://librorix/sernum/getlicense.php?type=${type}\&sn=${sn}\&hwsn=${hwsn}
    
    if [ -e getlicense.err ] ; then
        echo "getlicense failed:"
        cat getlicense.err
        exit 1
    fi
    
    if [ -e ${type}-*.flf ] ; then
        sudo mv ${type}-*.flf /data
    else
        echo "I'm goofed: got license but no license found"
        exit 1
    fi

    rm -f getlicense.err
    
}


isnews2m=$(sudo lspci -n | grep -c "1011:f00[12]")
if [ ${isnews2m} -ge 1 ] ; then 
    echo "Sorry, this test is for S0 boxes only"
fi

if sudo vpd-dump >/dev/null 2>&1 ; then
    echo "Sorry, this test is for S0 boxes only"
    exit 1
fi

if which wdstop >/dev/null 2>&1; then
   wdstop
fi



echo "Looking for serial number ..."

sernum=i2crdserial
rc=$?
if [ ${rc} -eq 2 ]; then
#     Oups
    echo "Error: Getting serial number failed (No eeprom?)."
    exit 1
fi

if [ -z ${sernum} ]; then
        # Mmh, Hamlet
    echo "No serial in eeprom found"
    exit 1
fi

if [ ${rc} -eq 1 ]; then
    echo "EEPROM seems to be uninitialized, calling i2cconfig"
    ask "eeprom jumper set?"
    i2cconfig
    ask "Continue?"
fi

sernum=$(i2crdserial)
if [ -z "${sernum}" ] ; then
    echo "Ouups, no serial number found, what happened?"
    exit 1
fi
hwsernum=$(i2crdhwserial)
if [ -z "${hwsernum}" ] ; then
    echo "Ouups, no HW serial number found, what happened?"
    exit 1
fi


# Serial o.k. so let's request a licence ...


if ls /data/*.flf > /dev/null 2>&1; then
    echo "License found, skipping this part"
    
rm -f getlicense.err

echo "Requesting license for line test"
getlic 106 ${sernum} ${hwsernum}

fi

cp /usr/fb/SingleUnit4S0.lua /data/config.lua

fail=0
if ! netstat -natp | grep -q ":3217" ; then
    echo "No process listen on 3217"
    fail=1
fi
if ! netstat -natp | grep -q ":3218" ; then
    echo "No process listen on 3218"
    fail=1
fi
if ! netstat -natp | grep -q ":3219" ; then
    echo "No process listen on 3219"
    fail=1
fi
if ! netstat -natp | grep -q ":3220" ; then
    echo "No process listen on 3220"
    fail=1
fi

if [ ${fail} -eq 1 ] ; then
    echo "Trying to restart"
    sleep 3
    /etc/init.d/omg.setup
    /etc/init.d/omg.start
fi

echo "Taking a nap ..."
sleep 10
echo "Testing again"
if ! netstat -natp | grep -q ":3217" ; then
    echo "Still no process listen on 3217, I give up."
    exit
fi
if ! netstat -natp | grep -q ":3218" ; then
    echo "Still no process listen on 3218, I give up."
    exit
fi
if ! netstat -natp | grep -q ":3219" ; then
    echo "Still no process listen on 3219, I give up."
    exit
fi
if ! netstat -natp | grep -q ":3220" ; then
    echo "Stil no process listen on 3220, I give up."
    exit
fi


# At this point we have a license, a (hopefully) correct config.lua, and callp is listening

echo "Starting test ..."

ltest -a

rm -f /data/*.flf
rm -f /data/config.lua

ask "Continue?"

echo "Select license type for box: 2 b-channels   ->  1     S0 8 b-channels   ->  2"
answer=""

while test -z "$answer" ; do
    showselection
    read -p "Enter number (or q to quit)? " answer
    
    case $answer in
            
        "1") Licensetype=105
             cfgfile=SingleUnit1S0.lua
             bt="1S0"
             ;;
        "2") Licensetype=106
             cfgfile=SingleUnit4S0.lua
             bt="4S0"
             ;;
        "q") exit 0
            ;;
        *) answer=""
        esac
done



getlic ${Licensetype} ${sernum} ${hwsernum}
echo "License for ${bt} is installed"
cp /usr/fb/${cfgfile} /data/config.lua
echo "config.lua installed"

exit



