#!/bin/bash
set -eu -o pipefail

REQUIREDPACKAGES="liblz4-1:arm64 libidn2-0:arm64 librav1e0:arm64 libsystemd0:arm64 libgpg-error0:arm64 libharfbuzz0b:arm64 libxcb-xfixes0:arm64 libxshmfence1:arm64 libdrm2:arm64 libegl-mesa0:arm64 libglx-mesa0:arm64 libdrm-nouveau2:arm64 libwayland-client0:arm64 libicu74:arm64 libgstreamer-plugins-base1.0-0:arm64 libdatrie1:arm64 gstreamer1.0-libav:arm64 libpango-1.0-0:arm64 libgraphite2-3:arm64 libdw1t64:arm64 libgl1-mesa-dri:arm64 libopus0:arm64 liblerc4:arm64 libzstd1:arm64 libaom3:arm64 libselinux1:arm64 libblkid1:arm64 liblzo2-2:arm64 libxdmcp6:arm64 libxcb-dri3-0:arm64 gstreamer1.0-plugins-base:arm64 libmount1:arm64 liblcms2-2:arm64 libxfixes3:arm64 libtiff6:arm64 libxcb-render0:arm64 libmd0:arm64 libelf1t64:arm64 libunistring5:arm64 libxrender1:arm64 libwebpdemux2:arm64 libgtk-4-1:arm64 libgav1-1:arm64 libthai0:arm64 libpsl5t64:arm64 libfontconfig1:arm64 gstreamer1.0-gl:arm64 libvulkan1:arm64 libwayland-server0:arm64 libwayland-egl1:arm64 shared-mime-info gstreamer1.0-plugins-ugly:arm64 libllvm19:arm64 libsharpyuv0:arm64 libxcb-dri2-0:arm64 libpng16-16t64:arm64 liblzma5:arm64 libtasn1-6:arm64 libxinerama1:arm64 libyuv0:arm64 libgraphene-1.0-0:arm64 libjbig0:arm64 libx11-xcb1:arm64 libjpeg-turbo8:arm64 gstreamer1.0-plugins-good:arm64 libglapi-mesa:arm64 libdeflate0:arm64 libflite1:arm64 libxcb-sync1:arm64 libcap2:arm64 libsecret-1-0:arm64 libpangocairo-1.0-0:arm64 libdrm-tegra0:arm64 libgdk-pixbuf-2.0-0:arm64 libdrm-freedreno1:arm64 libgles2:arm64 libgbm1:arm64 libgstreamer-plugins-bad1.0-0:arm64 libxcb-present0:arm64 libxcb-shm0:arm64 libwebp7:arm64 libabsl20220623t64:arm64 libfreetype6:arm64 libexpat1:arm64 libfribidi0:arm64 libdrm-etnaviv1:arm64 dconf-gsettings-backend:arm64 libgcc-s1:arm64 libwayland-cursor0:arm64 libdrm-amdgpu1:arm64 ca-certificates libwoff1:arm64 libbz2-1.0:arm64 libevdev2:arm64 liborc-0.4-0t64:arm64 glib-networking:arm64 libcairo-script-interpreter2:arm64 libwebpmux3:arm64 libc6:arm64 libdrm-radeon1:arm64 libnghttp2-14:arm64 libsensors5:arm64 libhyphen0:arm64 libepoxy0:arm64 libcairo-gobject2:arm64 libasound2t64:arm64 libenchant-2-2:arm64 libvpx9:arm64 libunwind8:arm64 libxcursor1:arm64 mesa-libgallium:arm64 libgstreamer1.0-0:arm64 libpixman-1-0:arm64 libglx0:arm64 libgstreamer-gl1.0-0:arm64 libpcre2-8-0:arm64 libmanette-0.2-0:arm64 libstdc++6:arm64 libxau6:arm64 zlib1g:arm64 libxml2:arm64 libxext6:arm64 libgl1:arm64 libx11-6:arm64 libpangoft2-1.0-0:arm64 libharfbuzz-icu0:arm64 gstreamer1.0-plugins-bad:arm64 libxcb1:arm64 libudev1:arm64 libgcrypt20:arm64 libxdamage1:arm64 libegl1:arm64 libxrandr2:arm64 libxcb-randr0:arm64 libsvtav1enc1d1:arm64 libdav1d7:arm64 libsqlite3-0:arm64 libevent-2.1-7t64:arm64 libtinfo6:arm64 libavif16:arm64 libxkbcommon0:arm64 libxslt1.1:arm64 libglvnd0:arm64 libcairo2:arm64 libgudev-1.0-0:arm64 libffi8:arm64 libglib2.0-0t64:arm64 libedit2:arm64 libbsd0:arm64 libxi6:arm64"

if ! which apt-get >/dev/null; then
    echo "This script only supports apt-get based distributions like Debian or Ubuntu."
    exit 1
fi

# Calling dpkg-query is slow, so call it only once and cache the results
TMPCHECKPACKAGES="$(mktemp)"
dpkg-query --show --showformat='${binary:Package} ${db:Status-Status}\n' > "${TMPCHECKPACKAGES}"
TOINSTALL=""
for PACKAGE in ${REQUIREDPACKAGES}; do
    if ! grep -qxF "${PACKAGE} installed" "${TMPCHECKPACKAGES}"; then
        TOINSTALL="${TOINSTALL} ${PACKAGE}"
    fi
done
rm -f "${TMPCHECKPACKAGES}"

if [[ -z "${TOINSTALL}" ]]; then
    echo "All required dependencies are already installed"
else
    echo "Need to install the following extra packages: ${TOINSTALL}"
    [[ ${#} -gt 0 ]] && [[ "${1}" == "--printonly" ]] && exit 0
    SUDO=""
    [[ ${UID} -ne 0 ]] && SUDO="sudo"
    AUTOINSTALL=""
    if [[ ${#} -gt 0 ]] && [[ "${1}" == "--autoinstall" ]]; then
        AUTOINSTALL="-y"
        export DEBIAN_FRONTEND="noninteractive"
        [[ ${UID} -ne 0 ]] && SUDO="sudo --preserve-env=DEBIAN_FRONTEND"
        ${SUDO} apt-get update
    fi
    set -x
    ${SUDO} apt-get install --no-install-recommends ${AUTOINSTALL} ${TOINSTALL}
fi
