inside_lxc_container ()
{
    if [ -e /proc/1/cgroup ]; then
        /usr/bin/grep -q ':/lxc/' /proc/1/cgroup
        local res=$?
        if [ $res -ne 0 ]; then
            echo 0
        else
            echo 1
        fi
    else
        echo 0
    fi
}

enforce_reboot () {
    local KILLALL
    local RESETP3
    local RESETP3_PID
    local REBOOT
    local rc
    local tries
    local maxtime
    local in_container=$(inside_lxc_container)

    local ROOT_DIR
    local START_SCRIPT

    if [ -d '/tmp/SIM' ]; then
        ROOT_DIR='/tmp/SIM'
    else
        ROOT_DIR=''
    fi
 
    if [ -d "${ROOT_DIR}/sdm_selected" ]; then
        START_SCRIPT="${ROOT_DIR}/sdm_selected/start_script.conf"
    else
        START_SCRIPT="${ROOT_DIR}/ffs/run/trs_data/sim/start_script.conf"
    fi

    [ -f "${START_SCRIPT}" ] && . "${START_SCRIPT}"

    if [ -x '/opt/trs/bin/llpd' ]; then
        EXISTSPROCESS='/opt/trs/bin/llpd existsprocess'
        ISALIVEPID='/opt/trs/bin/llpd isalivepid'
        RESETP3='/opt/trs/bin/llpd resetP3'
        REBOOT='/opt/trs/bin/llpd reboot'
    else
        EXISTSPROCESS='killall -0'
        ISALIVEPID='kill -0'
        RESETP3='resetP3'
        if [ -x '/sbin/reboot' ]; then
            REBOOT='/sbin/reboot -f'
        else
            REBOOT='/bin/systemctl reboot -f'
        fi
    fi

    rc=1
    let tries=50
    while [ $tries -gt 0 ]; do
        if [ $in_container -ne 0 ] || $EXISTSPROCESS ukTmgr 2>/dev/null; then
            critErrHdlrFlag CEH set OFF
            critErrHdlrFlag CEH set ON

            $RESETP3 </dev/null 2>&1 &
            RESETP3_PID=${!}
            let maxtime=120
            while $ISALIVEPID $RESETP3_PID 2>/dev/null; do
                let --maxtime
                if [ $maxtime -lt 0 ]; then
                    break
                fi
                sleep 1
            done
            if [ !$ISALIVEPID $RESETP3_PID 2>/dev/null ]; then
                wait $RESETP3_PID
                rc=${?}
            else
                break
            fi
            if [ $rc -lt 128 ]; then
                break
            fi
        else
            break
        fi
        let --tries
    done
    case "${BTSOM_SIMULATED}" in
        yes|y|YES|Y)
            if [ $in_container -eq 0 ] && [ $rc -ne 0 ]; then
                nohup sh -c "sleep 5; $REBOOT" > /dev/null 2>&1 &
            fi
            ;;
        *)
            ;;
    esac

}

cache_control () {
    # fix IE bug during https access
    local STATUS=false;
    if [ `wasFsmr3` == 0 ] && [ `isFzm` == 0 ]; then
      if [ "$REMOTE_ADDR" != "127.0.0.1" ]; then
        STATUS=true
      fi
    else
      if [ "$SERVER_PORT" != "443" -a "$SERVER_PROTOCOL" != "HTTP/1.1" ]; then
        STATUS=true
      fi
    fi

    if [ "$STATUS" = 'true' ]; then
        cat <<EOF
Pragma: no-cache
Cache-control: no-cache
EOF
    else
        cat <<EOF
Pragma: public
EOF
    fi
}

server_response2() {
    local SESSION_ID=$1
    local HOST=$2
    cat <<EOF
Content-type: text/html
Set-Cookie: FTMSession=$SESSION_ID; domain=$HOST; path=/; secure; httponly
Status: "200 OK"
Connection: close

EOF
}

server_response() {
    local SEND_NPH_STATUS
    local STATUS_VALUE
    local STATUS
    local ETAG
    local EXPIRES
    local RFC1123_DATE=$(date  -u +"%a, %d %h %Y %H:%M:%S GMT")

    SEND_NPH_STATUS=0
    if test $# -gt 0; then
        SEND_NPH_STATUS="$1"
        shift
    fi

    STATUS_VALUE="200 OK"
    if test $# -gt 0; then
        STATUS_VALUE="$1"
        shift
    fi

    if test ${SEND_NPH_STATUS} -ne 0; then
        STATUS="${SERVER_PROTOCOL} ${STATUS_VALUE}"
    else
        STATUS="Status: ${STATUS_VALUE}"
    fi

    ETAG=${RFC1123_DATE}
    EXPIRES=${RFC1123_DATE}

    cat <<EOF
$STATUS
Etag: $ETAG
Expires: $EXPIRES
EOF

    cache_control

    cat <<EOF
Connection: close
Content-Type: text/html; charset=iso-8859-1

EOF
}

header() {
    server_response ${1+"$@"}

    cat <<EOF
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="cache-control" content="public,no-cache">
    <meta http-equiv="pragma" content="no-cache">
    </head>
    <body bgcolor="#E7E7E7">

    <bgcolor="#00FF00" text="#000000" link="#A00000" vlink="#600000" alink="#A00000">

    <h3><FONT face="Arial, Helvetica, sans-serif">
EOF
}

footer() {
    cat <<EOF
</body>
</html>
EOF
}

getProduct() {
    local O_PATH="$PATH"
    export PATH="$PATH:/opt/trs/bin"
    echo $(test_envInfo -a|sed -e '/Product/ !d;s/.*PROD_//;s/)$//')
    PATH="$O_PATH"
}

buildLink() # page desc linkdesc product ( with protection )
{
    local LOCK_IMG=""
    local PREFIX=""
    local PRODUCT=$(getProduct)

    if echo "$1" | grep -q ".cgi" 2>/dev/null; then
        PREFIX="/cgi-bin"
    fi

    if [ -n "$4" ]; then
        if [ "$4" = "ALL" -o "$4" = "$PRODUCT" ]; then
            PREFIX=/protected
            LOCK_IMG='<IMG SRC="/images/Authentication_16.gif" HEIGHT="15" BORDER="0" ALIGN="TOP">'
        fi
    fi
    
    local TARGET="$PREFIX/$1"

    cat <<EOF
  <A HREF="$TARGET" target="view" onMouseOver = "window.status='$2'; return true" onMouseOut = "window.status=''; return true"> $3 </A> $LOCK_IMG <BR>
EOF
}

buildAction() # compressedFile buttonDescription product ( WITHOUT protection )
{
    local LOCK_IMG=""
    local PREFIX=""
    local PRODUCT=$(getProduct)

    if echo "$1" | grep -q ".tgz" 2>/dev/null; then
        PREFIX="/cgi-bin"
    fi

    if [ -z "$3" -o "$3" = "NONE" -o "$3" != "$PRODUCT" ]; then
        PREFIX=/protected
        LOCK_IMG='<IMG SRC="/images/Authentication_16.gif" HEIGHT="15" BORDER="0" ALIGN="TOP">'
    fi

    local TARGET="$PREFIX/$1"

    cat <<EOF
  <form ACTION="$TARGET" method="get"> <b> <input type="SUBMIT" VALUE=" $2 "> </b> </form> $LOCK_IMG
EOF
}

wasCgiBin() # checks, if was called via cgi-bin; SCRIPT_NAME
{
    PRODUCT=$(getProduct)
    was_cgi_bin=`echo "$1" | grep -c '/cgi-bin/'`
    was_auth_cgi=`echo "$1" | grep -c '/cgi-bin/auth.cgi'`
    if [ "WIMAX" != "$PRODUCT" ] && [ $was_cgi_bin -eq 1 ] && [ $was_auth_cgi -eq 0 ]; then
        echo 1
    else
        echo 0
    fi
}

sendUnauthorizedMessage() # buttonName
{
    server_response "$1" "401 Unauthorized"

    cat <<EOF
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<HTML>
    <HEAD>
        <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
        <META HTTP-EQUIV="Expires" CONTENT="-1">
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    </HEAD>
    <BODY bgcolor="#00FF00" text="#000000" link="#A00000" vlink="#600000" alink="#A00000">
        <h3>
            <font face="Arial, Helvetica, sans-serif">
                Unauthorized access denied!
            </font>
        </h3>
        <br>Please, use the button resp. link <b>$2</b>
    </BODY>
</HTML>
EOF
}

# $1 - server response, $2 - reference text, $3 - display message, $4 - font size
sendMessage() # buttonName
{
    server_response "$1" "401 Unauthorized"

    cat <<EOF
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<HTML>
    <HEAD>
        <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
        <META HTTP-EQUIV="Expires" CONTENT="-1">
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
    </HEAD>
    <BODY bgcolor="lightgrey" text="#000000" link="#A00000" vlink="#600000" alink="#A00000">
        <h3>
            <font size=$4 face="Arial, Helvetica, sans-serif">
               $3
            </font>
            <br>
            <br>
            <font size=2 face="Arial, Helvetica, sans-serif">
              <u>$2</u>
        </h3>
    </BODY>
</HTML>
EOF
}

getUnit() {
    local O_PATH="$PATH"
    export PATH="$PATH:/opt/trs/bin"
    echo $(test_envInfo -a|sed -e '/Unit/ !d;s/.*UNIT_//;s/)$//')
    PATH="$O_PATH"
}

wasFsmr3()
{
  UNIT=$(getUnit)
  if [ "FCTA" != "$UNIT" ] && [ "FQBA" != "$UNIT" ] && [ "FQGA" != "$UNIT" ] &&  [ "FCTF" != "$UNIT" ]; then
    echo 0
  else
    echo 1
  fi
}

isFsmr4()
{
  local O_PATH="$PATH"
  export PATH="$PATH:/opt/trs/bin"
  RESULT=$(test_envInfo envInfoUnitType | grep "UNIT")
  PATH="$O_PATH"
  if [ "$RESULT" == "UNIT_FSCA" ] || [ "$RESULT" == "UNIT_FCTJ" ]; then
    echo 1
  else
    echo 0
  fi
}

isFzm()
{
  local O_PATH="$PATH"
  export PATH="$PATH:/opt/trs/bin"
  RESULT=$(test_envInfo envInfoUnitTypeIsFzm | grep "UNIT_FZM")
  PATH="$O_PATH"
  if [ "$RESULT" == "UNIT_FZM" ] ; then
    echo 1
  else
    echo 0
  fi
}

#Returns FCT system IP
get_fct_sys_ip()
{
    local result=192.168.255.1
    if [ -e /tmp/lmp_addr.conf ]; then
        result=`grep FCT_SYS_IP /tmp/lmp_addr.conf |awk -F"=" '{print $2}'`
    fi
    echo "$result"
}

#Returns mode if RP441 is enabled or not (for FCT only)
isRP441()
{
    local O_PATH="$PATH"
    export PATH="$PATH:/opt/trs/bin"

    if [ `isFsmr4` -eq 1 ]; then
      UNIT=$(test_envInfo envInfoUnitType | grep "UNIT")
    else
      UNIT=$(getUnit)
    fi
    MODE=$(test_envInfo envInfoIfRP441 | grep "RP441")
    PATH="$O_PATH"
    if ([ "UNIT_FCTJ" = "$UNIT" ] || [ "FCTA" = "$UNIT" ] || [ "FCTF" = "$UNIT" ]) && [ "RP441_ENABLED" = "$MODE" ]; then
        echo 1
    else
        echo 0
    fi
}

