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

REQUIREDPACKAGES="libgbm1:arm64 libdrm-tegra0:arm64 libexpat1:arm64 libgcrypt20:arm64 libwebpdemux2:arm64 libasound2:arm64 libicu67:arm64 libidn2-0:arm64 libxml2:arm64 libgnutls30:arm64 libpango-1.0-0:arm64 libraw1394-11:arm64 libxi6:arm64 libxcb1:arm64 libxslt1.1:arm64 libxrender1:arm64 libmpg123-0:arm64 libunwind8:arm64 libepoxy0:arm64 libglx-mesa0:arm64 libxinerama1:arm64 libelf1:arm64 libilmbase25:arm64 libzstd1:arm64 libgl1-mesa-dri:arm64 libwoff1:arm64 libx11-xcb1:arm64 libopenjp2-7:arm64 libvorbisenc2:arm64 libmd0:arm64 libglx0:arm64 libcom-err2:arm64 libgtk-3-0:arm64 libv4l-0:arm64 libuuid1:arm64 libxcomposite1:arm64 libgraphite2-3:arm64 libglapi-mesa:arm64 libhogweed6:arm64 libpulse0:arm64 libasyncns0:arm64 libgssapi-krb5-2:arm64 libbsd0:arm64 libdrm-amdgpu1:arm64 libxdamage1:arm64 libwrap0:arm64 libharfbuzz-icu0:arm64 libharfbuzz0b:arm64 libsqlite3-0:arm64 libtheora0:arm64 libfdk-aac2:arm64 libogg0:arm64 libnettle8:arm64 libusb-1.0-0:arm64 libgl1:arm64 libdrm2:arm64 libgcc-s1:arm64 shared-mime-info libxext6:arm64 libdrm-freedreno1:arm64 libxdmcp6:arm64 libgles2:arm64 libkrb5-3:arm64 libnsl2:arm64 libudev1:arm64 libbz2-1.0:arm64 libfreetype6:arm64 libsystemd0:arm64 libkrb5support0:arm64 libfribidi0:arm64 libv4lconvert0:arm64 librsvg2-2:arm64 libdrm-etnaviv1:arm64 libcairo-gobject2:arm64 libegl1:arm64 zlib1g:arm64 libkeyutils1:arm64 libevent-2.1-7:arm64 libdw1:arm64 libxfixes3:arm64 libdrm-radeon1:arm64 libgdk-pixbuf-2.0-0:arm64 libstdc++6:arm64 ca-certificates libk5crypto3:arm64 libxau6:arm64 libdrm-nouveau2:arm64 libp11-kit0:arm64 libkate1:arm64 libc6:arm64 libnghttp2-14:arm64 libwebp6:arm64 libgudev-1.0-0:arm64 libpcre3:arm64 libx11-6:arm64 libcairo2:arm64 libunistring2:arm64 libthai0:arm64 libpangocairo-1.0-0:arm64 libjpeg62-turbo:arm64 liblz4-1:arm64 libdatrie1:arm64 libsndfile1:arm64 libgmp10:arm64 libdc1394-25:arm64 libxcursor1:arm64 libopenexr25:arm64 libdbus-1-3:arm64 libxcb-shm0:arm64 libjson-glib-1.0-0:arm64 libglvnd0:arm64 libfontconfig1:arm64 libegl-mesa0:arm64 libflac8:arm64 libproxy1v5:arm64 libtasn1-6:arm64 libpng16-16:arm64 libssl1.1:arm64 libpangoft2-1.0-0:arm64 libflite1:arm64 libtirpc3:arm64 libgpg-error0:arm64 libpixman-1-0:arm64 liblzma5:arm64 libffi7:arm64 libxrandr2:arm64 libvorbis0a:arm64 libpsl5:arm64 liblcms2-2:arm64 libxcb-render0:arm64 libopus0: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
