#!/usr/bin/env bash # Download official Open Watcom snapshot; no installer executed, local project cache. set -euo pipefail cd "$(dirname "$0")/.." for t in curl jq tar xz sha256sum; do command -v "$t" >/dev/null || { echo "Missing $t; run cross/install-tools.sh --apt-only" >&2; exit 1; }; done cache=${MARS_TOOLCHAIN_DIR:-$PWD/.toolchains}; mkdir -p "$cache"; cache=$(realpath "$cache") if [[ -f $cache/watcom/ROOT ]]; then root=$(cat "$cache/watcom/ROOT"); if [[ -x $cache/watcom/$root/binl64/wcc386 || -x $cache/watcom/$root/binl/wcc386 ]]; then echo "Using cached Open Watcom: $cache/watcom/$root"; exit 0; fi echo 'Incomplete cache; move the watcom directory aside first.' >&2; exit 1 fi [[ ! -e $cache/watcom ]] || { echo 'Incomplete Watcom cache; move it aside first.' >&2; exit 1; } work=$(mktemp -d "$cache/.watcom.XXXXXXXX"); trap 'rm -rf -- "$work"' EXIT fetch(){ curl --fail --show-error --silent --location --retry 3 --connect-timeout 15 --max-time 1800 --proto '=https' --proto-redir '=https' "$@"; } tag=${WATCOM_TAG:-Current-build} [[ $tag =~ ^[A-Za-z0-9._-]+$ ]] || exit 2 if [[ -n ${WATCOM_URL:-} ]]; then [[ ${WATCOM_SHA256:-} =~ ^[a-fA-F0-9]{64}$ ]] || { echo 'Custom WATCOM_URL requires WATCOM_SHA256.' >&2; exit 2; } url=$WATCOM_URL; digest=$WATCOM_SHA256 else fetch "https://api.github.com/repos/open-watcom/open-watcom-v2/releases/tags/$tag" -o "$work/release.json" asset=$(jq -ce '[.assets[] | select(.name=="owsnapshot.tar.xz" or .name=="ow-snapshot.tar.xz")][0] // error("No snapshot for this release")' "$work/release.json") url=$(jq -er '.browser_download_url' <<<"$asset") [[ $url == https://github.com/open-watcom/open-watcom-v2/releases/download/* ]] || exit 2 digest=${WATCOM_SHA256:-$(jq -r '(.digest // "") | sub("^sha256:"; "")' <<<"$asset")} fi fetch "$url" -o "$work/snapshot.tar.xz" actual=$(sha256sum "$work/snapshot.tar.xz" | cut -d' ' -f1) if [[ -n $digest ]]; then [[ ${digest,,} == "$actual" ]] || { echo 'Watcom SHA256 mismatch' >&2; exit 1; }; else echo 'WARNING: upstream supplied no digest. HTTPS transport only; recorded SHA256 is not independent verification.' >&2; fi mkdir "$work/extract" tar -xJf "$work/snapshot.tar.xz" --no-same-owner --no-same-permissions -C "$work/extract" root=''; for d in "$work/extract" "$work/extract"/*; do if [[ -d $d/h && ( -x $d/binl64/wcc386 || -x $d/binl/wcc386 ) ]]; then root=$d; break; fi done [[ -n $root ]] || { echo 'Open Watcom snapshot layout not recognized.' >&2; exit 1; } relative=${root#"$work/extract"}; relative=${relative#/}; printf '%s\n' "${relative:-.}" > "$work/extract/ROOT" printf 'url=%s\nsha256=%s\ntag=%s\n' "$url" "$actual" "$tag" > "$work/extract/ORIGIN.txt" mv -T "$work/extract" "$cache/watcom" echo "Open Watcom available in $cache/watcom/${relative:-.}"