# moonlight-android — task runner (https://github.com/casey/just)
#
# Wraps the Nix flake (devShell + hermetic nonRoot debug APK build). Two flake
# gotchas are baked in so `just <recipe>` just works:
#   * `?submodules=1` — the build needs the moonlight-common-c + enet submodule
#     sources, which Nix only includes when the flake is fetched with submodules.
#   * `--builders ""` — force a LOCAL build. Without it the Android SDK/NDK
#     closure (multi-GB) gets copied to the remote builder (eu.nixbuild.net),
#     which is far slower than building here where the SDK is already realised.

pkg := ".?submodules=1#moonlight-android"
build_flags := "--builders '' --print-build-logs"

# adb from the flake devShell (android-tools, pinned to the flake's nixpkgs).
# The adb server daemon persists between invocations, so `connect`/`pair` in
# one recipe line carries to the next.
adb := "nix develop '.?submodules=1' --command adb"

# list available recipes
default:
    @just --list

# enter the dev shell (JDK 17 + Android SDK 34 / NDK 27 + Gradle), then e.g. ./gradlew assembleNonRootDebug
dev:
    nix develop '.?submodules=1'

# build the nonRoot debug APK -> ./result/app-nonRoot-debug.apk
build:
    nix build '{{ pkg }}' {{ build_flags }}

# build and print the resulting APK path
apk: build
    @ls -l result/app-nonRoot-debug.apk

# install the built debug APK onto a USB-connected device/emulator (needs USB debugging)
install: build
    {{ adb }} install -r result/app-nonRoot-debug.apk

# install over WiFi to a Chromecast with Google TV (enable Developer options -> USB debugging first): just install-tv 192.168.1.x
install-tv ip: build
    {{ adb }} connect {{ ip }}:5555
    {{ adb }} -s {{ ip }}:5555 install -r result/app-nonRoot-debug.apk
    @echo "Installed. Launch Moonlight from the TV's app list (apps row / 'See all apps')."

# Wireless-debugging pair (Developer options -> Wireless debugging -> Pair with code shows HOST:PAIRPORT + code): just tv-pair 192.168.88.19:37123 123456
tv-pair host code:
    {{ adb }} pair {{ host }} {{ code }}

# connect after pairing, to the HOST:PORT on the main Wireless debugging screen (different port than pairing): just tv-connect 192.168.88.19:41234
tv-connect host:
    {{ adb }} connect {{ host }}

# install the built APK to an explicit adb target (e.g. the wireless-debugging host:port)
install-to host: build
    {{ adb }} -s {{ host }} install -r result/app-nonRoot-debug.apk

# launch Moonlight (Debug) on an Android TV target — uses the LEANBACK activity
# directly (monkey's LAUNCHER category no-ops on TV): just tv-launch 192.168.88.19:43079
tv-launch host:
    {{ adb }} -s {{ host }} shell am start -n com.limelight.debug/com.limelight.PcView

# disconnect a network adb device (e.g. the TV) when done
tv-disconnect ip:
    {{ adb }} disconnect {{ ip }}:5555

# regenerate the offline Maven lockfile (deps.json) after changing dependencies
deps:
    nix build '.?submodules=1#moonlight-android.mitmCache.updateScript'
    ./result

# evaluate the flake without building (catches eval errors fast)
check:
    nix flake check '.?submodules=1' --no-build

# remove the build result symlink
clean:
    rm -f result
