#!/usr/bin/env bash
set -euo pipefail

instance="${1:?missing instance name}"
root_dir="{{RUNTIME_ROOT}}/channels"
channel_dir="${instance%_*}"
core_dir="${instance##*_}"
workdir="${root_dir}/${channel_dir}/${core_dir}"
binary="./${instance}"

if [ ! -d "$workdir" ]; then
  echo "Missing workdir for instance ${instance}: ${workdir}" >&2
  exit 1
fi

cd "$workdir"

if [ ! -x "$binary" ]; then
  echo "Missing executable for instance ${instance}: ${workdir}/${instance}" >&2
  exit 1
fi

exec "$binary"
