#!/usr/bin/env bash
# txclip — clipboard get/set with a clear interface.
# Usage: txclip [get] | txclip set TEXT... | txclip set --stdin | txclip --clear
#   On Android uses termux-clipboard-get/set. On plain Linux falls back
#   to wl-paste/wl-copy (Wayland), xclip or xsel (X11). If none of those
#   exist it says so plainly instead of crashing.
set -u
prog=txclip

usage() {
  cat <<'USAGE'
Usage: txclip [get] | txclip set TEXT... | txclip set --stdin | txclip --clear
  Clipboard get/set.
  get           print the clipboard contents (default when no command given)
  set TEXT...   put TEXT on the clipboard (words joined with spaces)
  set --stdin   read the clipboard contents from stdin
  --clear       empty the clipboard
  --selftest    run built-in checks (fake termux-api shims)
  --help        show this help
  Android: termux-clipboard-get / termux-clipboard-set.
  Linux fallback: wl-paste/wl-copy, xclip, or xsel, in that order.
USAGE
}

# pick backend: prints "termux", "wl", "xclip", "xsel" or fails
pick_backend() {
  if command -v termux-clipboard-get >/dev/null 2>&1 && command -v termux-clipboard-set >/dev/null 2>&1; then printf 'termux'; return 0; fi
  if command -v wl-paste >/dev/null 2>&1 && command -v wl-copy >/dev/null 2>&1; then printf 'wl'; return 0; fi
  command -v xclip >/dev/null 2>&1 && { printf 'xclip'; return 0; }
  command -v xsel >/dev/null 2>&1 && { printf 'xsel'; return 0; }
  return 1
}

clip_get() {
  local be; be=$(pick_backend) || {
    printf '%s: no clipboard available\n' "$prog" >&2
    printf '%s: termux-api not available (termux-clipboard-get not found), and no Linux clipboard tool found (wl-paste, xclip, xsel).\n' "$prog" >&2
    printf '%s: on Android install Termux:API; on Linux install wl-clipboard or xclip.\n' "$prog" >&2
    return 1; }
  case $be in
    termux) termux-clipboard-get;;
    wl) wl-paste;;
    xclip) xclip -o -selection clipboard;;
    xsel) xsel --clipboard --output;;
  esac
}

clip_set() { # clip_set TEXT
  local be text=$1; be=$(pick_backend) || {
    printf '%s: no clipboard available\n' "$prog" >&2
    printf '%s: termux-api not available (termux-clipboard-set not found), and no Linux clipboard tool found (wl-copy, xclip, xsel).\n' "$prog" >&2
    return 1; }
  case $be in
    termux) printf '%s' "$text" | termux-clipboard-set;;
    wl) printf '%s' "$text" | wl-copy;;
    xclip) printf '%s' "$text" | xclip -i -selection clipboard;;
    xsel) printf '%s' "$text" | xsel --clipboard --input;;
  esac
}

selftest() {
  local tmp fails=0 out rc
  tmp=$(mktemp -d); trap "rm -rf '$tmp'" EXIT
  local self="${BASH_SOURCE[0]}"
  mkdir -p "$tmp/bin"
  printf '%s\n' '#!/usr/bin/env bash' 'cat "$TXCLIP_STORE"' > "$tmp/bin/termux-clipboard-get"
  printf '%s\n' '#!/usr/bin/env bash' 'cat > "$TXCLIP_STORE"' > "$tmp/bin/termux-clipboard-set"
  chmod +x "$tmp/bin/termux-clipboard-get" "$tmp/bin/termux-clipboard-set"
  : > "$tmp/store"
  out=$(TXCLIP_STORE="$tmp/store" PATH="$tmp/bin:/usr/bin:/bin" "$self" set "hello world" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $(cat "$tmp/store") == "hello world" ]]; then echo "PASS: set stores text"; else echo "FAIL: set rc=$rc out=$out store=$(cat "$tmp/store")"; fails=1; fi
  out=$(TXCLIP_STORE="$tmp/store" PATH="$tmp/bin:/usr/bin:/bin" "$self" get 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == "hello world" ]]; then echo "PASS: get round-trip"; else echo "FAIL: get rc=$rc out=$out"; fails=1; fi
  out=$(printf 'from stdin' | TXCLIP_STORE="$tmp/store" PATH="$tmp/bin:/usr/bin:/bin" "$self" set --stdin 2>&1); rc=$?
  if (( rc == 0 )) && [[ $(cat "$tmp/store") == "from stdin" ]]; then echo "PASS: set --stdin"; else echo "FAIL: set --stdin rc=$rc store=$(cat "$tmp/store")"; fails=1; fi
  out=$(TXCLIP_STORE="$tmp/store" PATH="$tmp/bin:/usr/bin:/bin" "$self" set 2>&1); rc=$?
  if (( rc != 0 )); then echo "PASS: set with no text rejected"; else echo "FAIL: bare set should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" "$self" get 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 "txclip selftest: all PASS" || echo "txclip selftest: $fails FAILURES"
  return $fails
}

case ${1:-get} in
  --help|-h) usage; exit 0;;
  --selftest) selftest; exit $?;;
  get) clip_get;;
  set)
    shift
    if [[ ${1:-} == --stdin ]]; then text=$(cat); clip_set "$text"
    elif (( $# == 0 )); then echo "$prog: 'set' needs text (or --stdin)" >&2; usage >&2; exit 1
    else clip_set "$*"; fi;;
  --clear) clip_set "";;
  *) echo "$prog: unknown command: $1" >&2; usage >&2; exit 1;;
esac
