#!/bin/sh
# Fetch the .proto sources of the protobuf modules generated by upstream into
# debian/missing-sources, see README.Debian.
#
# Upstream's code generation scripts do not keep the .proto files in the
# repository, they fetch them from other projects at generation time:
#
#   scripts/opamp_proto_codegen.sh
#     open-telemetry/opamp-spec at $OPAMP_SPEC_REPO_BRANCH_OR_COMMIT
#   exporter/opentelemetry-exporter-prometheus-remote-write/proto/generate-proto-py.sh
#     prometheus/prometheus (prompb/) at $PROM_VERSION
#     gogo/protobuf (gogoproto/gogo.proto) at $PROTO_VERSION
#
# This script reads those pinned versions from the upstream scripts and
# downloads the same .proto files. Run it from the top of the source tree
# after importing a new upstream version, and commit the result.

set -eu

OPAMP_SCRIPT=scripts/opamp_proto_codegen.sh
PROM_SCRIPT=exporter/opentelemetry-exporter-prometheus-remote-write/proto/generate-proto-py.sh
DEST=debian/missing-sources

if [ ! -f "$OPAMP_SCRIPT" ] || [ ! -f "$PROM_SCRIPT" ]; then
    echo "Run this from the top of the source tree." >&2
    exit 1
fi

# Get VAR=value (optionally quoted) from an upstream shell script.
# $1 is the script to search.
# $2 is the var name to search for.
# return is the var value.
# Example: scripts/opamp_proto_codegen.sh contains
# OPAMP_SPEC_REPO_BRANCH_OR_COMMIT="v0.12.0"
# get_var "$OPAMP_SCRIPT" OPAMP_SPEC_REPO_BRANCH_OR_COMMIT returns v0.12.0
get_var() {
    sed -n -e "s/^$2=[\"']\{0,1\}\([^\"']*\)[\"']\{0,1\}\$/\1/p" "$1" | head -n 1
}

OPAMP_VERSION=$(get_var "$OPAMP_SCRIPT" OPAMP_SPEC_REPO_BRANCH_OR_COMMIT)
PROM_VERSION=$(get_var "$PROM_SCRIPT" PROM_VERSION)
GOGO_VERSION=$(get_var "$PROM_SCRIPT" PROTO_VERSION)

for v in OPAMP_VERSION PROM_VERSION GOGO_VERSION; do
    eval "val=\$$v"
    if [ -z "$val" ]; then
        echo "Could not find $v in the upstream scripts, did they change?" >&2
        exit 1
    fi
done

echo "opamp-spec:     $OPAMP_VERSION"
echo "prometheus:     $PROM_VERSION"
echo "gogo/protobuf:  $GOGO_VERSION"

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

fetch() {
    # fetch <github owner/repo> <ref> <path in repo>
    mkdir -p "$TMP/new/$(dirname "$1/$3")"
    echo "  $1/$3"
    curl --fail --silent --show-error --location \
        --output "$TMP/new/$1/$3" \
        "https://raw.githubusercontent.com/$1/$2/$3"
}

echo "Downloading:"

# Upstream compiles every .proto file of opamp-spec, so take them all. The
# repository is small, get its tarball and list them from there.
echo "  open-telemetry/opamp-spec (tarball)"
mkdir -p "$TMP/opamp" "$TMP/new/open-telemetry/opamp-spec"
curl --fail --silent --show-error --location \
    "https://github.com/open-telemetry/opamp-spec/archive/$OPAMP_VERSION.tar.gz" \
    | tar -xz -C "$TMP/opamp" --strip-components=1
OPAMP_PROTOS=$(cd "$TMP/opamp" && find . -name '*.proto' | sed 's#^\./##' | sort)
if [ -z "$OPAMP_PROTOS" ]; then
    echo "No .proto files found in opamp-spec $OPAMP_VERSION." >&2
    exit 1
fi
for f in $OPAMP_PROTOS LICENSE; do
    echo "  open-telemetry/opamp-spec/$f"
    mkdir -p "$TMP/new/open-telemetry/opamp-spec/$(dirname "$f")"
    cp "$TMP/opamp/$f" "$TMP/new/open-telemetry/opamp-spec/$f"
done
for f in prompb/remote.proto prompb/types.proto LICENSE; do
    fetch prometheus/prometheus "$PROM_VERSION" "$f"
done
for f in gogoproto/gogo.proto LICENSE; do
    fetch gogo/protobuf "$GOGO_VERSION" "$f"
done

# Keep README.Debian-style notes in $DEST/README, replace everything else.
for d in open-telemetry prometheus gogo; do
    rm -rf "${DEST:?}/$d"
done
mkdir -p "$DEST"
cp -r "$TMP/new/." "$DEST/"

cat > "$DEST/VERSIONS" <<EOF
# Generated by debian/update-missing-sources, do not edit.
open-telemetry/opamp-spec $OPAMP_VERSION
prometheus/prometheus $PROM_VERSION
gogo/protobuf $GOGO_VERSION
EOF

echo "Updated $DEST, review with: git status $DEST"
