#!/usr/bin/env bash
# txwallpaper — set the wallpaper from an image file.
# Usage: txwallpaper [-l|--lockscreen] [-h|--homescreen] IMAGE [--dry-run]
#   Wrapper around termux-wallpaper. Validates the file exists and looks
#   like an image before calling it, so a typo cannot silently fail.
set -u
prog=txwallpaper

usage() {
  cat <<'USAGE'
Usage: txwallpaper [-l|--lockscreen] [-h|--homescreen] IMAGE [--dry-run]
  Set the Android wallpaper from an image file via termux-wallpaper.
  -l, --lockscreen   set the lock-screen wallpaper only
  -h, --homescreen   set the home-screen wallpaper only
  (default with neither flag: set both)
  --dry-run          print the termux-wallpaper command, do not set it
  --selftest         run built-in checks (dry-run + validation + guard)
  --help             show this help
  IMAGE must exist and be a jpg, png, webp, gif or bmp file.
  Requires termux-wallpaper (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 '%s' 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAwMCAO+ip1sAAAAASUVORK5CYII=' | base64 -d > "$tmp/pic.png"
  printf 'not an image' > "$tmp/notes.txt"
  out=$("$self" --dry-run "$tmp/pic.png" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"termux-wallpaper"* && $out == *"$tmp/pic.png"* ]]; then echo "PASS: --dry-run with a png"; else echo "FAIL: dry-run rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run -l "$tmp/pic.png" 2>&1); rc=$?
  if (( rc == 0 )) && [[ $out == *"-l"* ]]; then echo "PASS: --dry-run lockscreen flag"; else echo "FAIL: lockscreen rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run "$tmp/notes.txt" 2>&1); rc=$?
  if (( rc != 0 )) && [[ $out == *"not an image"* ]]; then echo "PASS: non-image file rejected"; else echo "FAIL: txt should fail rc=$rc out=$out"; fails=1; fi
  out=$("$self" --dry-run "$tmp/missing.jpg" 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 image argument rejected"; else echo "FAIL: no-arg should fail"; fails=1; fi
  out=$(PATH="/usr/bin:/bin" "$self" "$tmp/pic.png" 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 "txwallpaper selftest: all PASS" || echo "txwallpaper selftest: $fails FAILURES"
  return $fails
}

lock=0 home=0 dryrun=0 img=""
while (( $# )); do
  case $1 in
    --help|-h) usage; exit 0;;
    --selftest) selftest; exit $?;;
    --dry-run) dryrun=1;;
    -l|--lockscreen) lock=1;;
    -h|--homescreen) home=1;;
    -*) echo "$prog: unknown option: $1" >&2; usage >&2; exit 1;;
    *) [[ -z $img ]] && img=$1 || { echo "$prog: only one image at a time" >&2; exit 1; };;
  esac
  shift
done
[[ -n $img ]] || { echo "$prog: need an image file" >&2; usage >&2; exit 1; }
[[ -f $img ]] || { echo "$prog: no such file: $img" >&2; exit 1; }
case ${img##*.} in
  jpg|jpeg|png|webp|gif|bmp|JPG|JPEG|PNG|WEBP|GIF|BMP) ;;
  *) echo "$prog: not an image file (need jpg/png/webp/gif/bmp): $img" >&2; exit 1;;
esac
if command -v file >/dev/null 2>&1; then
  mime=$(file -b --mime-type "$img" 2>/dev/null)
  case $mime in image/*) ;; *) echo "$prog: '$img' does not look like an image (mime: ${mime:-unknown})" >&2; exit 1;; esac
fi
args=()
(( lock )) && args+=(-l)
(( home )) && args+=(-h)
args+=("$img")
if (( dryrun )); then printf 'termux-wallpaper'; printf ' %q' "${args[@]}"; printf '\n'; exit 0; fi
command -v termux-wallpaper >/dev/null 2>&1 || {
  printf '%s: termux-api not available (termux-wallpaper not found)\n' "$prog" >&2
  printf '%s: run this on Android with Termux:API installed. There is no Linux equivalent for setting a phone wallpaper.\n' "$prog" >&2
  exit 1; }
termux-wallpaper "${args[@]}"
echo "wallpaper set from $img"
