#!/bin/bash
# Connect script for e2e tests. Invoked by mng's connect_command mechanism.
#
# Starts asciinema in headless mode to record the tmux session, writes the
# recording PID to a file, and exits so the mng create process can return
# to the test runner.
#
# Environment (set by mng's connect_command machinery):
#   MNG_AGENT_NAME    - name of the agent to connect to
#   MNG_SESSION_NAME  - full tmux session name (with prefix)
#
# Environment (set by e2e test fixture):
#   MNG_TEST_ASCIINEMA_DIR - directory for .cast and .pid output files

set -euo pipefail

cast_path="$MNG_TEST_ASCIINEMA_DIR/$MNG_AGENT_NAME.cast"
pid_path="$MNG_TEST_ASCIINEMA_DIR/$MNG_AGENT_NAME.pid"

asciinema rec --headless --command "mng conn $MNG_AGENT_NAME" "$cast_path" </dev/null >/dev/null 2>&1 &
echo $! > "$pid_path"
