#!/usr/bin/env bash set -euo pipefail action="${1:?action is required}" root="${STEM_REPO:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)}" profile="${STEM_PROFILE:-native-amd64}"; target="${STEM_TARGET:-native}"; selector="${STEM_TASK:-task1}" tasks=(task01_session_lifecycle task02_windows_and_panes task03_detach_attach_state) resolve_task() { local value="${1,,}" name; for name in "${tasks[@]}"; do [[ "$value" == "$name" ]] && { printf '%s\n' "$name"; return; }; done; [[ "$value" =~ ^(task|t)?[-_]?0*([1-3])($|[-_].*) ]] && { printf '%s\n' "${tasks[${BASH_REMATCH[2]} - 1]}"; return; }; printf 'Unknown task: %s\n' "$1" >&2; exit 2; } task="$(resolve_task "$selector")"; artifact_dir="$root/.stem/artifacts/$profile/$target/$task"; mkdir -p "$artifact_dir" case "$profile:$target:$action" in native-amd64:native:build) bash -n "$root"/src/tasks/*.sh "$root/tests/test_tasks.sh"; command -v tmux >/dev/null; printf 'BUILD tasks=3 tmux=%s\n' "$(tmux -V | tr ' ' '-')" ;; native-amd64:native:test|native-amd64:native:run) exec env CARD_ROOT="$root" "$root/tests/test_tasks.sh" "$task" ;; native-amd64:native:debug) trace_file="$artifact_dir/trace.log"; set +e; env CARD_ROOT="$root" bash -x "$root/tests/test_tasks.sh" "$task" >"$trace_file" 2>&1; status=$?; set -e; cat "$trace_file"; [[ $status -eq 0 && -s "$trace_file" ]]; printf 'TRACE %s log=%s\n' "$task" "$trace_file" ;; *) printf 'Unsupported profile/target/action: %s/%s/%s\n' "$profile" "$target" "$action" >&2; exit 2 ;; esac