#!/bin/sh
# patch given spec file to have the correct version and declare bundled NPM dependencies
# Usage: fix-spec <spec-file> [version]
set -eu

spec="$1"
version="${2:-}"

ROOT_DIR=$(realpath "$(dirname "$0")/..")

PROVIDES=$(awk '{print "Provides: bundled(npm(" $1 ")) = " $2}' "${ROOT_DIR}/runtime-npm-modules.txt")
awk -v p="$PROVIDES" 'gsub(/#NPM_PROVIDES/, p) 1' "$spec" > "$spec".new

if [ -n "$version" ]; then
    sed -i '/Version/ s/\b0\b/'"$version"'/' "$spec".new
fi

mv -f "$spec".new "$spec"
