#!/bin/bash

function check-armhf() {
	ARMHF="$(dpkg --print-foreign-architectures | grep "armhf")"
}

#add armhf architecture (multiarch)
check-armhf
if [[ "$ARMHF" == *"armhf"* ]]; then
  echo "armhf arcitecture already added..."
else
  sudo dpkg --add-architecture armhf
  check-armhf
  if [[ "$ARMHF" != *"armhf"* ]]; then
    error "armhf architecture should be added by now, but it isn't!"
  fi
fi

if dpkg -l box86 &>/dev/null ;then
  sudo apt purge -y --allow-change-held-packages box86*
fi

#install box86 dependencies
unset rpi_arm_userspace
# only install the libraspberrypi0 arm32 package if the user already has the libraspberrypi0 arm64 package installed
if package_installed libraspberrypi0 ; then
  rpi_arm_userspace="libraspberrypi0:armhf"
fi

unset mesa_va_drivers
# only install the mesa-va-drivers arm32 package if the user already has the mesa-va-drivers arm64 package installed
if package_installed mesa-va-drivers ; then
  mesa_va_drivers="mesa-va-drivers:armhf"
fi

if [[ ! -z "$__os_original_id" ]]; then
  error "User error: Box86 cannot be installed on $__os_original_desc. Please use any official Ubuntu Flavor, Debian, or PiOS/Raspbian Bullseye."
elif [[ "$__os_codename" == "buster" ]]; then
  error "User error: Box86 cannot be installed on $__os_desc. Please use any official Ubuntu Flavor, Debian, or PiOS/Raspbian Bullseye."
fi

# libsdl2-mixer-2.0-0:armhf NOT included since it depends on libopusfile0 on newer distros (buster+ and focal+) which is NOT multiarch compatible

install_packages libc6:armhf libstdc++6:armhf \
  libx11-6:armhf \
  libgtk-3-0:armhf \
  libgtk2.0-0:armhf libgdk-pixbuf2.0-0:armhf \
  libpng16-16:armhf libjpeg62:armhf \
  libopenal1:armhf osspd:armhf libvorbisfile3:armhf \
  libcurl4:armhf \
  libudev1:armhf \
  libsdl2-2.0-0:armhf libsdl2-image-2.0-0:armhf libsdl2-net-2.0-0:armhf libsdl2-ttf-2.0-0:armhf \
  libsdl1.2debian:armhf libsdl-mixer1.2:armhf libsdl-image1.2:armhf libsdl-net1.2:armhf libsdl-sound1.2:armhf libsdl-ttf2.0-0:armhf libsmpeg0:armhf \
  libcal3d12v5:armhf \
  libssl1.1:armhf '|' libssl3:armhf libssh-gcrypt-4:armhf \
  libcups2:armhf \
  libgssapi-krb5-2:armhf libkrb5-3:armhf \
  $rpi_arm_userspace $mesa_va_drivers libegl1-mesa:armhf libgl1-mesa-glx:armhf libgles2-mesa:armhf

sudo wget https://pi-apps-coders.github.io/box86-debs/box86.list -O /etc/apt/sources.list.d/box86.list
if [ $? != 0 ];then
  sudo rm -f /etc/apt/sources.list.d/box86.list
  error "Failed to add box86.list file!"
fi

sudo rm -f /usr/share/keyrings/box86-debs-archive-keyring.gpg /etc/apt/trusted.gpg.d/box86-debs-archive-keyring.gpg
sudo mkdir -p /etc/apt/trusted.gpg.d
wget -qO- https://pi-apps-coders.github.io/box86-debs/KEY.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/box86-debs-archive-keyring.gpg > /dev/null

if [ $? != 0 ];then
  sudo rm -f /etc/apt/sources.list.d/box86.list
  error "Failed to add KEY.gpg to APT keyring!"
fi

# obtain SOC_ID
get_model
if [[ "$SOC_ID" == "tegra-x1" ]] || [[ "$SOC_ID" == "tegra-x2" ]]; then
  install_packages box86-tegrax1:armhf || exit 1
elif [[ "$SOC_ID" == "rk3399" ]]; then
  install_packages box86-rk3399:armhf || exit 1
elif [[ "$SOC_ID" == "bcm2711" ]]; then
  install_packages box86-rpi4arm64:armhf || exit 1
elif [[ "$SOC_ID" == "bcm2837" ]]; then
  install_packages box86-rpi3arm64:armhf || exit 1
elif cat /proc/cpuinfo | grep -q aes; then
  warning "There is no box86 pre-build for your device $SOC_ID $model"
  warning "Installing the generic arm box86 build as a fallback (crypto extensions enabled)"
  install_packages box86-generic-arm:armhf || exit 1
else
  warning "There is no box86 pre-build for your device $SOC_ID $model"
  warning "Installing the RPI4 tuned box86 build as a fallback (no crypto extensions enabled)"
  install_packages box86-rpi4arm64:armhf || exit 1
fi
