#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# vscode-helper 1.0 — Download and install Microsoft Visual Studio Code
# InterGenOS extra tier
#
# VS Code is proprietary software. This helper downloads it from
# Microsoft's official distribution. The user accepts Microsoft's
# license terms by running this installer.

configure() {
    set -e
    :
}

build() {
    set -e
    :
}

do_install() {
    set -e
    # Install the helper script
    mkdir -pv "${DESTDIR}/usr/bin"

    # K21.F: install the pinned Microsoft apt-repo signing key
    # (dearmored binary keyring) at /usr/share/igos/helpers/keyrings/
    # vscode-keyring.gpg. The runtime helper sources helper-lib.sh and
    # calls igos_helper_verify_deb_via_signed_release with this keyring
    # path. Trust-anchor reuse: the Microsoft apt-repo signing key is
    # the SAME key K21.E pinned for edge-helper (microsoft.asc
    # fingerprint BC528686B50D79E339D3721CEB3E94ADBE1229CF, verified
    # against vendor publication at https://learn.microsoft.com/en-us/
    # linux/packages); reading the source .asc from edge-helper's
    # assets dir at build time avoids redundant key duplication in the
    # repo per the SPOC K21.F dispatch recommendation. Cross-package
    # source coupling: vscode-helper requires edge-helper's assets dir
    # to exist at build time (acceptable since both ship together in
    # packages/extra/).
    install -dm755 "${DESTDIR}/usr/share/igos/helpers/keyrings"
    gpg --dearmor < \
        "${IGOS_SOURCE_ROOT:-/mnt/intergenos}/packages/extra/edge-helper/assets/edge-keyring.asc" \
        > "${DESTDIR}/usr/share/igos/helpers/keyrings/vscode-keyring.gpg"
    chmod 644 "${DESTDIR}/usr/share/igos/helpers/keyrings/vscode-keyring.gpg"

    cat > "${DESTDIR}/usr/bin/igos-install-vscode" << 'HELPEREOF'
#!/bin/bash
# InterGenOS Visual Studio Code Installer
#
# Downloads and installs VS Code from Microsoft's official source.
# License: https://code.visualstudio.com/license
#
# H-007 Phase B migration: records the install footprint via the
# /usr/share/igos/helpers/helper-lib.sh API.

set -e

source /usr/share/igos/helpers/helper-lib.sh

ACCEPTANCE_DIR="/var/lib/intergen/legal"
ACCEPTANCE_FILE="$ACCEPTANCE_DIR/vscode-helper-1.0-accepted.json"

# K21.F: pivot from the unsigned direct tar.gz URL (https://code.
# visualstudio.com/sha/download?build=stable&os=linux-x64) to the
# canonical apt-pool path on packages.microsoft.com. The direct URL
# was HTTPS-only soft trust -- no signed metadata, no per-version
# sha256. The apt-pool path provides the InRelease + Packages chain
# that igos_helper_verify_deb_via_signed_release verifies.
VSCODE_APT_BASE="https://packages.microsoft.com/repos/vscode"
VSCODE_DIST="stable"
VSCODE_PKG_NAME="code"
VSCODE_KEYRING="/usr/share/igos/helpers/keyrings/vscode-keyring.gpg"
TMPDIR=$(mktemp -d)
# BLOCKING-D fix (2026-05-19): register TMPDIR cleanup via the
# helper-lib's IGOS_HELPER_USER_CLEANUP env var instead of `trap EXIT`.
# Installing a native trap would collide with the one igos_helper_init
# installs for partial-manifest sidecar emission (bash trap-replace
# semantics; no native composition).
IGOS_HELPER_USER_CLEANUP="rm -rf $TMPDIR"

echo ""
echo "  InterGenOS Visual Studio Code Installer"
echo "  ========================================"
echo ""
echo "  Visual Studio Code is proprietary software."
echo "  License: https://code.visualstudio.com/license"
echo ""

# Check for root
if [ "$(id -u)" -ne 0 ]; then
    echo "  ERROR: Run via 'sudo pkm install-helper vscode' instead."
    echo "  Direct invocation bypasses pkm's manifest ingestion;"
    echo "  pkm files/verify/remove will not see the installed files."
    exit 1
fi

# EULA acceptance gate. The acceptance record at /var/lib/intergen/legal/
# is intentionally NOT manifest-tracked: pkm remove leaves it in place
# so a subsequent reinstall reads existing acceptance and skips the
# re-prompt (Row F decision 2026-05-19; ffmpeg-nonfree-helper canonical
# K21.C pattern). The record IS captured in pkm's operation log as
# transparency content via the post_install_action below.
if [ -f "$ACCEPTANCE_FILE" ]; then
    echo "  Acceptance already recorded at $ACCEPTANCE_FILE"
    echo "  Proceeding to install."
else
    echo ""
    echo "  Do you accept Microsoft's VS Code license above and authorize"
    echo "  installing Visual Studio Code on this machine for your own use?"
    echo "  Type 'I ACCEPT' (exact match, capitals) to proceed:"
    echo ""
    read -r REPLY
    if [ "$REPLY" != "I ACCEPT" ]; then
        echo "  Acceptance not given. Exiting."
        exit 1
    fi
    mkdir -p "$ACCEPTANCE_DIR"
    cat > "$ACCEPTANCE_FILE" <<JSON
{
  "helper": "vscode-helper",
  "version": "1.0",
  "payload_license": "LicenseRef-Microsoft-VSCode-Software-License",
  "accepted_at": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "user": "$(logname 2>/dev/null || echo unknown)"
}
JSON
    chmod 644 "$ACCEPTANCE_FILE"
    echo "  Acceptance recorded at $ACCEPTANCE_FILE"
fi

igos_helper_init "vscode"

# K21.C: capture the consent event in pkm's operation log as
# transparency content. The acceptance JSON itself is intentionally
# NOT manifest-tracked (see EULA acceptance gate above).
igos_helper_record_post_install_action \
    "User accepted Microsoft VS Code license (acceptance artifact at $ACCEPTANCE_FILE)"

echo "  Finding latest Visual Studio Code release in signed apt metadata..."
LATEST=$(igos_helper_find_latest_deb_in_packages "$VSCODE_PKG_NAME" "$VSCODE_APT_BASE" "$VSCODE_DIST")
if [ -z "$LATEST" ]; then
    echo "  ERROR: Could not locate code package in the official PMC apt"
    echo "         Packages metadata at ${VSCODE_APT_BASE}/dists/${VSCODE_DIST}/"
    exit 1
fi
DEB_NAME=$(echo "$LATEST" | cut -d'|' -f1)
VSCODE_VERSION=$(echo "$LATEST" | cut -d'|' -f2)
POOL_PATH=$(echo "$LATEST" | cut -d'|' -f3)
igos_helper_set_version "${VSCODE_VERSION:-unknown}"

echo "  Downloading ${DEB_NAME}..."
wget -q --show-progress -O "$TMPDIR/vscode.deb" "${VSCODE_APT_BASE}/${POOL_PATH}"

# K21.F: refuse install if signed-Release verification fails.
echo "  Verifying signed-Release integrity chain..."
if ! igos_helper_verify_deb_via_signed_release \
    "$DEB_NAME" \
    "$TMPDIR/vscode.deb" \
    "$VSCODE_APT_BASE" \
    "$VSCODE_KEYRING" \
    "$VSCODE_DIST"; then
    echo ""
    echo "  ERROR: Signed-Release verification FAILED for ${DEB_NAME}."
    echo "  Refusing to install. The downloaded .deb did not match the"
    echo "  vendor's signed apt-repo metadata. This may indicate network"
    echo "  corruption, a man-in-the-middle attack, or a vendor key"
    echo "  rotation. Do NOT extract the .deb manually."
    exit 1
fi

echo "  Extracting to /opt/vscode/..."
cd "$TMPDIR"
ar x vscode.deb
# The code .deb has historically used data.tar.xz; probe alternatives
# (gz / zst) for forward-compatibility with future compressor swaps.
if [ -f data.tar.xz ]; then
    tar -xf data.tar.xz
elif [ -f data.tar.zst ]; then
    tar --zstd -xf data.tar.zst
elif [ -f data.tar.gz ]; then
    tar -xzf data.tar.gz
fi
rm -rf /opt/vscode
mkdir -p /opt/vscode
cp -a usr/share/code/. /opt/vscode/
mkdir -p /usr/share/applications
cp -a usr/share/applications/code*.desktop /usr/share/applications/ 2>/dev/null || true
cp -a usr/share/man/* /usr/share/man/ 2>/dev/null || true

# H-007: record everything deposited under /opt/vscode + the .desktop
# launchers + man pages.
while IFS= read -r f; do
    igos_helper_record_file "$f"
done < <(find /opt/vscode -type f -o -type l 2>/dev/null)
for f in /usr/share/applications/code*.desktop /usr/share/man/man*/code*; do
    if [ -f "$f" ]; then
        igos_helper_record_file "$f"
    fi
done

# Create /usr/bin/code symlink pointing into /opt/vscode. The code .deb
# places the binary at usr/share/code/code so the post-cp-a layout has
# the binary at /opt/vscode/code (not /opt/vscode/bin/code as the prior
# tar.gz layout used).
ln -sf /opt/vscode/code /usr/bin/code
igos_helper_record_symlink /usr/bin/code /opt/vscode/code

igos_helper_record_dep glibc

igos_helper_commit

echo ""
echo "  Visual Studio Code installed successfully!"
echo "  Run: code"
echo ""
HELPEREOF
    chmod 755 "${DESTDIR}/usr/bin/igos-install-vscode"
}
