#!/usr/bin/env bash
# txshare — share a file via the Android share sheet.
# Usage: txshare [-a ACTION] [-t TITLE] [-c TYPE] [--dry-run] FILE...
#   Wrapper around termux-share. Validates every file exists first so
#   a typo does not open the share sheet with nothing to share.
set -u
prog=txshare

usage() {
  cat <<'USAGE'
Usage: txshare [-a ACTION] [-t TITLE] [-c TYPE] [--dry-run] FILE...
  Share file(s) via the Android share sheet (termux-share).
  -a ACTION     share action: send (default), view, edit
  -t TITLE      title shown in the share sheet
  -c TYPE       content type, e.g. text/plain, image/png
  --dry-run     print the termux-share command, do not share
  --selftest    run built-in checks (dry-run + validation + guard)
  --help        show this help
  Requires termux-share (Termux:API). No Linux equivalent exists, so
  on plain Linux this prints a clear message and exits non-zero.
USAGE
}

selftest() {
  local tmp fails=0 out rc
  tmp=$(mktemp -d); trap "rm -rf '$tmp'" EXIT
  local self="${BASH_SOURCE[0]}"
  printf 'hello' > "$tmp/a.txt"; printf 'world' > "$tmp/b.txt"
  out=$("$self" --dry-run -t "My files" "$tmp/a.txt" "$tmp/b.txt" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-share"* && $out == *"$tmp/a.txt"* && $out == *"$tmp/b.txt"* && $out == *"-t My\\ files"* ]]; then echo "PASS: --dry-run with two files"; else echo "FAIL: dry-run rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run -a bogus "$tmp/a.txt" 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: bad action rejected"; else echo "FAIL: bad action should fail"; fails=1; fi
  out=$("$self" --dry-run "$tmp/missing.txt" 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"no such file"* ]]; then echo "PASS: missing file rejected"; else echo "FAIL: missing rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: no file rejected"; else echo "FAIL: no-file should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" "$self" "$tmp/a.txt" 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 "txshare selftest: all PASS" || echo "txshare selftest: $fails FAILURES"
  return $fails
}

action=send title="" ctype="" dryrun=0 files=()
while (( $# )); do
  case $1 in
    --help|-h) usage; exit 0;;
    --selftest) selftest; exit $?;;
    --dry-run) dryrun=1;;
    -a) shift; action=${1:?-a needs a value};;
    -t|--title) shift; title=${1:?--title needs a value};;
    -c) shift; ctype=${1:?-c needs a value};;
    -*) echo "$prog: unknown option: $1" >&2; usage >&2; exit 1;;
    *) files+=("$1");;
  esac
  shift
done
case $action in send|view|edit) ;; *) echo "$prog: -a must be send|view|edit, got '$action'" >&2; exit 1;; esac
(( ${#files[@]} )) || { echo "$prog: need at least one file to share" >&2; usage >&2; exit 1; }
for f in "${files[@]}"; do [[ -f $f ]] || { echo "$prog: no such file: $f" >&2; exit 1; }; done
args=(-a "$action")
[[ -n $title ]] && args+=(-t "$title")
[[ -n $ctype ]] && args+=(-c "$ctype")
args+=("${files[@]}")
if (( dryrun )); then printf 'termux-share'; printf ' %q' "${args[@]}"; printf '\n'; exit 0; fi
command -v termux-share >/dev/null 2>&1 || {
  printf '%s: termux-api not available (termux-share not found)\n' "$prog" >&2
  printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for the Android share sheet.\n' "$prog" >&2
  exit 1; }
termux-share "${args[@]}"
