#!/bin/sh
#
# Repack monaco-editor_<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 cuts a vX.Y.Z tag per release and GitHub serves its sources, so the
# tarball is that archive, repacked -- hence the +ds.  What is taken out:
#
#   * the demo projects (samples/), the documentation website (website/) and
#     the webpack plugin (webpack-plugin/).  None of them is built by this
#     package; the samples and the website carry lock files of their own, and
#     the website a prebuilt tarball;
#   * the CI and editor configuration (.github/, .azure-pipelines/,
#     .devcontainer/, .husky/, .vscode/, test-results/), and test/manual/, the
#     browser playground whose samples/ are 1.5 MB of third party documents;
#   * five files upstream generates at release time with `npm run
#     import-typescript` from the release of TypeScript it pins: the compiler
#     itself, the lib.*.d.ts it ships and their declarations.  Debian packages
#     TypeScript, so debian/build.sh rebuilds them from it -- a prebuilt
#     compiler in the source package would be dead weight at best.
#
# The rest of the tree, test/ and docs/ included, is upstream's.
#
# This is a maintainer tool: it needs network access and is never invoked by a
# build.

set -eu

PKG=monaco-editor
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
TAG=v$VERSION

DROP="samples
website
webpack-plugin
.azure-pipelines
.devcontainer
.github
.husky
.vscode
test-results
test/manual"

GENERATED="src/language/typescript/lib/lib.ts
src/language/typescript/lib/lib.index.ts
src/language/typescript/lib/typescriptServices.js
src/language/typescript/lib/typescriptServices.d.ts
src/language/typescript/lib/typescriptServicesMetadata.ts"

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

log "resolving microsoft/monaco-editor $TAG"
INFO=$(curl -4 -fsS "https://api.github.com/repos/microsoft/monaco-editor/commits/$TAG")
COMMIT=$(printf '%s' "$INFO" | python3 -c 'import json, sys; print(json.load(sys.stdin)["sha"])')
EPOCH=$(printf '%s' "$INFO" | python3 -c '
import calendar, datetime, json, sys
date = json.load(sys.stdin)["commit"]["committer"]["date"]
print(calendar.timegm(datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%SZ").timetuple()))
')
case $COMMIT in
????????????????????????????????????????) ;;
*) log "cannot parse the commit (got '$COMMIT')"; exit 1 ;;
esac
COMMITDATE=$(TZ=UTC date -d "@$EPOCH" '+%Y-%m-%dT%H:%M:%SZ')
log "$TAG is $COMMIT ($COMMITDATE)"

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

log "fetching the archive"
curl -4 -fsSL --retry 3 --retry-all-errors --retry-delay 5 --max-time 3600 \
	-o "$WORK/upstream.tar.gz" \
	"https://codeload.github.com/microsoft/monaco-editor/tar.gz/refs/tags/$TAG"

mkdir -p "$WORK/$PKG-$UVERSION"
tar xzf "$WORK/upstream.tar.gz" -C "$WORK/$PKG-$UVERSION" --strip-components=1
STAGE=$WORK/$PKG-$UVERSION

for f in $DROP $GENERATED; 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="@$EPOCH" \
	-czf "$TARBALL" -C "$WORK" "$PKG-$UVERSION"

log "done"
sha256sum "$TARBALL"
