#!/bin/sh

MODE_FULL=full
MODE_LITE=lite

MODE_CURRENT=${MODE_FULL}

MODE_LAST_UNBOUND=0
MODE_LAST_KWILTREADY=0

MODE_LAST_UNBOUND_FILE="${MEDIACASTER_DIR}/tmp/unbound.event"
MODE_LAST_KWILTREADY_FILE="${MEDIACASTER_DIR}/tmp/kwiltready.event"

# Compare the mode the daemon should be in to the current mode
# Return 1 if they are different
# Return 0 if they are the same
daemon_mode_compare_state () {
    if [ "$(daemon_mode_target)" != "$MODE_CURRENT" ]; then
        return 1
    fi

    return 0
}

daemon_mode_set () {
    MODE_CURRENT="$1"
}

daemon_mode_get () {
    echo ${MODE_CURRENT}
}

# Respond to the bound_status event from the HipServ CS
daemon_mode_set_binding_count () {
    if [ "$1" == "0" ]; then
        echo "$(daemon_utils_uptime)" > "${MODE_LAST_UNBOUND_FILE}"
    fi

    #Filepath to store the previous bind count
    LAST_BIND_COUNT_FILE=/tmp/bind_count

    #Setting to NOTSET to handle startup scenarios where we don't have a previous count stored.
    PREV_BIND_COUNT="NOTSET"

    #Update our previous bind count variable if there's a stored version
    if [ -f ${LAST_BIND_COUNT_FILE} ]; then
        PREV_BIND_COUNT=$(cat "${LAST_BIND_COUNT_FILE}" 2>/dev/null)
    fi

    #Store the current count to be used later
    echo "$1" > ${LAST_BIND_COUNT_FILE}

    #If bind count is 0 we turn off the internet persistence anyway
    #access-patrol stops afterwards so we won't get more of this event
    #and we won't be constantly repeating the request
    if [ "$1" = "0" ]; then
        internet set connection kwilt 0
        return
    fi

    #Handle changes in bind count and ignore startup cases
    if [ "${PREV_BIND_COUNT}" != "NOTSET" -a "${PREV_BIND_COUNT}" != "$1" ]; then
        echo "Bind count changed to $1. Running internet command accordingly."
        internet set connection kwilt $1
    fi
}

daemon_mode_read_unbound () {
    VALUE=$(cat "${MODE_LAST_UNBOUND_FILE}" 2>/dev/null)
    if [ "" == "$VALUE" ]; then
        echo ${MODE_LAST_UNBOUND}
        return; # Homework done
    fi
    echo ${VALUE}
}

# Respond to the kwilt_ready event from the local web server
daemon_mode_set_kwilt_ready () {
    echo "$(daemon_utils_uptime)" > "${MODE_LAST_KWILTREADY_FILE}"
}


daemon_mode_read_kwilt_ready () {
    VALUE=$(cat "${MODE_LAST_KWILTREADY_FILE}" 2>/dev/null)
    if [ "" == "$VALUE" ]; then
        echo ${MODE_LAST_KWILTREADY}
        return; # Homework done
    fi
    echo ${VALUE}
}


# Calculate which mode the daemon should be in
# Return the target mode name
daemon_mode_target () {

    # If kwiltReady was called in the last N seconds, make sure we are in full mode
    MODE_LAST_KWILTREADY=$(daemon_mode_read_kwilt_ready)
    if [ ${MODE_FULL_KWILTREADY_TIMEOUT} -gt $(daemon_utils_uptime_diff ${MODE_LAST_KWILTREADY}) ]; then
        echo ${MODE_FULL}
        return;
    fi

    if [ "${MODE_CURRENT}" == "${MODE_FULL}" ]; then
        MODE_LAST_UNBOUND=$(daemon_mode_read_unbound)
        # If we have received unbound status response in the last N seconds, switch to lite mode
        if [ ${MODE_FULL_UNBOUND_TIMEOUT} -gt $(daemon_utils_uptime_diff ${MODE_LAST_UNBOUND}) ]; then
            echo ${MODE_LITE}
            return;
        else
            echo ${MODE_FULL}
            return;
        fi
    fi

    echo ${MODE_LITE}
}
