#!/usr/bin/env bash

# Install VSCodium

# https://repo.vscodium.dev/
GPG_URL="https://repo.vscodium.dev/vscodium.gpg"
GPG="/usr/share/keyrings/vscodium.gpg"
SRC="/etc/apt/sources.list.d/vscodium.sources"

if [ ! -e $GPG ]; then
    curl -fsSL $GPG_URL \
        | gpg --dearmor \
        | dd of=$GPG
fi

if [ ! -e $GPG ]; then
    echo "Could not download $GPG_URL to $GPG"
    exit 1
fi

# Original sources file includes not supported armhf and arm64 architectures
#curl --output-dir /etc/apt/sources.list.d -LO https://repo.vscodium.dev/vscodium.sources

if [ ! -e $SRC ]; then
    echo "Write $SRC"
    echo cat >> $SRC << EOF
# VSCodium source file
Types: deb
URIs: https://repo.vscodium.dev/deb
Suites: vscodium
Components: main
Architectures: amd64
Signed-by: $GPG"
EOF
fi

if [ ! -e $SRC ]; then
    echo "Failed to write $SRC"
    exit 2
fi

# Update and install
apt-get update
apt-get -y --autoremove --no-install-recommends install linux-headers-`uname -r` build-essential python3-all gdb codium

if ! command -v codium >/dev/null 2>&1; then
    echo "Failed to install Codium"
    exit 3
fi

exit 0
