#!/bin/sh
set -eu

target=${1:?usage: install-linux-build-dependencies.sh arm64|x64}

install_packages() {
    sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends "$@"
}

case "$target" in
    arm64)
        sudo apt-get update
        install_packages \
            build-essential \
            cmake \
            libgtk-3-dev \
            libwebkit2gtk-4.1-dev \
            libxdo-dev \
            pkg-config
        ;;
    x64)
        if [ "$(dpkg --print-architecture)" != "arm64" ]; then
            echo "the x64 release build expects an arm64 Ubuntu runner" >&2
            exit 1
        fi
        if [ ! -r /etc/os-release ]; then
            echo "cannot determine the Ubuntu release" >&2
            exit 1
        fi
        . /etc/os-release
        if [ "${ID:-}" != "ubuntu" ] || [ -z "${VERSION_CODENAME:-}" ]; then
            echo "the x64 release build requires an Ubuntu runner" >&2
            exit 1
        fi

        native_sources=/etc/apt/sources.list.d/ubuntu.sources
        if [ ! -f "$native_sources" ]; then
            echo "the x64 release build requires Ubuntu's deb822 package sources" >&2
            exit 1
        fi
        sudo sed -i \
            '/^Architectures:/d; /^Types: deb$/a Architectures: arm64' \
            "$native_sources"
        sudo apt-get update

        install_packages build-essential cmake gcc-x86-64-linux-gnu g++-x86-64-linux-gnu pkg-config

        sudo dpkg --add-architecture amd64
        {
            printf '%s\n' \
                'Types: deb' \
                'URIs: http://archive.ubuntu.com/ubuntu' \
                "Suites: $VERSION_CODENAME ${VERSION_CODENAME}-updates" \
                'Components: main universe restricted multiverse' \
                'Architectures: amd64' \
                'Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg' \
                '' \
                'Types: deb' \
                'URIs: http://security.ubuntu.com/ubuntu' \
                "Suites: ${VERSION_CODENAME}-security" \
                'Components: main universe restricted multiverse' \
                'Architectures: amd64' \
                'Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg'
        } | sudo tee /etc/apt/sources.list.d/ruds-amd64.sources >/dev/null
        sudo apt-get update

        # Foreign development packages contain architecture-specific helper
        # programs whose maintainer scripts cannot run on the arm64 host.
        # Download them without installing and extract a link-only sysroot.
        sysroot=/opt/ruds-x64-sysroot
        archives=/tmp/ruds-x64-archives
        sudo mkdir -p "$archives/partial" "$sysroot"
        sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
            --download-only \
            -o Dir::Cache::archives="$archives" \
            libgtk-3-dev:amd64 \
            libwebkit2gtk-4.1-dev:amd64 \
            libxdo-dev:amd64
        for package in "$archives"/*.deb; do
            sudo dpkg-deb --extract "$package" "$sysroot"
        done

        # libc's development linker scripts use legacy absolute paths even
        # though Noble stores their targets below /usr. Materialize those few
        # aliases so GNU ld can resolve them inside the extracted sysroot.
        legacy_lib="$sysroot/lib/x86_64-linux-gnu"
        sudo mkdir -p "$legacy_lib" "$sysroot/lib64"
        for library in libc.so.6 libm.so.6 libmvec.so.1; do
            source="$sysroot/usr/lib/x86_64-linux-gnu/$library"
            if [ ! -f "$source" ]; then
                echo "the downloaded amd64 libc is missing $library" >&2
                exit 1
            fi
            sudo cp -a "$sysroot/usr/lib/x86_64-linux-gnu/$library" "$legacy_lib/"
        done
        loader="$sysroot/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2"
        if [ ! -f "$loader" ]; then
            echo "the downloaded amd64 libc is missing its dynamic loader" >&2
            exit 1
        fi
        sudo cp -a "$sysroot/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2" \
            "$sysroot/lib64/"

        # Architecture-independent development packages may already be
        # installed on the host, so apt does not download them again. Their
        # pkg-config files contain no host binaries and are safe to reuse with
        # PKG_CONFIG_SYSROOT_DIR, which redirects every path into this sysroot.
        sudo mkdir -p "$sysroot/usr/share/pkgconfig"
        sudo cp -a /usr/share/pkgconfig/. "$sysroot/usr/share/pkgconfig/"
        ;;
    *)
        echo "unsupported Linux build target: $target" >&2
        exit 1
        ;;
esac
