#!/usr/bin/env bash
set -e

ARCH=$(arch)
PKG_BASE="make libmpv-dev cmake clang clang-format manpages man-db"
PKG_BACKPORT=""

PKG_BACKPORT+=" qt6-base-dev qt6-declarative-dev qt6-multimedia-dev qt6-tools-private-dev"
PKG_BACKPORT+=" qt6-tools-dev-tools qt6-tools-dev qt6-5compat-dev"
PKG_BACKPORT+=" qt6-image-formats-plugins qt6-gtk-platformtheme qt6-base-abi libqt6xml6"
PKG_BACKPORT+=" libqt6multimedia6 libqt6concurrent6 libqt6multimediawidgets6 "
PKG_BACKPORT+=" libqt6opengl6 libqt6openglwidgets6 libqt6widgets6"
PKG_BACKPORT+=" qt6-translations-l10n qt6-l10n-tools"
PKG_BACKPORT+=" libdrm2 libdrm-nouveau2 libdrm-radeon1 libdrm-amdgpu1 libdrm-common libdrm-intel1"

PKG_BACKPORT+=" qt6-wayland-dev qt6-wayland qt6-wayland-dev-tools"
PKG_BACKPORT+=" libqt6waylandclient6 libqt6waylandcompositor6 libqt6waylandeglclienthwintegration6"
PKG_BACKPORT+=" libqt6waylandeglcompositorhwintegration6 libqt6wlshellintegration6"

PKG_BACKPORT+=" qt6-qpa-plugins"

function appimage_setup() {
    local APPIMAGE=$(basename "$1")
    wget "$1"
    chmod +x ${APPIMAGE}
    ./${APPIMAGE} --appimage-extract
    rm -rf ${APPIMAGE}
}

echo "deb     http://deb.debian.org/debian bullseye-backports main contrib non-free" >> /etc/apt/sources.list
echo "deb-src http://deb.debian.org/debian bullseye-backports main contrib non-free" >> /etc/apt/sources.list

apt update
apt upgrade -y
apt -y install $PKG_BASE
apt -y install -t bullseye-backports $PKG_BACKPORT
rm -rf /var/cache/

appimage_setup \
    https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-$ARCH.AppImage

appimage_setup \
    https://github.com/linuxdeploy/linuxdeploy-plugin-qt/releases/download/continuous/linuxdeploy-plugin-qt-$ARCH.AppImage

appimage_setup \
    https://github.com/linuxdeploy/linuxdeploy/releases/download/continuous/linuxdeploy-$ARCH.AppImage
