#!/bin/sh
#
# Repack monaco-editor-core_<version>+ds.orig.tar.gz.  Called by
# `debian/rules get-orig-source`; VERSION= overrides the version and otherwise
# the top entry of debian/changelog is used.
#
# Upstream does not release this as a source tarball: monaco-editor-core is a
# subtree of microsoft/vscode, taken at the commit upstream records in the
# package's own npm metadata (the "vscodeCommitId" field).  That commit is the
# only input, so the orig is one tarball and not a MUT component.
#
# Only a subtree is wanted, so the archive is taken from codeload: it serves
# the tree of one commit, which is much less than cloning vscode for it.
#
# This is a maintainer tool: it needs network access and is never invoked by a
# build.

set -eu

PKG=monaco-editor-core
ROOT=$(cd "$(dirname "$0")/.." && pwd)

VERSION=${1:-${VERSION:-}}
if [ -z "$VERSION" ]; then
    VERSION=$(dpkg-parsechangelog -l"$ROOT/debian/changelog" -SVersion |
        sed -e 's/^[0-9]*://' -e 's/-[^-]*$//' -e 's/+ds$//')
fi
UVERSION=$VERSION+ds
TARBALL=$ROOT/../${PKG}_${UVERSION}.orig.tar.gz

# Subtree of microsoft/vscode that makes up monaco-editor-core.  The source
# boundary is upstream's own src/tsconfig.monaco.json.  Of build/ only lib/
# (the build library this package drives directly) and monaco/ (the recipes and
# the npm package metadata) are used; the rest is the Windows/VS Code product
# build, the npm bootstrap and the rspack/vite tooling, none of which this
# source package needs.  The loader and the compiler plugins in src/vs are the
# ESM chain: extract-editor-esm drops them from the tree, but the worker entry
# point imports src/vs/loader, so the shaker needs it present.
PATHS="package.json tsfmt.json build/lib build/monaco src/tsconfig.base.json src/tsconfig.monaco.json src/typings src/vs/amdX.ts src/vs/nls.ts src/vs/monaco.d.ts src/vs/base src/vs/editor src/vs/platform src/vs/loader.js src/vs/loader.d.ts src/vs/css.ts src/vs/css.build.ts src/vs/nls.build.ts src/vs/nls.mock.ts"

# Third party material shipped by Debian as separate source packages, plus the
# Windows-only packaging assets.  The two prebuilt bundles come from
# node-dompurify and node-marked; the icon font is built by fonts-codicons from
# upstream's SVG.  build/win32/ is the Inno Setup installer of the Windows
# product and build/lib/watch/watcher.exe is the Windows file watcher; neither
# is used when building the browser ESM tree, and keeping them only makes the
# source package carry prebuilt Windows binaries
# (lintian: source-contains-prebuilt-windows-binary).
EXCLUDE="src/vs/base/browser/dompurify/dompurify.js src/vs/base/common/marked/marked.js src/vs/base/browser/ui/codicons/codicon/codicon.ttf build/win32 build/lib/watch/watcher.exe"

WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

log() { printf 'get-orig-source: %s\n' "$*" >&2; }

mkdir -p "$WORK/$PKG-$UVERSION"
STAGE=$WORK/$PKG-$UVERSION

log "resolving vscode commit for $PKG $VERSION"
COMMIT=$(curl -4 -fsSL "https://registry.npmjs.org/$PKG/$VERSION" |
    tr -d ' \t\n' |
    sed -n 's/.*"vscodeCommitId":"\([0-9a-f]\{40\}\)".*/\1/p')
case $COMMIT in
????????????????????????????????????????) ;;
*) log "cannot parse vscodeCommitId (got '$COMMIT')"; exit 1 ;;
esac
log "vscodeCommitId = $COMMIT"

log "fetching microsoft/vscode at $COMMIT"
curl -4 -fsSL --retry 3 --retry-all-errors --retry-delay 5 --max-time 3600 \
    -o "$WORK/vscode.tar.gz" \
    "https://codeload.github.com/microsoft/vscode/tar.gz/$COMMIT"

# The archive carries the commit date as the mtime of every member, which is
# the timestamp the orig tarball is built at.
COMMITEPOCH=$(TZ=UTC date -d "$(tar --utc -tvzf "$WORK/vscode.tar.gz" |
    head -n 1 | awk '{ print $4 " " $5 }') UTC" +%s)
COMMITDATE=$(TZ=UTC date -d "@$COMMITEPOCH" '+%Y-%m-%dT%H:%M:%SZ')

log "extracting the editor subtree"
set --
for p in $PATHS; do set -- "$@" "*/$p"; done
tar -xzf "$WORK/vscode.tar.gz" -C "$STAGE" --strip-components=1 \
    --wildcards --no-wildcards-match-slash "$@"
for f in $EXCLUDE; do
    rm -rf "$STAGE/$f"
done

find "$STAGE" -type d -exec chmod 755 {} +
find "$STAGE" -type f -exec chmod 644 {} +

log "packing $(basename "$TARBALL") (mtime: $COMMITDATE)"
tar --sort=name --owner=0 --group=0 --numeric-owner --mtime="@$COMMITEPOCH" \
    -czf "$TARBALL" -C "$WORK" "$PKG-$UVERSION"

log "done"
sha256sum "$TARBALL"
