#!/usr/bin/env bash
# txwake — acquire/release a wake lock so the device stays awake.
# Usage: txwake [lock|unlock|status] [--dry-run]
#   Wraps termux-wake-lock / termux-wake-unlock. Remembers the last
#   state it set in a state file so `status` works (the API has no
#   query command).
set -u
prog=txwake
STATE_DIR="${TXSTATE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/txstate}"
STATE_FILE="$STATE_DIR/wake"

usage() {
  cat <<'USAGE'
Usage: txwake [lock|unlock|status] [--dry-run]
  Manage the Android wake lock via termux-wake-lock/unlock.
  lock      acquire a wake lock (CPU stays awake, screen may sleep)
  unlock    release the wake lock
  status    show the last state txwake set (the API cannot query the
            lock, so this is the last-known state, labelled so)
  --dry-run print the termux-wake-* command, do not run it
  --selftest run built-in checks (dry-run + guard only)
  --help    show this help
  Default command is status. Requires termux-wake-lock/unlock
  (Termux:API); on plain Linux this prints a clear message and
  exits non-zero.
USAGE
}

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_wake() { # do_wake lock|unlock DRYRUN
  local want=$1 dryrun=$2 cmd
  [[ $want == lock ]] && cmd=termux-wake-lock || cmd=termux-wake-unlock
  if (( dryrun )); then printf '%s\n' "$cmd"; return 0; fi
  command -v "$cmd" >/dev/null 2>&1 || {
    printf '%s: termux-api not available (%s not found)\n' "$prog" "$cmd" >&2
    printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for an Android wake lock.\n' "$prog" >&2
    return 1; }
  "$cmd" && 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 lock 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == "termux-wake-lock" ]]; then echo "PASS: --dry-run lock"; else echo "FAIL: lock rc=$rc out=$out"; fails=1; fi
  out=$(TXSTATE_DIR="$tmp/st" "$self" --dry-run unlock 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == "termux-wake-unlock" ]]; then echo "PASS: --dry-run unlock"; else echo "FAIL: unlock rc=$rc out=$out"; fails=1; fi
  mkdir -p "$tmp/st"; printf 'lock' > "$tmp/st/wake"
  out=$(TXSTATE_DIR="$tmp/st" "$self" status 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"lock (last set by txwake"* ]]; 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" lock 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 "txwake selftest: all PASS" || echo "txwake 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;;
    lock|unlock|status) cmd=$1;;
    *) echo "$prog: unknown command: $1" >&2; usage >&2; exit 1;;
  esac
  shift
done
case $cmd in
  lock|unlock) do_wake "$cmd" "$dryrun";;
  status) printf 'wake lock: %s (last set by txwake; the API cannot query the lock)\n' "$(last_state)";;
esac
