#!/bin/bash

echo "*** Running TestBuild/upload.sh ..."

## mosi hikisuu(=port) ga areba, sokodake ni upload
if [ $# -eq 1 ] ; then
   port=$1
   echo ${port}
   arduino-cli upload --fqbn esp32:esp32:m5stick-c -p "/dev/${port}" "."
   exit
fi


## hikisuu ga nakereba, zenbu ni upload

## 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."
  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} "."
done

