#!/usr/bin/env bash
# Repro shim for get-bb/bb#1758.
# Transparent wrapper around the real `gh`. While the flag file exists it
# forces every gh network call through a dead proxy (127.0.0.1:9), which is
# how we simulate "the network was down / gh could not reach api.github.com
# for a moment when bb started". Remove the flag and gh works normally again.
FLAG="/tmp/bb-reports/issues/1758/repro/gh-offline"
LOG="/tmp/bb-reports/issues/1758/repro/gh-calls.log"
REAL="/home/sawyer/.local/bin/gh"
SLOW="/tmp/bb-reports/issues/1758/repro/gh-slow"
mode=online
[ -e "$FLAG" ] && mode=OFFLINE
[ -e "$SLOW" ] && { mode=SLOW; sleep 12; }   # a starved host: gh takes >10 s (plugin's checkAuth timeout)
printf '%s pid=%s ppid=%s mode=%s args=%s\n' "$(date +%T)" "$$" "$PPID" "$mode" "$*" >> "$LOG"
if [ "$mode" = OFFLINE ]; then
  exec env HTTPS_PROXY=http://127.0.0.1:9 HTTP_PROXY=http://127.0.0.1:9 "$REAL" "$@"
fi
exec "$REAL" "$@"
