#!/bin/sh
#
# Name: tmgr
# Date: 2015-03-08
# Author: Narayana Cheluvaraju
# Copyright 2015, Nokia.
#

# change umask to 0 to solve problems related to LLPD
umask 0

# enable the possibility to get core files of binaries started from this script
ulimit -c unlimited

OPT_DIR="/opt/trs"
CCS_DIR="/opt/CCS"
TRS_BIN_DEST="${OPT_DIR}/bin"
TRS_ETC_DEST="${OPT_DIR}/etc"
PROGRAM_RC="${TRS_ETC_DEST}/tmgr_start.rc.fsmr3fct"
TMGR_CONDITIONS='FCTA'

# Individual Program information
PROGRAM_START="${TRS_BIN_DEST}/ukTmgr"
PROGRAM_ENVINFO="${TRS_BIN_DEST}/test_envInfo"

# Verify programs are installed
test -f $PROGRAM_START || exit 1
test -f $PROGRAM_ENVINFO || exit 1

# Load networking script
NETWORKING="${TRS_ETC_DEST}/networking"
[ -f "${NETWORKING}" ] && . "${NETWORKING}"

# MT related directories
TRS_DATA="/ffs/run/trs_data"
TRS_ACT_DEST="${TRS_DATA}/active"
TAUF_CONF="${TRS_ACT_DEST}/tauf_config"
TAUF_SCRIPT="${TRS_BIN_DEST}/tauf/target"

export PATH="${TRS_BIN_DEST}:/sbin:/bin:$PATH"
export LD_LIBRARY_PATH="/usr/lib:${CCS_DIR}:/usr/lib64:/lib64:/lib:$LD_LIBRARY_PATH"

enableRioPorts() 
{
    local HOST_BIT=`cat /sys/class/rio-adapter/rio0/host_bit`
    local tries
    if [ "$HOST_BIT" -eq 0 ]; then
        let tries=6
        while [ "$(cat /sys/class/rio-adapter/rio0/base_id | cut -c 7-10)" = "ffff" ]; do
            echo "waiting for RIO enumeration ($(cat /sys/class/rio-adapter/rio0/base_id):$(cat /sys/class/rio-adapter/rio0/port_ok))"
            sleep 10
            let --tries
            if [ $tries -lt 0 ]; then
                break
            fi
        done
        if [ "$(cat /sys/class/rio-adapter/rio0/base_id | cut -c 7-10)" = "ffff" ]; then
            echo "!!!!!!!!!!!!!!! RIO enumeration problems ($(cat /sys/class/rio-adapter/rio0/base_id):$(cat /sys/class/rio-adapter/rio0/port_ok)) !!!!!!!!!!!!!!!"
        fi
        # execute rio interface configuration script (BTS PS)
        echo "Execute RIO network setup script (BTS PS)..."
        /etc/rc.d/init.d/common.d/setuprionetwork
        echo "Execute RIO network setup script (BTS PS): DONE"
    fi

    if [ -e /ffs/run/addons/rio-elog.py ]; then
        set -o pipefail

        ret=0
        let i=3

        while [ $i -gt 0 ]; do
            PYTHONHOME=/usr /usr/bin/python /ffs/run/addons/rio-elog.py -p 4 -e1 0x1000 </dev/null
            if [ $ret -eq 0 ]; then
                ret=$?
            fi
            PYTHONHOME=/usr /usr/bin/python /ffs/run/addons/rio-elog.py -p 5 -e1 0x1000 </dev/null
            if [ $ret -eq 0 ]; then
                ret=$?
            fi
            PYTHONHOME=/usr /usr/bin/python /ffs/run/addons/rio-elog.py -p 6 -e1 0x1000 </dev/null
            if [ $ret -eq 0 ]; then
                ret=$?
            fi
            let --i
        done

        if [ $ret -ne 0 ]; then
            echo "####################################################"
            echo "##"
            echo "## disabling of Link Layer Security failed"
            echo "## traffic might not work"
            echo "##"
            echo "####################################################"
        fi
    fi
}

check_config()
{
    # Copy TAUF config files to config directory if not.
    if [ ! -d $TAUF_CONF ]; then
        mkdir -p $TAUF_CONF
        chmod 644 $TAUF_CONF
    fi
    if [ ! -f $TAUF_CONF/target.hconf ]; then
        cp -f $TRS_ETC_DEST/tauf_target.hconf $TAUF_CONF/target.hconf >/dev/null 2>&1
        chmod 777 $TAUF_CONF/target.hconf >/dev/null 2>&1
    fi

    # Dynamic Support from TAUF
    if [ ! -f $TAUF_CONF/tauf_dynamic.sh -a -f $TAUF_SCRIPT/tauf_dynamic.sh ]; then
        cp -f $TAUF_SCRIPT/tauf_dynamic.sh $TAUF_CONF/tauf_dynamic.sh
        chmod 777 $TAUF_CONF/tauf_dynamic.sh
    fi
    # Get env's at Startup for MT.
    if [ -f $TAUF_SCRIPT/getenv.sh ]; then
        $TAUF_SCRIPT/getenv.sh
    fi
}

start_tmgr()
{
    local UNIT_TYPE=`${PROGRAM_ENVINFO} envInfoUnitType | head -1 | cut -d "_" -f2`
    local PROD_TYPE=`${PROGRAM_ENVINFO} envInfoProductType | head -1 | cut -d "_" -f2-3`
    echo "Starting on unit $UNIT_TYPE for $PROD_TYPE product"

    # start task manager, don't return
    local command="$PROGRAM_START -F -C $TMGR_CONDITIONS -s 6 -c 0 -f 0 -m 1 -l 4 $PROGRAM_RC"
    exec $command </dev/null
}

ipStackIndication()
{
    # send IP stack Indication : PR050152
    echo "*********Sending IP stack Indication"
    ${TRS_BIN_DEST}/sendIpStackIndication &
}

run_trs_fct_debugger()
{
     #Start the periodic fastpath counter collector script
     if [ -f "${TRS_ETC_DEST}/vtc_periodic_fplog_collector.sh" ]; then
        PERIOD=10 #in secs
        FILE_SIZE=500000 #in bytes
        HISTORY=3 #last 2 + current reset count
        echo "***Starting TRS FCT FP log collector SCRIPT $PERIOD $FILE_SIZE $HISTORY***"
        ${TRS_ETC_DEST}/vtc_periodic_fplog_collector.sh $PERIOD $FILE_SIZE $HISTORY &
        disown %+
     fi
}

create_pipe()
{
    local use_console=1
    local without_tee
    if which tee >/dev/null 2>&1; then
        without_tee=0
    else
        without_tee=1
    fi

    local without_console
    if [ -c /dev/console ] && echo 2>/dev/null >/dev/console; then
        without_console=0
    else
        without_console=1
    fi

    local pipe_end
    if [ $use_console -ne 0 ] && [ $without_tee -eq 0 ] && [ $without_console -eq 0 ]; then
        pipe_end='tee -a /dev/console'
    else
        pipe_end='cat'
    fi

    /bin/rm -f /tmp/ukTmgr_pipe
    /usr/bin/mkfifo -m 0600 /tmp/ukTmgr_pipe

    ($pipe_end < /tmp/ukTmgr_pipe 2>&1 | logger -t TRS_SW/tmgr) >/dev/null 2>&1 </dev/null &
    disown %+
}

main()
{
    enableRioPorts
    check_config
    ipStackIndication
    run_trs_fct_debugger
    start_tmgr
}

create_pipe
main >/tmp/ukTmgr_pipe 2>&1
