#!/bin/sh
# shellcheck source=/dev/null
# shellcheck disable=SC2086

sysfilepath=""

CURRENT_CURRENT_INTERFACE=""
CURRENT_PRINCIPAL_INTERFACE=""
CURRENT_PRINCIPAL_STATE=""
CURRENT_RESCUE_INTERFACE=""
CURRENT_RESCUE_STATE=""


primary_led_update()
{
    :
}

rescue_led_update()
{
    :
}

SystemGetFilePath "$ROOTACT/lrr/failovermgr" "led_control.sh"
[ -f "${sysfilepath}" ] && . "${sysfilepath}"

checkInterfaceStatus()
{

INTERFACE=$1

if ! ip ad show "$INTERFACE" >/dev/null 2>&1 ; then
    Log "Interface $INTERFACE is not READY"
    return 1
fi

if ! ip ad show "$INTERFACE" | grep -q ",UP[,>]" ; then
    Log "Interface $INTERFACE is not UP"
    return 1
fi

if ! ip ad show "$INTERFACE" | grep -q ",LOWER_UP[,>]" ; then
    Log "Interface $INTERFACE is LINKDOWN"
    return 1
fi

# Filter out virtual interfaces IP (ie avahi)
for ip in $(ip ad show "$INTERFACE" | grep "inet" | xargs | cut -d' ' -f2 | grep -vE '^169.254.'); do
    Log "Interface $INTERFACE is UP"
    return 0
done

Log "Interface $INTERFACE has no IP"
return 1
}

getItfType()
{
    itf=$1
    case $itf in
        eth*)
            echo "0"
            ;;
        wlan*)
            echo "1"
            ;;
        ppp*|wwan*)
            echo "2"
            ;;
        *)
            echo "3"
            ;;
    esac
}


# convItfType <itfname>
#
# get itf type from the itf name
#
# result is unknown|ethernet|wifi|cellular
#
convItfType()
{
    cit_itf="$1"

    if [ "$cit_itf" = "ERROR" ]
    then
        echo "unknown"
    else
        cit_typ=$(getItfType "$cit_itf")
        if [ "$cit_typ" = "1" ]
        then
            echo "wifi"
        elif [ "$cit_typ" = "2" ]
        then
            echo "cellular"
        elif [ "$cit_typ" = "0" ]
        then
            echo "ethernet"
        else
            echo "unknown"
        fi
    fi
}


# updateStateFile <currentit>
#
# Update file state used by the lrr for the wan report
#
# Format:
# ITUSED=<ittype>
#
# <ittype> = unknown|ethernet|wifi|cellular
#
updateStateFile()
{
    [ -z "$STATEFILE" ]&& return
    current="$1"

    [ "$current" = "$CURRENT_CURRENT_INTERFACE" ] && return
    CURRENT_CURRENT_INTERFACE=$current

    Log "updateStateFile currentit='$current'"

    {
        echo "ITUSED=$(convItfType $current)"
    } > $STATEFILE
}


# updateStatePrincipal <principalitf> <principalstate>
#
# Update rescue file state to know if the route to lrc is ok
#
# Format:
# ITTYPEPRINCIPAL=<ittype>
# ITSTATEPRINCIPAL=<itstate>
#
# <ittype> = unknown|ethernet|wifi|cellular
# <itstate> = ok|fail
#
updateStatePrincipal()
{
    # Rescue state file defined ?
    [ -z "$STATEPRINCIPAL" ]&& return
    usr_principal="$1"
    usr_state="$2"

    [ -z "$usr_principal" ]&& return

    [ "$usr_principal" = "$CURRENT_PRINCIPAL_INTERFACE" ] && [ "$usr_state" = "$CURRENT_PRINCIPAL_STATE" ] && return

    CURRENT_PRINCIPAL_INTERFACE=$usr_principal
    CURRENT_PRINCIPAL_STATE=$usr_state

    Log "updateStatePrincipal principal='$usr_principal' state=$usr_state"

    {
        echo "ITTYPEPRINCIPAL=$(convItfType $usr_principal)"
        echo "ITSTATEPRINCIPAL=$usr_state"
    } > $STATEPRINCIPAL

    primary_led_update "$usr_state"
}

# updateStateRescue <rescueit> <rescuestate>
#
# Update rescue file state to know if the route to lrc is ok
#
# Format:
# ITTYPERESCUE=<ittype>
# ITSTATERESCUE=<itstate>
#
# <ittype> = unknown|ethernet|wifi|cellular
# <itstate> = ok|fail
#
updateStateRescue()
{
    # Rescue state file defined ?
    [ -z "$STATERESCUE" ] && return
    usr_rescue="$1"
    usr_state="$2"

    [ -z "$usr_rescue" ] && return

    [ "$usr_rescue" = "$CURRENT_RESCUE_INTERFACE" ] && [ "$usr_state" = "$CURRENT_RESCUE_STATE" ] && return

    CURRENT_RESCUE_INTERFACE=$usr_rescue
    CURRENT_RESCUE_STATE=$usr_state

    Log "updateStateRescue rescue='$usr_rescue' state=$usr_state"

    {
        echo "ITTYPERESCUE=$(convItfType $usr_rescue)"
        echo "ITSTATERESCUE=$usr_state"
    } > $STATERESCUE

    rescue_led_update "$usr_state"
}
