#!/bin/sh -

git="$HOME"/git/scrcpy
export SCRCPY_ICON_PATH="$HOME/etc/img/scrcpy.xpm"

std='-m1024 --window-x=138 --window-y=0'

g91=192.168.1.11
k50=192.168.1.14
s6lite=192.168.1.13

# android - convenience front-end for scrcpy Android device interface
# Steve Kinzler, steve@kinzler.com, Feb 2025
# https://kinzler.com/me/home.html#android

# See https://github.com/Genymobile/scrcpy
#     https://github.com/Genymobile/scrcpy/blob/master/doc/shortcuts.md
#     https://github.com/Genymobile/scrcpy/blob/master/doc/connection.md

lists=' --list-encoders --list-displays --list-cameras'
lists="$lists --list-camera-sizes --list-apps"

name=
case "$1" in
-n|--name)	shift; name="$1"; shift;;
esac
op="$1"; test $# -ge 1 && shift
name="${name:-@ $op}"
std="$std --window-title="
ip=`eval 'echo $'$op`

case "$op" in
-h|--help)	echo "usage: $0 [ -n win_name ] op [ scrcpy_arg ... ]" 1>&2
		$0 -L | sed 's/^/	/'      1>&2;		    exit 1;;
-L)		sed -n 's/#FUNCXX\ //p' `which $0` |
			sed 's/^#//; s/|/,/g; s/)	/	/; s/\\//g'
								    exit;;
-l)		$0 -L | sed 's/[ 	].*//' | tr , '\012';	    exit;;

###############################################################################

update*)		#FUNCXX update scrcpy installation
	case "$op" in *q) cd "$git" 2> /dev/null || exit;;
		      *)  cd "$git"		 || exit;; esac
	case "`git pull`" in
	Alrea*)	echo "$0: scrcpy is already up-to-date" 1>&2; exit;;
	*)	./install_release.sh;;
	esac;;

# Note: re-run this if cannot connect to :5555 - NOTE NEED TO BE ON USB
enable)		#FUNCXX configure tcpip for the device on USB
	/bin/echo -n 'Enable "Wireless Debugging" in the device settings:'
	read done
	exec scrcpy --select-usb --tcpip;;
dev*)		#FUNCXX list connected devices
	exec adb devices;;

usb)		#FUNCXX run scrcpy to the device on USB
	exec scrcpy $std"$name"		-d		  ${1+"$@"};;
#g91)		#FUNCXX run scrcpy to g91
#k50)		#FUNCXX run scrcpy to k50
#s6lite)		#FUNCXX run scrcpy to s6lite
g91|k50|s6lite)
	exec scrcpy $std"$name"		--tcpip="$ip"	  ${1+"$@"};;

usb\?)		#FUNCXX output info about the device on USB
	exec scrcpy $std"$name" $lists	-d		  ${1+"$@"};;
#g91\?)		#FUNCXX output info about g91
#k50\?)		#FUNCXX output info about k50
#s6lite\?)		#FUNCXX output info about s6lite
g91\?|k50\?|s6lite\?)
	exec scrcpy $std"$name" $lists	--tcpip="$ip"	  ${1+"$@"};;

*)	exec "$0" -h;;
esac
