#!/usr/bin/env bash

#deb https://ppa.launchpadcontent.net/mkusb/ppa/ubuntu questing main
#deb-src https://ppa.launchpadcontent.net/mkusb/ppa/ubuntu questing main
#deb https://ppa.launchpadcontent.net/mkusb/unstable/ubuntu questing main
#deb-src https://ppa.launchpadcontent.net/mkusb/unstable/ubuntu questing main
#
#Signing key:
#    4096R/29D76ADA2D15A87BF4C68B823729827454B8C8AC (What is this?)

ACTIVE="/etc/apt/sources.list.d"
AVAILABLE="/etc/apt/sources.list-available"
TRANSPORT="https"
URL="ppa.launchpadcontent.net"
RELEASE="questing"
APP="mkusb"
PPA_KEY="29D76ADA2D15A87BF4C68B823729827454B8C8AC"
VERSION="ppa" # unstable

# wget -O - https://${URL}/${APP}/openwebrx-plus.gpg | gpg --dearmor -o /etc/apt/trusted.gpg.d/openwebrx-plus.gpg
# sudo gpg --export --armor ${PPA_KEY} | sudo tee /usr/share/keyrings/${APP}-archive-keyring.gpg
export GNUPGHOME="$(mktemp -d)"
sudo gpg --keyserver keyserver.ubuntu.com --recv-keys ${PPA_KEY}
sudo gpg --export --armor "${PPA_KEY}" | sudo tee /usr/share/keyrings/${APP}-archive-keyring.asc >/dev/null
# echo "deb http:/${URL}/${APP}/${VERSION}/ubuntu ${RELEASE} main" > sudo tee -a /etc/apt/sources.list-available/mkusb-ppa.list
# echo "deb-src http://${URL}/${APP}/${VERSION}/ubuntu ${RELEASE} main" >> sudo tee -a /etc/apt/sources.list-available/mkusb-ppa.list

cat >${AVAILABLE}/${APP}_${RELEASE}.sources <<EOF
Enabled: yes
Types: deb deb-src
URIs: ${TRANSPORT}://${URL}/${APP}/ppa/ubuntu/
Suites: ${RELEASE}
Components: main
Architectures: amd64
Signed-by: /usr/share/keyrings/${APP}-archive-keyring.asc
EOF

sudo ln -sf ${AVAILABLE}/${APP}_${RELEASE}.sources ${ACTIVE}/${APP}_${RELEASE}.sources

# Bypass apt-proxy for mkusb packages
if [ -d /etc/apt/apt.conf.d/02proxy ]; then
  if [ "$(grep -q ${URL})" ]; then
    echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >>/etc/apt/apt.conf.d/02proxy
  fi
else
  echo "Acquire::http::Proxy { \"${URL}\" DIRECT; };" >>/etc/apt/apt.conf.d/02proxy
fi

sudo apt update

sudo apt install -y --no-install-recommends \
  mkusb \
  mkusb-nox \
  usb-pack-efi \
  dus \
  guidus
