#!/bin/bash

set -ex

forgejo=$1
forgejo_pr=$2
runner=$3
runner_pr_or_ref=$4

#
# Get information from the runner
#
cd $runner
#
# code.forgejo.org/forgejo/runner/vN may be
# upgraded to code.forgejo.org/forgejo/runner/vN+1
#
module=$(cat go.mod | head -1 | cut -d' ' -f2)
test "$module"
sha=$(git -C $runner show --no-patch --format=%H)
test "$sha"

#
# Update Forgejo to use the runner at $runner_pr_or_ref
#
cd $forgejo
#
# Update the runner major version if needed
#
find * -name '*.go' -o -name 'go.mod' | xargs sed -i -E -e "s|code.forgejo.org/forgejo/runner/v[0-9]+|$module|"
#
# If it is a pull request, change the module to reference the forked repository so
# go mod tidy can find the SHA from a known branch or tag
#
if test -f "$runner_pr_or_ref"; then
  repository=$(jq --raw-output .head.repo.full_name <$runner_pr_or_ref)
  test "$repository"
  replacement_module=$(echo $module | sed -e "s|code.forgejo.org/forgejo/runner|code.forgejo.org/$repository|")
else
  replacement_module=$module
fi
#
# add a "replace code.forgejo.org/forgejo/runner/v?? $sha" line to the forgejo go.mod
# so that it uses the branch or pull request from which the cascade is run.
#
sed -i -e "\|replace $module|d" go.mod
echo "replace $module => $replacement_module $sha" >>go.mod
go mod tidy
