#!/bin/bash

#get the first match for the arduino folder location (changes name depending on the version)
location="$(find $HOME/.local/share -maxdepth 1 -name "arduino-*" -print -quit)"

if [ -d "${location}" ]; then
  if [ -f "${location}/uninstall.sh" ]; then
    sudo "${location}/uninstall.sh" || error "Failed to run Arduino uninstall script!"
    rm -rf "${location}" || error "Failed to remove ${location} folder"
  elif [ -f "${location}/install.sh" ]; then
    #there is an option to uninstall Arduino in its install script, if uninstall script not found, use sudo install.sh -u instead
    sudo "${location}/install.sh" -u || error "Failed to run sudo ${location}/install.sh -u"
    rm -rf "${location}" || error "Failed to remove ${location} folder"
  else
    warning "Arduino's install and uninstall script were not found! Most likely it was never fully installed. Removing ${location} folder..."
    rm -rf "${location}" || error "Failed to remove ${location} folder"
  fi
else
  warning "No Arduino folder was found in $HOME/.local/share. Most likely it was never fully installed"
fi
