#!/bin/bash
ICRON_PROGRAMMER_BOARD_CONFIG="/etc/lwr/static-config/icron-programmer/board_config.env"

if [ -f "$ICRON_PROGRAMMER_BOARD_CONFIG" ]; then
    source "$ICRON_PROGRAMMER_BOARD_CONFIG"
else
    echo "Static board config does not exist, programming failed."
    exit 1
fi

avxPath=/uid/mac_addresses/Avx_1
icronPath=/uid/mac_addresses/Icron_1
avx=$(cat $avxPath | sed 's/://g' | tr '[:upper:]' '[:lower:]') #this is a simpler method than discovering all avx devices and making sure we find the local one
port=8080

icronFirmwareVersionXml=/mnt/storage/permanent/fwversion/icron_1.xml
icronEepromVersionXml=/mnt/storage/permanent/fwversion/icron_eeprom_1.xml
icronFirmwareDir=/usr/lib/firmware/lwr

waitForSuccess() {
    local requestId=$1
    while true; do
        local statusFull=$(curl -s 127.0.0.1:$port/api/request/$requestId \
            -H 'Content-Type: application/json')
        local status=$(jq '.status' <<< ${statusFull})
        echo "Status of request: $status"

        if [ "$status" == "\"SUCCESS\"" ]; then
            return 0
        elif [ "$status" == "\"ERROR\"" ]; then
            return 1
        fi
        sleep 1
    done
}

waitForAvx() {
    local avxToDiscover=$1
    while true; do
        local deviceId=$(curl -s 127.0.0.1:$port/api/device/$avxToDiscover \
            -H 'Content-Type: application/json' | jq '.result.devices[] | .device_id' 2>&1)
        if [ "$deviceId" == "\"$avxToDiscover\"" ]; then
            break
        elif [[ "$deviceId" == *"jq: error"* ]]; then
            echo "Avx not available on the network"
        fi
        sleep 5
    done
}

enterNonProgrammingMode() {
    local avxToStopProgramming=$1
    local exitcode=1
    local requestIdFull
    local requestId

    while [[ exitcode -ne 0 ]]; do
        #this command pulls the BOOT_SEL0 high and also resets the icron on the I_RESET pin
        requestIdFull=$(curl -s -X POST 127.0.0.1:$port/api/device/$avxToStopProgramming \
            -H 'Content-Type: application/json' \
            -d '{"op" : "icron:program:none"}')
        requestId=$(jq '.request_id' <<< ${requestIdFull})

        echo "Asking Icron to enter non-programming mode, answer in $requestId requestId"
        waitForSuccess $requestId
        exitcode=$?
        if [[ exitcode -eq 1 ]]; then #error, try process again
            sleep 1
        fi
    done
}

rebootAvx() {
    local avxToReboot=$1
    local status

    while true; do
        status=$(curl -s -X POST 127.0.0.1:$port/api/device/$avxToReboot \
            -H 'Content-Type: application/json' -d '{"op" : "reboot"}' | jq '.status')

        echo "Status of request: $status"
        if [ "$status" == "\"SUCCESS\"" ]; then
            return
        fi

        sleep 1
    done
}

claimAvx() {
    local avxToClaim=$1
    local exitcode=1
    local requestId

    while [[ exitcode -ne 0 ]]; do
        requestId=$(curl -s -X POST 127.0.0.1:$port/api/device/$avxToClaim \
            -H 'Content-Type: application/json' \
            -d '{"op" : "claim"}' | jq '.request_id')

        echo "Claim AVX device, answer in $requestId requestId"
        waitForSuccess $requestId
        exitcode=$?
        if [[ exitcode -eq 1 ]]; then #error, try process again
            sleep 1
        fi
    done
}

releaseAvx() {
    local avxToRelease=$1
    local exitcode=1
    local requestId

    while [[ exitcode -ne 0 ]]; do
        requestId=$(curl -s -X POST 127.0.0.1:$port/api/device/$avxToRelease \
            -H 'Content-Type: application/json' \
            -d '{"op" : "release"}' | jq '.request_id')

        echo "Release AVX device, answer in $requestId requestId"
        waitForSuccess $requestId
        exitcode=$?
        if [[ exitcode -eq 1 ]]; then #error, try process again
            sleep 1
        fi
    done
}
