#!/usr/bin/env bash
# txtorch — torch (flashlight) on/off/toggle/status.
# Usage: txtorch [on|off|toggle|status] [--dry-run]
#   Wrapper around termux-torch. Remembers the last state it set in a
#   small state file so `toggle` and `status` work (termux-torch itself
#   has no query command).
set -u
prog=txtorch
STATE_DIR="${TXSTATE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/txstate}"
STATE_FILE="$STATE_DIR/torch"

usage() {
  cat <<'USAGE'
Usage: txtorch [on|off|toggle|status] [--dry-run]
  Control the camera flashlight via termux-torch.
  on        turn the torch on
  off       turn the torch off
  toggle    switch to the opposite of the last state txtorch set
  status    show the last state txtorch set (termux-torch cannot query
            the hardware, so this is the last-known state, labelled so)
  --dry-run print the termux-torch command, do not run it
  --selftest run built-in checks (dry-run + guard only)
  --help    show this help
  Default command is status. Requires termux-torch (Termux:API); on
  plain Linux this prints a clear message and exits non-zero.
USAGE
}

api_missing() {
  printf '%s: termux-api not available (termux-torch not found)\n' "$prog" >&2
  printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for a phone flashlight.\n' "$prog" >&2
  return 1
}

last_state() { [[ -r $STATE_FILE ]] && cat "$STATE_FILE" || printf 'unknown'; }
save_state() { mkdir -p "$STATE_DIR" 2>/dev/null; printf '%s' "$1" > "$STATE_FILE" 2>/dev/null || true; }

do_torch() { # do_torch on|off DRYRUN
  local want=$1 dryrun=$2
  if (( dryrun )); then printf 'termux-torch %s\n' "$want"; return 0; fi
  command -v termux-torch >/dev/null 2>&1 || api_missing
  termux-torch "$want" && save_state "$want"
}

selftest() {
  local tmp fails=0 out rc
  tmp=$(mktemp -d); trap "rm -rf '$tmp'" EXIT
  local self="${BASH_SOURCE[0]}"
  out=$(TXSTATE_DIR="$tmp/st" "$self" --dry-run on 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == "termux-torch on" ]]; then echo "PASS: --dry-run on"; else echo "FAIL: dry-run on rc=$rc out=$out"; fails=1; fi
  out=$(TXSTATE_DIR="$tmp/st" "$self" --dry-run toggle 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == "termux-torch on" ]]; then echo "PASS: toggle from unknown defaults to on"; else echo "FAIL: toggle rc=$rc out=$out"; fails=1; fi
  printf 'on' > "$tmp/state"; mkdir -p "$tmp/st"; printf 'on' > "$tmp/st/torch"
  out=$(TXSTATE_DIR="$tmp/st" "$self" status 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"on (last set by txtorch"* ]]; then echo "PASS: status reports last-known state"; else echo "FAIL: status rc=$rc out=$out"; fails=1; fi
  out=$(TXSTATE_DIR="$tmp/st" "$self" bogus 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: bad command rejected"; else echo "FAIL: bad command should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" TXSTATE_DIR="$tmp/st" "$self" on 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"termux-api not available"* ]]; then echo "PASS: graceful degradation message"; else echo "FAIL: guard rc=$rc out=$out"; fails=1; fi
  out=$("$self" --help 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"Usage:"* ]]; then echo "PASS: --help"; else echo "FAIL: --help rc=$rc"; fails=1; fi
  (( fails == 0 )) && echo "txtorch selftest: all PASS" || echo "txtorch selftest: $fails FAILURES"
  return $fails
}

cmd=status dryrun=0
while (( $# )); do
  case $1 in
    --help|-h) usage; exit 0;;
    --selftest) selftest; exit $?;;
    --dry-run) dryrun=1;;
    on|off|toggle|status) cmd=$1;;
    *) echo "$prog: unknown command: $1" >&2; usage >&2; exit 1;;
  esac
  shift
done
case $cmd in
  on|off) do_torch "$cmd" "$dryrun";;
  toggle)
    if [[ $(last_state) == on ]]; then do_torch off "$dryrun"; else do_torch on "$dryrun"; fi;;
  status)
    printf 'torch: %s (last set by txtorch; termux-torch cannot query the hardware)\n' "$(last_state)";;
esac
