#!/bin/bash

echo "Finding serial ports..."
## get serial port via board list, choose lines including "USB", and pick up 1st word
serials=`arduino-cli board list | grep USB | awk '{print $1}'`

# echo ${serials}
if [ -z "${serials}" ]; then
  echo "No device connected."
  echo "デバイスを接続しているのに認識しないときは、ドライバを入れてください。"
  echo "https://ftdichip.com/drivers/vcp-drivers/ → Click here to download の hereをおす。ZIP解凍して実行。"
  exit
fi

## if multiple ports found, try uploading to all ports.
seriallist=($serials)

## print the list
# echo ${seriallist[@]}

for ser in ${seriallist[@]} ; do
    echo ${ser}
#   arduino-cli upload --fqbn esp32:esp32:m5stick-c -p ${ser} -v FactoryTest
    arduino-cli upload --fqbn esp32:esp32:m5stack_stickc_plus2 -p ${ser} -v FactoryTest
done


if [[ "${#seriallist[@]}" -eq 1 ]]; then
  arduino-cli monitor -p ${seriallist[0]} --config 115200
fi

