#!/usr/bin/env bash
##############################################################################
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (C) 2025 Engineering Design Team, Inc.
# https://edt.com
#
# Invoke NVIDIA's flashing script(s) to flash Jetson Linux to an EDT carrier
# board.
#
##############################################################################

set -eo pipefail

SCRIPT_FNAME=$(basename $0)
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

function fn_confirm_or_die {
    select yn in "Yes" "No"; do
        case $yn in
            Yes ) echo "User confirmed. Continuing..."; break;;
            No ) exit 255;;
            *) echo "That is not a valid choice"; continue;;
        esac
    done
}

# Include board-specific variables and functions.
source "$SCRIPT_DIR"/_bsp_script_vars.sh

pushd ./Linux_for_Tegra

echo ""
echo "Do you want to erase and re-flash a NVIDIA Jetson device in Recovery mode attached to this computer?"
fn_confirm_or_die

echo "RUNNING:"
echo "    sudo  ${EDT_BOARD_FLASH_SH} --flash-only --erase-all " "${EDT_BOARD_FLASH_SH_ARGS[@]}"
sudo "$EDT_BOARD_FLASH_SH" --flash-only --erase-all "${EDT_BOARD_FLASH_SH_ARGS[@]}"
