#!/bin/bash
version=1.8.19

#determine filename of download
if [ $arch == 32 ];then
  filename="arduino-${version}-linuxarm.tar.xz"
elif [ $arch == 64 ];then
  filename="arduino-${version}-linuxaarch64.tar.xz"
fi

#ask if user wants dark theme
wget -qO /tmp/dark-arduino.png 'https://github.com/Botspot/DarkArduinoTheme/raw/master/dark-arduino.png'
wget -qO /tmp/light-arduino.png 'https://github.com/Botspot/DarkArduinoTheme/raw/master/light-arduino.png'

yad --center --separator='\n' \
  --title='Arduino color theme' \
  --form --columns=2 --no-buttons \
  --window-icon="$(dirname "$0")/icon-64.png" \
  --text="Choose a color theme for Arduino:" \
  --field='!/tmp/dark-arduino.png!Dark mode:BTN' 'bash -c "echo dark; kill $YAD_PID"' \
  --field='!/tmp/light-arduino.png!Light mode (default):BTN' 'bash -c "echo light; kill $YAD_PID"' | grep -qx dark &
question_pid=$!

cd ~/.local/share
wget -O $PWD/$filename "https://downloads.arduino.cc/${filename}" || error "Failed to download arduino package!"

tar -xf $PWD/$filename || error "failed to extract with tar!"
sudo $PWD/arduino-${version}/install.sh || error "failed run Arduino install script!"
rm -f $HOME/Desktop/arduino-arduinoide.desktop $PWD/$filename

if package_installed brltty ; then
  status "Removing brltty package because it makes it impossible to connect to arduino boards..."
  apt_lock_wait
  sudo apt purge -y brltty | less_apt
fi

#configure dark mode if user chose it
wait $question_pid
if [ $? == 0 ];then
  status "Installing dark mode..."
  git_clone https://github.com/Botspot/DarkArduinoTheme || exit 1
  
  cp -r DarkArduinoTheme/theme ~/.local/share/arduino-${version}/lib
  cp -r DarkArduinoTheme/images/. ~/.local/share/arduino-${version}/lib
  rm -rf DarkArduinoTheme
  status_green "Done"
fi
rm -f /tmp/dark-arduino.png /tmp/dark-arduino.png
