#!/bin/bash
exec 2>/dev/null


. /tmp/cmdline 

startS2MIsdnAtomBox() {
    genpattern | writetrans /dev/ilid 3.a/B1 >/tmp/selftest_isdn_3.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.b/B1 >/tmp/selftest_isdn_3.b_write.log 2>&1 &

    readtrans /dev/ilid 3.a/B1 | checkpattern >/tmp/selftest_isdn_3.a_read.log 2>&1 &
    readtrans /dev/ilid 3.b/B1 | checkpattern >/tmp/selftest_isdn_3.b_read.log 2>&1 &
}

startS0IsdnAtomBox() {
    checkl1 /dev/ilid 1.a/D te >/dev/null
    checkl1 /dev/ilid 1.b/D nt >/dev/null
    checkl1 /dev/ilid 1.c/D te >/dev/null
    checkl1 /dev/ilid 1.d/D nt >/dev/null

    checkl1 /dev/ilid 1.a/D 1 >/dev/null
    checkl1 /dev/ilid 1.b/D 1 >/dev/null
    checkl1 /dev/ilid 1.c/D 1 >/dev/null
    checkl1 /dev/ilid 1.d/D 1 >/dev/null

    genpattern | writetrans /dev/ilid 1.a/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.b/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.c/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.d/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &

    readtrans /dev/ilid 1.a/B1 | checkpattern >/tmp/selftest_isdn_1.a_read.log 2>&1 &
    readtrans /dev/ilid 1.b/B1 | checkpattern >/tmp/selftest_isdn_1.b_read.log 2>&1 &
    readtrans /dev/ilid 1.c/B1 | checkpattern >/tmp/selftest_isdn_1.c_read.log 2>&1 &
    readtrans /dev/ilid 1.d/B1 | checkpattern >/tmp/selftest_isdn_1.d_read.log 2>&1 &
}

startS2MIsdnMgb() {
    genpattern | writetrans /dev/ilid 1.a/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.b/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &

    readtrans /dev/ilid 1.a/B1 | checkpattern >/tmp/selftest_isdn_1.a_read.log 2>&1 &
    readtrans /dev/ilid 1.b/B1 | checkpattern >/tmp/selftest_isdn_1.b_read.log 2>&1 &
}

startS0IsdnMgb() {
    checkl1 /dev/ilid 3.a/D te >/dev/null
    checkl1 /dev/ilid 3.b/D nt >/dev/null
    checkl1 /dev/ilid 3.c/D te >/dev/null
    checkl1 /dev/ilid 3.d/D nt >/dev/null

    checkl1 /dev/ilid 3.a/D 1 >/dev/null
    checkl1 /dev/ilid 3.b/D 1 >/dev/null
    checkl1 /dev/ilid 3.c/D 1 >/dev/null
    checkl1 /dev/ilid 3.d/D 1 >/dev/null

    genpattern | writetrans /dev/ilid 3.a/B1 >/tmp/selftest_isdn_3.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.b/B1 >/tmp/selftest_isdn_3.b_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.c/B1 >/tmp/selftest_isdn_3.c_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.d/B1 >/tmp/selftest_isdn_3.d_write.log 2>&1 &

    readtrans /dev/ilid 3.a/B1 | checkpattern >/tmp/selftest_isdn_3.a_read.log 2>&1 &
    readtrans /dev/ilid 3.b/B1 | checkpattern >/tmp/selftest_isdn_3.b_read.log 2>&1 &
    readtrans /dev/ilid 3.c/B1 | checkpattern >/tmp/selftest_isdn_3.c_read.log 2>&1 &
    readtrans /dev/ilid 3.d/B1 | checkpattern >/tmp/selftest_isdn_3.d_read.log 2>&1 &
}

startS2MIsdnS2MPCIE() {
    genpattern | writetrans /dev/ilid 1.a/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.b/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &

    readtrans /dev/ilid 1.a/B1 | checkpattern >/tmp/selftest_isdn_1.a_read.log 2>&1 &
    readtrans /dev/ilid 1.b/B1 | checkpattern >/tmp/selftest_isdn_1.b_read.log 2>&1 &
}

startS2MIsdnQUADE1() {
    genpattern | writetrans /dev/ilid 1.a/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.b/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.a/B1 >/tmp/selftest_isdn_3.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 3.b/B1 >/tmp/selftest_isdn_3.b_write.log 2>&1 &

    readtrans /dev/ilid 1.a/B1 | checkpattern >/tmp/selftest_isdn_1.a_read.log 2>&1 &
    readtrans /dev/ilid 1.b/B1 | checkpattern >/tmp/selftest_isdn_1.b_read.log 2>&1 &
    readtrans /dev/ilid 3.a/B1 | checkpattern >/tmp/selftest_isdn_3.a_read.log 2>&1 &
    readtrans /dev/ilid 3.b/B1 | checkpattern >/tmp/selftest_isdn_3.b_read.log 2>&1 &
}

startS0IsdnSingleUnit4S0() {
    checkl1 /dev/ilid 1.a/D te >/dev/null
    checkl1 /dev/ilid 1.b/D nt >/dev/null
    checkl1 /dev/ilid 1.c/D te >/dev/null
    checkl1 /dev/ilid 1.d/D nt >/dev/null

    checkl1 /dev/ilid 1.a/D 1 >/dev/null
    checkl1 /dev/ilid 1.b/D 1 >/dev/null
    checkl1 /dev/ilid 1.c/D 1 >/dev/null
    checkl1 /dev/ilid 1.d/D 1 >/dev/null

    genpattern | writetrans /dev/ilid 1.a/B1 >/tmp/selftest_isdn_1.a_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.b/B1 >/tmp/selftest_isdn_1.b_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.c/B1 >/tmp/selftest_isdn_1.c_write.log 2>&1 &
    genpattern | writetrans /dev/ilid 1.d/B1 >/tmp/selftest_isdn_1.d_write.log 2>&1 &

    readtrans /dev/ilid 1.a/B1 | checkpattern >/tmp/selftest_isdn_1.a_read.log 2>&1 &
    readtrans /dev/ilid 1.b/B1 | checkpattern >/tmp/selftest_isdn_1.b_read.log 2>&1 &
    readtrans /dev/ilid 1.c/B1 | checkpattern >/tmp/selftest_isdn_1.c_read.log 2>&1 &
    readtrans /dev/ilid 1.d/B1 | checkpattern >/tmp/selftest_isdn_1.d_read.log 2>&1 &
}

rm -f /tmp/selftest_isdn*

if [ "${HWTYPE}" = "AtomBox" ] ; then
    echo "checking AtomBox"
    echo "stopping running Tasks"
    omgstopall 2>/dev/null >/dev/null
    echo "loading modules" 
    omgstartilid 2>/dev/null >/dev/null
    
    #set internal clock as master on all three prioritys
    echo "clk_master 8 8 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null
    
    echo "starting S2M ports"
    startS2MIsdnAtomBox
    echo "starting S0  ports"
    startS0IsdnAtomBox

    sleep 30
    killall checkpattern
    sleep 1
    killall genpattern

    for i in /tmp/selftest_isdn_[13].[abcd]_read.log; do
        id=$(echo ${i} | cut -d "_" -f 3)
        device=$(getchannels -d 2>& 1 | grep -e "^${id}")
        port=$(echo ${device} | cut -d "/" -f 1) 
        type=$(echo ${device} | cut -d "/" -f 3)
        
        if grep -q "\*\*\*\*\*\*\*\*\*\*\*\*\\*\*\*\*\*\*\*\*\*" ${i} ;then
            state="OK"
        else
            state="failure"
        fi

        printf "    %-3.3s %s %s\n" ${type} ${port} ${state}
    done

    #following is standard setting
    echo "clk_master 0 1 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null

    echo restarting normal service
    omgrestart 2>/dev/null >/dev/null
    echo done
fi

if [ "${HWTYPE}" = "MGB" ] ; then
    echo "checking MGB"
    echo "stopping running Tasks"
    omgstopall 2>/dev/null >/dev/null
    echo "loading modules" 
    omgstartilid 2>/dev/null >/dev/null
    
    #echo "clk_show" | confTool /dev/ilid 0.a/X

    #set internal clock as master on all three prioritys
    echo "set clock master" 
    echo "clk_master 8 8 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null
    
    echo "set termination S0 ports"
    i2cutil -y ${I2CBUS} 0x08 0x0a 0x0a b 2>/dev/null >/dev/null

    echo "starting S2M ports"
    startS2MIsdnMgb
    echo "starting S0  ports"
    startS0IsdnMgb

    sleep 30
    killall checkpattern
    sleep 1
    killall genpattern

    for i in /tmp/selftest_isdn_[13].[abcd]_read.log; do
        id=$(echo ${i} | cut -d "_" -f 3)
        device=$(getchannels -d 2>& 1 | grep -e "^${id}")
        port=$(echo ${device} | cut -d "/" -f 1) 
        type=$(echo ${device} | cut -d "/" -f 3)
        
        if grep -q "\*\*\*\*\*\*\*\*\*\*\*\*\\*\*\*\*\*\*\*\*\*" ${i} ;then
            state="OK"
        else
            state="failure"
        fi

        printf "    %-3.3s %s %s\n" ${type} ${port} ${state}
    done

    #following is standard setting
    echo "clk_master 0 1 8" | confTool /dev/ilid 0.a/X >/dev/null

    echo restarting normal service
    omgrestart 2>/dev/null >/dev/null
    echo done
fi

if [ "$HWTYPE" = "QUADE1" ] ; then
    echo "checking QUADE1"
    echo "stopping running Tasks"
    omgstopall 2>/dev/null >/dev/null
    echo "loading modules" 
    omgstartilid 2>/dev/null >/dev/null
    
    #set internal clock as master on all three prioritys
    echo "clk_master 8 8 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null
    
    echo "starting S2M ports"
    startS2MIsdnQUADE1

    sleep 30
    killall checkpattern
    sleep 1
    killall genpattern

    for i in /tmp/selftest_isdn_[13].[abcd]_read.log; do
        id=$(echo ${i} | cut -d "_" -f 3)
        device=$(getchannels -d 2>& 1 | grep -e "^${id}")
        port=$(echo ${device} | cut -d "/" -f 1) 
        type=$(echo ${device} | cut -d "/" -f 3)
        
        if grep -q "\*\*\*\*\*\*\*\*\*\*\*\*\\*\*\*\*\*\*\*\*\*" ${i} ;then
            state="OK"
        else
            state="failure"
        fi

        printf "    %-3.3s %s %s\n" ${type} ${port} ${state}
    done


    #following is standard setting
    echo "clk_master 0 1 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null

    echo restarting normal service
    omgrestart 2>/dev/null >/dev/null
    echo done
fi

if [ "$HWTYPE" = "S2MPCIE" ] ; then
    echo "checking S2MPCIE"
    echo "stopping running Tasks"
    omgstopall 2>/dev/null >/dev/null
    echo "loading modules" 
    omgstartilid 2>/dev/null >/dev/null
    
    #set internal clock as master on all three prioritys
    echo "clk_master 8 8 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null
    
    echo "starting S2M ports"
    startS2MIsdnS2MPCIE

    sleep 30
    killall checkpattern
    sleep 1
    killall genpattern

    for i in /tmp/selftest_isdn_[13].[abcd]_read.log; do
        id=$(echo ${i} | cut -d "_" -f 3)
        device=$(getchannels -d 2>& 1 | grep -e "^${id}")
        port=$(echo ${device} | cut -d "/" -f 1) 
        type=$(echo ${device} | cut -d "/" -f 3)
        
        if grep -q "\*\*\*\*\*\*\*\*\*\*\*\*\\*\*\*\*\*\*\*\*\*" ${i} ;then
            state="OK"
        else
            state="failure"
        fi

        printf "    %-3.3s %s %s\n" ${type} ${port} ${state}
    done

    #following is standard setting
    echo "clk_master 0 8 8" | confTool /dev/ilid 0.a/X 2>/dev/null >/dev/null

    echo restarting normal service
    omgrestart 2>/dev/null >/dev/null
    echo done
fi

if [ "$HWTYPE" = "SingleUnit4S0" ] ; then
    echo "checking SingleUnit4S0"
    echo "stopping running Tasks"
    omgstopall 2>/dev/null >/dev/null
    echo "loading modules" 
    omgstartilid 2>/dev/null >/dev/null
    
    echo "starting S0 ports"
    startS0IsdnSingleUnit4S0

    sleep 30
    killall checkpattern
    sleep 1
    killall genpattern

    for i in /tmp/selftest_isdn_[13].[abcd]_read.log; do
        id=$(echo ${i} | cut -d "_" -f 3)
        device=$(getchannels -d 2>& 1 | grep -e "^${id}")
        port=$(echo ${device} | cut -d "/" -f 1) 
        type=$(echo ${device} | cut -d "/" -f 3)
        
        if grep -q "\*\*\*\*\*\*\*\*\*\*\*\*\\*\*\*\*\*\*\*\*\*" ${i} ;then
            state="OK"
        else
            state="failure"
        fi

        printf "    %-3.3s %s %s\n" ${type} ${port} ${state}
    done

    echo restarting normal service
    omgrestart 2>/dev/null >/dev/null
    echo done
fi
