#!/bin/sh
# Hands a repomon job's success back to the machine running release.fish --
# repomon has no artifact-return channel of its own (each job's workspace
# is deleted immediately on completion) and no API to poll job status, so
# this is a plain scp of an empty marker file to a staging path that
# dispatch-remote-builds.fish polls. There's no real build artifact to hand
# back (tracing-subscriber-init ships no binaries) -- the marker's mere
# existence *is* "this job's checks succeeded", since a step failing would
# stop the job before this one runs.
#
# Shared by the linux-rake/macos-rake jobs (.repomon/release.toml) and the
# audit job (.repomon/audit.toml) -- each passes its own marker name as
# $1, since the scp target and logic are identical and only the marker
# filename differs. Windows uses its own windows-handoff.ps1 (different
# shell/scp idiom, see that file).
#
# The marker is scp'd to a `.partial` suffix, then atomically mv'd into its
# final name via a script piped into `ssh ... sh -s`. A plain `scp foo
# "$dest"` creates the destination file and starts streaming into it
# immediately, so a `test -f`-based poll (rel_wait_for_files in lib.fish)
# could observe and act on a partially-written file if it landed mid-
# transfer -- the rename is a same-filesystem mv (atomic), so the poller
# never sees the final name until every byte has landed. Matches
# ruarango/sss's handoff.sh exactly.
#
# The rename script is piped to `sh -s` over stdin rather than passed as an
# `ssh host "..."` command string -- jozias@jasonozias.com's login shell is
# fish, and ssh hands a command-string argument straight to the login
# shell, so POSIX syntax like `set -e` would be misparsed as fish's own
# (incompatible) `set -e VARNAME`. `sh -s` sidesteps the login shell
# entirely.
set -eu

platform=$1
tag=$(git describe --tags --exact-match)
host="jozias@jasonozias.com"
dest_dir="/opt/releases/tracing-subscriber-init/staging/$tag"
marker="$platform.ok"

: > "$marker"
scp "$marker" "$host:$dest_dir/$marker.partial"

{
    echo "set -e"
    printf 'mv -- "%s/%s.partial" "%s/%s"\n' "$dest_dir" "$marker" "$dest_dir" "$marker"
} | ssh "$host" sh -s
