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

#ARDUINO_VERSION=1.8.13
ARDUINO_VERSION=$(/home/default/.local/bin/lastversion "arduino-ide")
#ARDUINO_VERSION=nightly
#ARDUINO_HOME=~/
ARDUINO_HOME=/opt
ARDUINO_DIR=${ARDUINO_HOME}/arduino-${ARDUINO_VERSION}
ARDUINO_TOOLS_DIR=${ARDUINO_DIR}
ADD_URLS=

# mkdir -p "${ARDUINO_TOOLS_DIR}"
sudo apt install -y gcc-arm-none-eabi openocd libarchive-tools git python3-serial

cd /var/tmp || exit

# Install arduino IDE
wget -c https://github.com/arduino/arduino-ide/releases/download/${ARDUINO_VERSION}/arduino-ide_${ARDUINO_VERSION}_Linux_64bit.zip && \
	unzip /var/tmp/arduino-ide_${ARDUINO_VERSION}_Linux_64bit.zip && \
	mv arduino-ide_${ARDUINO_VERSION}_Linux_64bit ${ARDUINO_HOME}/arduino-${ARDUINO_VERSION} && \
        sudo chown -R default:default ${ARDUINO_HOME}/arduino-${ARDUINO_VERSION} && \
	#rm /var/tmp/arduino-ide_${ARDUINO_VERSION}_Linux_64bit.zip

sudo usermod -a -G dialout "${USER}" && \

#https://raw.githubusercontent.com/espressif/arduino-esp32/master/package/package_esp32_index.template.json
#http://arduino.esp8266.com/stable/package_esp8266com_index.json
#http://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
#https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json
#https://micooke.github.io/package_nRF5_smartwatches_index.json

# http://github.com/arduino/Arduino/wiki/Unofficial-list-of-3rd-party-boards-support-urls
# Install ESP32 core
# See: http://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/boards_manager.md
ADD_URLS="${ADD_URLS},https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"
#ADD_URLS="${ADD_URLS}, https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json"
${ARDUINO_DIR}/arduino-ide \
	--pref boardsmanager.additional.urls=${ADD_URLS} \
	--save-prefs
# arduino [--install-boards package name:platform architecture[:version]]
${ARDUINO_DIR}/arduino-ide --install-boards esp32:esp32

## ESP32-core from git 369MB Choose this or the above otherwise you will get two menus
#mkdir -p ${ARDUINO_DIR}/hardware/espressif && \
#	cd ${ARDUINO_DIR}/hardware/espressif && \
#	git clone http://github.com/espressif/arduino-esp32.git esp32 && \
#	cd esp32 && \
#	git submodule update --init --recursive && \
#	cd tools/ && \
#	python3 get.py

# Install ESP8266
cd /var/tmp || exit
ADD_URLS="${ADD_URLS},http://arduino.esp8266.com/stable/package_esp8266com_index.json"
echo ${ADD_URLS}
${ARDUINO_DIR}/arduino-ide \
	--pref boardsmanager.additional.urls=${ADD_URLS} \
	--save-prefs
# arduino [--install-boards package name:platform architecture[:version]]
${ARDUINO_DIR}/arduino-ide --install-boards esp8266:esp8266
 


# Install stm32 bluepill
# http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/arduino-compatibility.html
#	--pref boardsmanager.additional.urls="http://dan.drown.org/stm32duino/package_STM32duino_index.json" \
#${ARDUINO_DIR}/arduino --install-boards stm32duino:STM32F1
ADD_URLS="${ADD_URLS},https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json"
echo ${ADD_URLS}
${ARDUINO_DIR}/arduino-ide \
	--pref boardsmanager.additional.urls=${ADD_URLS} \
	--save-prefs

## arduino [--install-boards package name:platform architecture[:version]]
${ARDUINO_DIR}/arduino-ide --install-boards STM32:stm32


# Install NRF5x sandeepmistry http://github.com/sandeepmistry/arduino-nRF5
#name	"BBC micro:bit"
#name	"Bluz DK"
#name	"Generic nRF51822"
#name	"Generic nRF52823"
#name	"OSHChip"
#name	"ng-beacon"
#name	"nRF51 Dongle"
#name	"nRF51822 Development Kit"
#name	"nRF52 DK"
#name	"RedBear BLE Nano 2"
#name	"RedBear Blend 2"
#name	"RedBearLab BLE Nano"
#name	"RedBearLab nRF51822"
#name	"Waveshare BLE400"

ADD_URLS="${ADD_URLS},d "
echo ${ADD_URLS}
${ARDUINO_DIR}/arduino-ide \
	--pref boardsmanager.additional.urls=${ADD_URLS} \
	--save-prefs

# arduino [--install-boards package name:platform architecture[:version]]
${ARDUINO_DIR}/arduino-ide --install-boards sandeepmistry:nRF5

NRF_HOME=${ARDUINO_TOOLS_DIR}/nRF5FlashSoftDevice/tool/ 
mkdir -p "${NRF_HOME}" && wget -c --directory-prefix "${NRF_HOME}" http://github.com/sandeepmistry/arduino-nRF5/releases/download/tools/nRF5FlashSoftDevice.jar


# nRF5 watches http://github.com/micooke/arduino-nRF5-smartwatches
# Smartwatch variants for sandeepmistry's Nordic Semiconductor nRF5 core
#name	"IDO003"
#name	"ID100HR"
#name	"ID107HR"
#name	"ID107HR Plus"
ADD_URLS="${ADD_URLS},https://micooke.github.io/package_nRF5_smartwatches_index.json"
${ARDUINO_DIR}/arduino-ide \
	--pref boardsmanager.additional.urls=${ADD_URLS} \
	--save-prefs

## arduino [--install-boards package name:platform architecture[:version]]
${ARDUINO_DIR}/arduino-ide --install-boards micooke:nRF5
## Install Readbear NRF5x 
#cd /var/tmp || exit
#${ARDUINO_DIR}/arduino \
#	--pref="boardsmanager.additional.urls=https://redbearlab.github.io/arduino/package_redbearlab_index.json"\
#	--save-prefs
#
#wget -c https://raw.githubusercontent.com/RedBearLab/nRF51822-Arduino/S130/98-daplink.rules
#cp 98-daplink.rules /etc/udev/rules.d/
#
#sudo udevadm control --reload-rules
#
## Fix the upload problem on Linux 64bit system
#
#sudo apt install -y lsb-core
#
#sudo apt install -y libudev1:i386
#
#sudo ln -sf /lib/x86_64-linux-gnu/libudev.so.1 /lib/x86_64-linux-gnu/libudev.so.0

# Guru meditation decode - get a more meaningful explanation of the stack traces you get on ESP8266/ESP32.
DECODE_VER="2.0.3"
wget -qO - http://github.com/me-no-dev/EspExceptionDecoder/releases/download/${DECODE_VER}/EspExceptionDecoder-${DECODE_VER}.zip | bsdtar -C "${ARDUINO_TOOLS_DIR}" -xvf-


# Other libraries direct from github
git clone http://github.com/adafruit/Touch-Screen-Library ~/Arduino/libraries/TouchScreen

# https://learn.adafruit.com/ahrs-for-adafruits-9-dof-10-dof-breakout/sensor-fusion-algorithmsahrs
git clone http://github.com/PaulStoffregen/MahonyAHRS ~/Arduino/libraries/MahonyAHRS
git clone http://github.com/PaulStoffregen/MadgwickAHRS ~/Arduino/libraries/MadgwickAHRS
