#!/usr/bin/env bash
set -euo pipefail
# Use matching-commit Metro and Windows backend. Pair URL files remain private.
session=$1
metro_origin=$2
pair_file=$3
environment_id=$4
original_thread_id=$5
separator_thread_id=$6
side=$7
output=$8
encoded_metro=$(python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.argv[1],safe=""))' "$metro_origin")
agent-device open com.t3tools.t3code.dev "t3code-dev://expo-development-client/?url=$encoded_metro&disableOnboarding=1&disableFab=1&disableAutoLaunch=1" --relaunch --session "$session"
# Wait for the app to finish loading, accepting a system deep-link prompt if presented.
agent-device wait text Chats --session "$session"
python3 pair-mobile.py "$session" "$pair_file"
# In Settings > Environments > Connection, rename the saved connection to "Windows proof".
# Dismiss the keyboard before Save. This removes the private host name from captures.
agent-device open com.t3tools.t3code.dev "t3code-dev://threads/$environment_id/$original_thread_id" --session "$session"
agent-device wait text 'Literal ampersand' --session "$session"
python3 mobile-capture.py "$session" "$side-original" "$output"
agent-device open com.t3tools.t3code.dev "t3code-dev://threads/$environment_id/$separator_thread_id" --session "$session"
agent-device wait text 'Markdown separator before ampersand' --session "$session"
python3 mobile-capture.py "$session" "$side-separators" "$output"
