#! /usr/bin/env bash
set -e


if [[ $1 != "--force" ]]; then
    echo "Flashing the bootloader is disabled."
    echo "Your Spark will automatically install bootloader updates when it is connected to the internet."
    exit
fi

SERIAL_FLASH="npx particle --no-update-check serial flash"

if [[ -n $(lsusb -d 2b04:c006) ]]; then
    echo "Triggering listening mode"
    ./trigger-listening
    echo "Flashing Photon bootloader..."
    # We've already triggered listening mode. Automatically press enter at the prompt
    echo -ne '\n' | ${SERIAL_FLASH} bootloader-photon.bin
    exit
fi

if [[ -n $(lsusb -d 2b04:c008) ]]; then
    echo "Triggering listening mode"
    ./trigger-listening
    echo "Flashing P1 bootloader..."
    # We've already triggered listening mode. Automatically press enter at the prompt
    echo -ne '\n' | ${SERIAL_FLASH} bootloader-p1.bin
    exit
fi

echo "No compatible USB devices found"
