#!/bin/bash
set -e

## 2022.01

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 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/Bullseye
if ! grep -E 'buster|bullseye' /etc/os-release &>/dev/null; then
  echo
  echo "FAILED: unsupported OS"
  echo "This software only runs on 'Raspberry Pi OS' Buster (legacy) or Bullseye (current)"
  echo "Download from one of the following links:"
  echo "  https://downloads.raspberrypi.org/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2021-12-02/2021-12-02-raspios-buster-armhf-lite.zip"
  echo "  https://downloads.raspberrypi.org/raspios_lite_arm64/images/raspios_lite_arm64-2021-11-08/2021-10-30-raspios-bullseye-arm64-lite.zip"
  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"
    exit 1
  fi
done

source /etc/os-release
[[ $(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=$(curl -s "https://repo.adsb.exchange/testing/dists/${VERSION_CODENAME}/pool/main/" | grep "${PACKAGE_ARCH}" | grep release | cut -d'"' -f2 | tail -n1)

_monkey
sleep 1
echo
echo "$(tput bold)●$(tput sgr0) $(tput setaf 1)Only install this software repository on the current RaspiOS.$(tput sgr0)"
echo
sleep 2
echo "$(tput bold)●$(tput sgr0) $(tput setaf 1)Do NOT install with other feeder software or repositories.$(tput sgr0)"
echo
sleep 2
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}"
sleep 3
echo "${BOLD_RED} ${TCLR}"'                                                                              '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}""  $(tput setaf 2)SB22$(tput sgr0) Repository 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}"'                         -R-                         _                        '"${BOLD_RED} ${TCLR}"
echo "${BOLD_RED} ${TCLR}"'                         /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
echo "You have 8 seconds to press '<Ctrl> + c' before the installer begins..."
sleep 8
echo
echo "Starting repo install for ${VERSION_CODENAME}-${PACKAGE_ARCH}..."
echo "${BOLD}● apt-get update${TCLR}" && apt-get -qq update
echo
#echo "${BOLD}● apt-get dist-upgrade${TCLR}" && apt-get -qq dist-upgrade
#echo
echo "${BOLD}● install repo${TCLR}"
wget -q "https://repo.adsb.exchange/testing/dists/${VERSION_CODENAME}/pool/main/${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: ${RELEASE_DEB}"
echo
echo "$(tput setab 1)$(tput bold)$(tput smul) READ THIS NOW $(tput sgr0)"
echo "https://git.adsb.exchange/unofficial-adsbexchange-raspberry-pi/documentation"
echo
apt-get install -qq libgdk-pixbuf2.0-0 wget
wget -q https://repo.adsb.exchange/2022.png -O /run/2022.png
wget -q "https://repo.adsb.exchange/tiv-${PACKAGE_ARCH}" -O /usr/local/bin/tiv
chmod +x /usr/local/bin/tiv
/usr/local/bin/tiv -s 40 -t /run/2022.png
echo
echo "$(tput bold)$(tput smul)  NOW INSTALL SKYBUP  $(tput sgr0)"
echo "sudo apt install -y skybup && sudo reboot"
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
