#!/bin/bash

# 以前コンパイルしたbin.imgファイルをつかって、直接アップロードするスクリプト
# USAGE: ./DirectBinUpload.sh [target.bin.img]
# 使用例: ./DirectBinUpload.sh sprite01.bin.img

# SampleSrc フォルダにある _AllBuildTest.sh を実行すると、すべてのサンプルプログラムをコンパイルしてbin.imgを生成します。（時間がかかります）

if [ $# -eq 1 ] ; then

   target=$1
   echo upload ${target}

   if [ ! -e "${target}" ]; then
      echo "No such bin.img file. You should prepare bin.img by compile in advance."
      echo "(to compile, run these commands.) =>  cd ../.. ; ./Upload.sh "
      exit
   fi

   cp ${target} TestBuild.ino.bin

   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} --input-dir .
   done

else
   echo "USAGE: ./DirectBinUpload.sh [target.bin.img]"
fi


