#!/bin/bash
#

function exists {
   for x in $*; do
     test -e "$x" && return 0;
   done
   return 1
}

function countBChannels {
    pgrep -laf "just check if -laf is available" >/dev/null 2>/dev/null;
    rc=$?;
    if [ "$rc" == "2" ]; then
        # -laf is not available
        pgreppar="-lf"
    else
        pgreppar="-laf"
    fi

    #all trunk related calls
    let calls=$(pgrep ${pgreppar} ".*\.prc .*--trunk=${ilidslot}.${ilidindex}/" | grep -v pgrep | grep -v -E "isdn.prc|readchannel.prc" | wc -l 2>/dev/null)
    #now all not trunk related calls
    let calls+=$(pgrep ${pgreppar} ".*\.prc .*--interface=${ilid}/B[0-9]*/[^T]" | grep -v pgrep | grep -v -E "isdn.prc|readchannel.prc" | wc -l 2>/dev/null)
    #interconnect.prc has maybe two channels on same interface allocated
    let calls+=$(pgrep ${pgreppar} "/usr/fb/bin/interconnect.prc .*=${ilid}/B.*=${ilid}/B*" | (let c=0; while read line; do if ! (for p in $line; do echo $p; done) | sed -e "s/--interface=//" -e "s/--second_interface=//" | uniq -c | grep "^ *2" >/dev/null; then let c+=1; fi ; done ; echo $c))
}

c=$(/usr/bin/getchannels -d -v 2>&1 | grep "/D")

for i in $c; do
    ilid=$(echo $i | sed -e "s/\(.*\)\/D\/.*/\1/")
    ilidslot=$(echo ${ilid} | cut -d "." -f 1)
    ilidindex=$(echo ${ilid} | cut -d "." -f 2)
    ilidtype=$(echo $i | cut -d '/' -f 3)

    l1state=OFF
    l2state=OFF
    calls=0

    if [ "${ilidtype}" = "TRUNK" ]; then
        l1state=UP
        l2state=ESTABLISHED
        if [ -e "/tmp/TRUNK-${ilidslot}.${ilidindex}" ]; then
            if grep -q "\-\-status=\"active\"" "/tmp/TRUNK-${ilidslot}.${ilidindex}" ; then
                l2state="REGISTERED"
            elif grep -q "\-\-status=\"failed\"" "/tmp/TRUNK-${ilidslot}.${ilidindex}" ; then
                l2state="REGISTER-FAILED"
            fi
        fi
        countBChannels
    else
        count=$(ps -C isdn.prc l | grep "\-\-slot=${ilidslot} \-\-slot-index=${ilidindex}" | wc -l)
        if [ "${count}" != "0" ]; then
            l1state=$(checkl1 /dev/ilid $ilid/D | grep "Layer1 activated" | wc -l)
            if [ "${l1state}" == "0" ]; then
                l1state=DOWN
            else
                l1state=UP
                if exists "/tmp/L2State-$ilid-D*" ;then
                    calls=0
                    l2state=$(grep "MULTIPLE_FRAME_ESTABLISHED" /tmp/L2State-$ilid-D-* 2>/dev/null | wc -l)
                    if [ "${l2state}" == "0" ]; then
                        l2state=IDLE;
                    else
                        l2state=ESTABLISHED;
                        countBChannels
                    fi
                fi
            fi
        fi
    fi


    echo ILID:$ilid, L1:$l1state, L2:$l2state, CALLS:$calls;
done

c=$(/usr/bin/getchannels -d -V 2>&1 | grep "/D")

for i in $c; do
    ilid=$(echo $i | sed -e "s/\(.*\)\/D\/.*/\1/")
    ilidslot=$(echo ${ilid} | cut -d "." -f 1)
    ilidindex=$(echo ${ilid} | cut -d "." -f 2)
    ilidtype=$(echo $i | cut -d '/' -f 3)

    l1state=OFF
    l2state=OFF
    calls=0

    if [ "${ilidtype}" = "TRUNK" ]; then
        echo ILID:$ilid, L1:$l1state, L2:$l2state, CALLS:$calls;
    fi
done
