#!/bin/bash

function manual_reset {
echo "The switch needs to be power cycled after fpga update."
PROMPT="Do you want to update the FPGA [N] "
while true
do
 read -ep "$PROMPT" REPLY
 case `echo $REPLY | /usr/bin/tr [:lower:] [:upper:]` in
  N|NO|"") exit 0 ;;
  Y|YES) break ;;
 esac
done
echo "Please avoid powering off the switch during programming."
echo "After JTAG programming is complete, please power cycle the switch!"
echo ""
/fabos/share/jtagpgm -f /fabos/share/Kestrel3_0x0C_20160407_32bit_no_remote.xsvf
}

function auto_reset {
echo "This option provides the capability of upgrading FPGA remotely."
echo "The switch will power itself off automatically after fpga update."
echo "Please avoid powering off the switch during programming and wait "
echo "for 2 minutes before powering the switch back on from SVP."
echo "The telnet/console access will be terminated during the operation "
echo "so please re-connect to the switch after powering the switch on."
PROMPT="Do you want to update the FPGA [N] "
while true
do
 read -ep "$PROMPT" REPLY
 case `echo $REPLY | /usr/bin/tr [:lower:] [:upper:]` in
  N|NO|"") exit 0 ;;
  Y|YES) break ;;
 esac
done
echo ""
/fabos/share/jtagpgm -f /fabos/share/K3_FPGA_0x07_1014.xsvf
}

function printusage {
echo "fgpa_update [--remote]"
echo "--remote: after upgrade, the FPGA will apply after remotely powering the switch back on from SVP."
}

if [[ $# -gt 1 ]]; then
printusage;
exit 1;
fi

if [[ $# -lt 1 ]]; then 
manual_reset;
elif [ "$1" = "--remote" ]; then
auto_reset;
else
printusage;
exit 1;
fi

exit 0;
