# Contributor: memdmp <memdmp@estrogen.zone>
# Maintainer: memdmp <memdmp@estrogen.zone>

# Originally based on AUR package for vscodium
pkgname=vscodium
pkgver=1.96.0.24347
pkgrel=0
pkgdesc="Binary releases of VS Code without MS branding/telemetry/licensing"
url="https://github.com/VSCodium/vscodium"
#arch="all"
arch="x86_64"
license="MIT"
depends="fontconfig libxtst gtk+3.0 alsa-lib nss libnotify libxscrnsaver musl libxkbfile"
makedepends="nodejs-current yarn npm jq git git-lfs patch python3 py3-distutils-extra pkgconf bash krb5-dev libx11-dev libxkbfile-dev"
checkdepends=""
options="net"
install=""
subpackages=""
source="
freedesktop/${pkgname}.desktop
freedesktop/${pkgname}-wayland.desktop
freedesktop/${pkgname}-uri-handler.desktop
${pkgname}-${pkgver}.tar.gz::https://github.com/VSCodium/vscodium/releases/download/${pkgver}/VSCodium-${pkgver}-src.tar.gz
"
builddir="$srcdir/"

case "$CARCH" in
  x86_64)
    _vscode_arch=x64
    ;;
  aarch64)
    _vscode_arch=arm64
    ;;
  armv7h)
    _vscode_arch=arm
    ;;
  *)
    # Needed for mksrcinfo
    _vscode_arch=DUMMY
    ;;
esac
build() {
# Deactivate any pre-loaded nvm, and make sure we use our own in the current source directory
#    command -v nvm >/dev/null && nvm deactivate && nvm unload
#    export NVM_DIR="${srcdir}/.nvm"
#    source /usr/share/nvm/init-nvm.sh || [[ $? != 1 ]]

    # Install the correct version of NodeJS (read from .nvmrc)
#    nvm install $(cat .nvmrc)
#    nvm use

    # Check if the correct version of node is being used
#    nvmrc_version="$(cat .nvmrc)"
#    if [[ "$nvmrc_version" != "v"* ]]
#    then
        # Add the v prefix, because it seems to be missing in .nvmrc
#        echo "Configured .nvmrc version is [$nvmrc_version], adding the v prefix before checking if it matches with the node command."
#        nvmrc_version="v$nvmrc_version"
#    fi

    # Now check if the version matches exactly, or at least starts with the same prefix
#    if [[ "$(node --version)" != "$nvmrc_version"* ]]
#    then
#    	echo "Using the wrong version of NodeJS! Expected ["$nvmrc_version"] but using ["$(node --version)"]."
#    	exit 1
#    fi
#    echo "Installed version of node ["$(node --version)"] matches required version ["$nvmrc_version"], continuing."

    # Remove old build
    if [ -d "vscode" ]; then
        rm -rf vscode* VSCode*
    fi

    # Export necessary environment variables
    export SHOULD_BUILD="yes"
    export SHOULD_BUILD_REH="no"
    export CI_BUILD="no"
    export OS_NAME="linux"
    export VSCODE_ARCH="$_vscode_arch"
    export VSCODE_QUALITY="stable"
    export RELEASE_VERSION="$pkgver"
    # the app will be updated with apk
    export DISABLE_UPDATE="yes"

    # Disabling this patch, since it is for win32 and does not apply here
    rm -rf patches/cleanup-archive.patch
    # Same for ppc64le-support.patch since that is not a supported architecture
    rm -rf patches/ppc64le-support.patch
    bash -c '
    set -eax
    . get_repo.sh
    . build.sh
    '
}

check() {
	# Replace with proper check command(s).
	# Remove and add !check option if there is no check command.
	:
}

package() {
    bash -c "
    set -eax
    install -d -m755 ${pkgdir}/usr/bin
    install -d -m755 ${pkgdir}/usr/share/{${pkgname},applications,pixmaps}
    install -d -m755 ${pkgdir}/usr/share/licenses/${pkgname}

    cp -r ${srcdir}/VSCode-linux-${_vscode_arch}/* ${pkgdir}/usr/share/${pkgname}
    cp -r ${srcdir}/VSCode-linux-${_vscode_arch}/resources/app/LICENSE.txt ${pkgdir}/usr/share/licenses/${pkgname}
    
    ln -s /usr/share/${pkgname}/bin/codium ${pkgdir}/usr/bin/codium
    ln -s /usr/share/${pkgname}/bin/codium ${pkgdir}/usr/bin/vscodium
    
    install -D -m644 ${pkgname}.desktop ${pkgdir}/usr/share/applications/${pkgname}.desktop
    install -D -m644 ${pkgname}-wayland.desktop ${pkgdir}/usr/share/applications/${pkgname}-wayland.desktop
    install -D -m644 ${pkgname}-uri-handler.desktop ${pkgdir}/usr/share/applications/${pkgname}-uri-handler.desktop
    install -D -m644 ${srcdir}/VSCode-linux-${_vscode_arch}/resources/app/resources/linux/code.png ${pkgdir}/usr/share/pixmaps/${pkgname}.png

    # Symlink shell completions
    # TODO: split into completion packages
    install -d -m755 ${pkgdir}/usr/share/zsh/site-functions
    install -d -m755 ${pkgdir}/usr/share/bash-completion/completions
    ln -s /usr/share/${pkgname}/resources/completions/zsh/_codium ${pkgdir}/usr/share/zsh/site-functions
    ln -s /usr/share/${pkgname}/resources/completions/bash/codium ${pkgdir}/usr/share/bash-completion/completions
    "
}

sha512sums="
9d23e7b2484773d77437f882d8669a423cea9511e366b18795c450148aacf609993d8eb2200fbb0b7573a3740e2d84944a428e56d3d78785f32de1041120dbfd  vscodium.desktop
16f560738d72d7dec918c716c9f3073249d76175743b8ffa8b21764913f3d990b16467bdeb6be43aa80a45787bf4e6deb55d0ac132a8bebed06d76f6f383cf97  vscodium-wayland.desktop
999fce259ff9a9217a756c916ed8a75bf211ecf61b5d65e0b10f6d9e27c0fca384ad6429198a2c8d6788889d77fc1b8be7af6e71bfd50dcaaf0085638bf5637c  vscodium-uri-handler.desktop
3e6877b987f1ef1ec0fd8606d1fb544f8045d645257fee894bbc47168f5bdd6353d00ad6331f6c102cd0d993ffe8aac34e580911b65550ba1bec542a0ca46de5  vscodium-1.96.0.24347.tar.gz
"