Files

97 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# txlocation — current location with a timeout, plus a maps link.
# Usage: txlocation [--provider gps|network|passive] [--timeout SEC] [--json]
# Wraps termux-location -r once and prints latitude/longitude,
# accuracy, altitude, speed, bearing and a Google Maps link.
set -u
prog=txlocation
usage() {
cat <<'USAGE'
Usage: txlocation [--provider gps|network|passive] [--timeout SEC] [--json]
Print the current location via termux-location.
--provider P gps|network|passive (default: gps)
--timeout N give up after N seconds waiting for a fix (default 30)
--json print the raw termux-location JSON
--selftest run built-in checks (fake termux-api shim + guard)
--help show this help
Requires termux-location (Termux:API) and location permission; on
plain Linux this prints a clear message and exits non-zero.
USAGE
}
render_json() {
if command -v python3 >/dev/null 2>&1; then
python3 -c '
import json, sys
d = json.load(sys.stdin)
lat = d.get("latitude"); lon = d.get("longitude")
acc = d.get("accuracy"); alt = d.get("altitude")
speed = d.get("speed"); bearing = d.get("bearing")
prov = d.get("provider", "?")
print("Location: %s, %s (provider: %s)" % (lat, lon, prov))
if acc is not None:
print(" accuracy: %s m" % acc)
if alt is not None:
print(" altitude: %s m" % alt)
if speed is not None:
print(" speed: %s m/s" % speed)
if bearing is not None:
print(" bearing: %s°" % bearing)
if lat is not None and lon is not None:
print(" map: https://maps.google.com/?q=%s,%s" % (lat, lon))
'
else
cat
echo "note: install python3 for a human-readable summary" >&2
fi
}
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' 'printf "%s" "{\"latitude\":37.7749,\"longitude\":-122.4194,\"altitude\":12.5,\"accuracy\":8.0,\"bearing\":90.0,\"speed\":1.2,\"provider\":\"gps\",\"elapsedMs\":1234}"' > "$tmp/bin/termux-location"
chmod +x "$tmp/bin/termux-location"
out=$(PATH="$tmp/bin:$PATH" "$self" 2>&1); rc=$?
if (( rc == 0 )) && [[ $out == *"37.7749"* && $out == *"-122.4194"* && $out == *"maps.google.com/?q=37.7749,-122.4194"* && $out == *"accuracy: 8.0 m"* ]]; then echo "PASS: termux path renders location + maps link"; else echo "FAIL: termux path rc=$rc out=$out"; fails=1; fi
out=$(PATH="$tmp/bin:$PATH" "$self" --json 2>&1)
if [[ $out == *'"latitude":37.7749'* ]]; then echo "PASS: --json passthrough"; else echo "FAIL: --json out=$out"; fails=1; fi
out=$(PATH="$tmp/bin:$PATH" "$self" --provider bogus 2>&1); rc=$?
if (( rc != 0 )); then echo "PASS: bad provider rejected"; else echo "FAIL: bad provider should fail"; fails=1; fi
out=$(PATH="/usr/bin:/bin" "$self" 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 "txlocation selftest: all PASS" || echo "txlocation selftest: $fails FAILURES"
return $fails
}
provider=gps timeout_s=30 asjson=0
while (( $# )); do
case $1 in
--help|-h) usage; exit 0;;
--selftest) selftest; exit $?;;
--json) asjson=1;;
--provider) shift; provider=${1:?--provider needs a value};;
--timeout) shift; timeout_s=${1:?--timeout needs seconds};;
*) echo "$prog: unknown option: $1" >&2; usage >&2; exit 1;;
esac
shift
done
case $provider in gps|network|passive) ;; *) echo "$prog: --provider must be gps|network|passive, got '$provider'" >&2; exit 1;; esac
[[ $timeout_s =~ ^[0-9]+$ ]] || { echo "$prog: --timeout must be a number, got '$timeout_s'" >&2; exit 1; }
command -v termux-location >/dev/null 2>&1 || {
printf '%s: termux-api not available (termux-location not found)\n' "$prog" >&2
printf '%s: run this on Android with Termux:API installed and location permission granted.\n' "$prog" >&2
exit 1; }
if command -v timeout >/dev/null 2>&1; then
raw=$(timeout "$timeout_s" termux-location -p "$provider" -r once) || {
echo "$prog: no location fix within ${timeout_s}s (provider: $provider) — try --provider network or move somewhere with a clearer sky view" >&2
exit 1; }
else
raw=$(termux-location -p "$provider" -r once) || { echo "$prog: termux-location failed" >&2; exit 1; }
fi
if (( asjson )); then printf '%s\n' "$raw"; else printf '%s\n' "$raw" | render_json; fi