#!/usr/bin/env sh
set -eu
case "$(uname -s)" in Linux|Darwin) ;; *) echo 'LocalCommand requires Linux or macOS.' >&2; exit 1 ;; esac
source_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
output=${1:-"$source_dir/../priv/bin/backplane-local-command-session"}
mkdir -p "$(dirname -- "$output")"
artifact=$(mktemp "${output}.tmp.XXXXXX")
trap 'rm -f "$artifact"' EXIT HUP INT TERM
link_flags=
if [ "$(uname -s)" = Darwin ]; then link_flags=-lproc; fi
"${CC:-cc}" -std=c11 -O2 -Wall -Wextra -Werror -Wformat=2 \
  "$source_dir/local_command_session.c" -o "$artifact" $link_flags
chmod 755 "$artifact"
"$artifact" --probe
mv -f "$artifact" "$output"
