#!/bin/bash

echo "Starting icron eeprom update"
source $(dirname $0)/flash_icron_utils

eepromConfig=$icronFirmwareDir/icron_eeprom_default.bin
eepromWriter=$(dirname $0)/ge_eeprom.py
macWriter=$(dirname $0)/add_mac.sh
exitCode=0

#Add mac crc to eeprom config file
/bin/bash $macWriter $eepromConfig $icronPath

requestId=$(curl -s -X POST 127.0.0.1:$port/api/device/$avx \
   -H 'Content-Type: application/json' \
   -d '{"op" : "icron:program:eeprom","port_index":0}' | jq '.request_id')
echo "Asking Icron to enter eeprom updater mode, answer in $requestId requestId"

waitForSuccess $requestId
echo "Icron entered eeprom updater mode"

echo "Writing eeprom"
errorCode=$(python3 $eepromWriter $uart1Avx write < $eepromConfig 2>&1)

if [[ $errorCode == *"Traceback (most recent call last):"* ]]; then
    exitCode=1
fi

requestId=$(curl -s -X POST 127.0.0.1:$port/api/device/$avx \
   -H 'Content-Type: application/json' \
   -d '{"op" : "icron:program:none"}' | jq '.request_id')
echo "Asking Icron to quit eeprom updater mode, answer in $requestId requestId"

waitForSuccess $requestId
echo "Icron quit eeprom updater mode"
exit $exitCode
