#!/bin/bash
set -e

## 2.4

TCLR=$(tput sgr0)
export TCLR
BOLD=$(tput setaf 2 && tput setab 234)
export BOLD
BOLD_RED=$(tput setaf 1 && tput setab 234)
export BOLD_RED
BOLD_YEL=$(tput setaf 3 && tput setab 234)
export BOLD_YEL

_monkey() {
  for monkey in $(seq 0 $((RANDOM % 40 + 0))); do
    sleep "0.0${monkey}"
    #echo -n "$(tput setaf "$(shuf -i 15-16 -n 1)" && tput setab "$(shuf -i 17-230 -n 1)")🐒$(tput sgr0)"
    echo -n "$(tput setaf "$(shuf -i 1-16 -n 1)" && tput setab "$(shuf -i 17-230 -n 1)")🚁$(tput sgr0)"
  done
  echo
}

##  Make sure we are root
if [[ $EUID -ne 0 ]]; then
  echo "FAILED: this script must be run as root"
  exit 1
fi

##  Make sure ths OS is Buster
if ! grep buster /etc/os-release &>/dev/null; then
  echo
  echo "FAILED: unsupported OS"
  echo "Use: https://downloads.raspberrypi.org/raspios_lite_armhf_latest"
  exit 1
fi

##  Make sure it's fresh
BAD_BINS='dump1090 dump1090-fa'
for bin in ${BAD_BINS}; do
  if command -v "${bin}" &>/dev/null; then
    echo
    echo "FAILED: install on a fresh raspios_lite"
    echo "Use: https://downloads.raspberrypi.org/raspios_lite_armhf_latest"
    exit 1
  fi
done

[[ $(uname -m) == "armv6l" ]] && PACKAGE_ARCH='armhf'
[[ $(uname -m) == "armv7l" ]] && PACKAGE_ARCH='armhf'
[[ $(uname -m) == "aarch64" ]] && PACKAGE_ARCH='arm64'
[[ $(uname -m) == "x86_64" ]] && PACKAGE_ARCH='amd64'


RELEASE_DEB="https://repo.adsb.exchange/testing/dists/buster/pool/main/adsb-exchange-testing-release_latest_${PACKAGE_ARCH}.deb"

_monkey
echo
echo "$(tput bold)●$(tput sgr0) $(tput setaf 1)Only install this software repository on the current RaspiOS.$(tput sgr0)"
echo
echo "$(tput bold)●$(tput sgr0) $(tput setaf 1)Do NOT install with other feeder software or repositories.$(tput sgr0)"
echo
echo "${BOLD_RED}                              READ THE DOCS FIRST                               ${TCLR}"
echo "${BOLD_RED} ${TCLR}$(tput bold) https://git.adsb.exchange/unofficial-adsbexchange-raspberry-pi/documentation ${BOLD_RED} ${TCLR}$(tput sgr0)"
echo "${BOLD_RED}                                                                                ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'                                                                              '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}""  $(tput setaf 2)SB21$(tput sgr0) Testing Installer                                                      ""${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'                                                __|__                         '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'                                         --X--X--(_)--@--@--                  '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'                          |                                                   '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'    Do you want          -R-                         _                        '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'  to install the         /e\     your Pi -->  {@}  _|=|_  <-- Adsbexchange    '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'    unofficial          /___\                /(")\  (")                       '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'   adsbexchange      .---"-"---.            /((~))\/\x/\                      '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'     software       /___AdsbX___\           ~~/@\~~\|_|/                      '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'    repository       |   /^\   |             /   \  |||                       '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'        ??           |   |"|   |            /~@~@~\ |||                       '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'_____________________|___|_|___|___________/_______\|||_______________________'"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED}                                                                                ${TCLR}"
echo
while true; do
  read -r -p "${BOLD_YEL}So do you?${TCLR} " yn
  case $yn in
  [Cc]*) yes c0cks ;;
  "I Do") break ;;
  [Nn]*) exit ;;
  *) echo "\"I Do\" or \"No Dice\"" ;;
  esac
done
echo
echo "${BOLD}● apt-get update${TCLR}"
apt-get -qy update
echo
#apt-get --allow-releaseinfo-change update
echo "${BOLD}● apt-get dist-upgrade${TCLR}"
apt-get -qy dist-upgrade
echo
echo "${BOLD}● install repo${TCLR}"
wget -q "${RELEASE_DEB}" -O "/run/adsb-exchange-testing-release_latest_${PACKAGE_ARCH}.deb"
apt-get -qq install "/run/adsb-exchange-testing-release_latest_${PACKAGE_ARCH}.deb"
rm -f "/run/adsb-exchange-testing-release_latest_${PACKAGE_ARCH}.deb"
apt-get -qq update 1>/dev/null
echo "installed: $(basename ${RELEASE_DEB})"
echo "$(tput setab 1)$(tput bold)$(tput smul) READ THIS NOW $(tput sgr0)"
echo "https://git.adsb.exchange/unofficial-adsbexchange-raspberry-pi/documentation"
echo
exit 0
echo "$(tput smso)Install Choices$(tput sgr0)"
echo "$(tput bold)$(tput smul)  headless  $(tput sgr0)"
echo "sudo apt install -y skybup-headless && sudo reboot"
echo
echo "$(tput bold)$(tput smul)  minimal  $(tput sgr0)"
echo "sudo apt install -y skybup-minimal && sudo reboot"
echo
echo "$(tput bold)$(tput smul)  full  $(tput sgr0)"
echo "sudo apt install -y skybup && sudo reboot"
echo
