abinfo "Setting VSCODE_QUALITY and RELEASE_VERSION var ..."
export VSCODE_QUALITY="stable"
export RELEASE_VERSION="${PKGVER}"

abinfo "Getting vscode repo ..."
"$SRCDIR"/get_repo.sh

abinfo "Setting build var ..."
export SKIP_LINUX_PACKAGES="True"
export SHOULD_BUILD=yes
export SHOULD_BUILD_REH=no
export SHOULD_BUILD_REH_WEB=no
export SHOULD_BUILD_CLI=no
export CI_BUILD=yes
export OS_NAME=linux
export RELEASE_VERSION="$PKGVER"
export DISABLE_UPDATE=yes
export VSCODE_USE_SYSTEM_CARGO=yes

if [[ "${CROSS:-$ARCH}" = "amd64" ]]; then
  export VSCODE_ARCH="x64"
elif [[ "${CROSS:-$ARCH}" = "arm64" ]]; then
  export VSCODE_ARCH="arm64"
elif [[ "${CROSS:-$ARCH}" = "loongarch64" ]]; then
  export VSCODE_ARCH="loong64"
elif [[ "${CROSS:-$ARCH}" = "loongarch64_nosimd" ]]; then
  export VSCODE_ARCH="loong64"
elif [[ "${CROSS:-$ARCH}" = "ppc64el" ]]; then
  export VSCODE_ARCH="ppc64le"
elif [[ "${CROSS:-$ARCH}" = "riscv64" ]]; then
  export VSCODE_ARCH="riscv64"
else
  aberr "Platform not supported" && exit 1
fi

# Playwright's postinstall script may hang in versions earlier than v1.16.0.
# Since we won't run tests here, it's safe to skip the browser download.
abinfo "Setting Playwright flag to skip browser download ..."
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1

abinfo "Building ..."
"$SRCDIR"/build.sh

# Temporarily fix a library bug that causes electron downloading failure, PR: https://github.com/microsoft/vscode-gulp-electron/pull/20
pushd "$SRCDIR"/vscode
jq '.devDependencies."@vscode/gulp-electron" = "git+https://github.com/darkyzhou/vscode-gulp-electron.git#94825f69fa68bb0affece10657106f3ac930ddac"' package.json > package.json.tmp
mv package.json.tmp package.json
npm i
popd

# Cheat the extraction logic in package_linux_bin.sh
tar -czf "$SRCDIR"/vscode.tar.gz -T /dev/null

abinfo "Packaging ..."
"$SRCDIR"/build/linux/package_bin.sh

abinfo "Switching to SRCDIR ..."
cd "$SRCDIR"

abinfo "Installing vscodium ..."
install -dvm755 "${PKGDIR}"/usr/bin
install -dvm755 "${PKGDIR}"/usr/share/{applications,pixmaps}
install -dvm755 "${PKGDIR}"/usr/lib/vscodium
install -dvm755 "${PKGDIR}"/usr/share/licenses/vscodium
cp -rv "${SRCDIR}"/VSCode-linux-${VSCODE_ARCH}/* "${PKGDIR}"/usr/lib/vscodium
ln -sfv /usr/lib/vscodium/bin/codium "${PKGDIR}"/usr/bin/codium
ln -sfv /usr/lib/vscodium/bin/codium "${PKGDIR}"/usr/bin/vscodium
install -Dvm644 "${SRCDIR}"/VSCode-linux-${VSCODE_ARCH}/resources/app/resources/linux/code.png \
        "${PKGDIR}"/usr/share/pixmaps/vscodium.png

abinfo "Installing shell completions ..."
install -Dvm644 "$SRCDIR/VSCode-linux-$VSCODE_ARCH/resources/completions/zsh/_codium" "$PKGDIR/usr/share/zsh/site-functions/_codium"
install -Dvm644 "$SRCDIR/VSCode-linux-$VSCODE_ARCH/resources/completions/bash/codium" "$PKGDIR/usr/share/bash-completion/completions/codium"
