================================================================================
OPENWRT ARGON THEME MANAGER (manage_argon.bat) - WINDOWS EDITION USER MANUAL
Automated GitHub Download, Local Package Picker, Single-Pipe SSH Deployment,
Theme Removal with Reboot, and SSH Host Key Management for OpenWrt (apk)
================================================================================

Author:         Richard Lloyd (www.RichardLloydusa.com)
License:        MIT License
Script File:    manage_argon.bat
Default Target: root@192.168.1.1 (configurable via ROUTER_IP and ROUTER_USER)

--------------------------------------------------------------------------------
1. ARCHIVE CONTENTS & DIRECTORY STRUCTURE
--------------------------------------------------------------------------------
Extracting Windows_Argon_Theme_Install_OpenWrt.zip produces the following layout:

Windows_Argon_Theme_Install_OpenWrt/
|-- manage_argon.bat
|-- luci-theme-argon-3.1.2-r1.apk
|-- luci-theme-argon-3.1.3-r1.apk
|-- luci-theme-argon-3.2.0-r1.apk
|-- windows_user_manual_manage_argon_bat.txt
`-- OpenWrt Argon Theme Manager (Windows Edition) Manual.pdf

Keep manage_argon.bat and all luci-theme-argon*.apk files together in the same
extracted folder so local folder scanning and GitHub downloads store packages
in one place.

--------------------------------------------------------------------------------
2. STARTUP BEHAVIOR & CONFIGURATION
--------------------------------------------------------------------------------
- Environment & Variables: Runs with `@echo off` and `setlocal EnableDelayedExpansion`
  so dynamic indexed array variables (`!APK_1!`, `!APK_2!`, etc.) and numeric
  checks expand reliably inside loops and `if` blocks.
- Defaults:
    ROUTER_IP=192.168.1.1
    ROUTER_USER=root
    SCRIPT_DIR=%~dp0
    GITHUB_API=https://api.github.com/repos/jerrykuku/luci-theme-argon/releases/latest
- Directory Resolution: Runs `cd /d "%SCRIPT_DIR%"`. If changing directories
  fails (`errorlevel 1`), it prints an error, pauses, and exits with code `1`.
- Interactive :MENU Loop: Clears the screen (`cls`), displays the banner with
  `%ROUTER_USER%@%ROUTER_IP%` and `%SCRIPT_DIR%`, presents options `[1]`
  through `[5]`, and loops back on invalid choices after `timeout /t 2 >nul`.

--------------------------------------------------------------------------------
3. ADDING THEMES TO THE INSTALLATION DIRECTORY
--------------------------------------------------------------------------------
You can populate `%SCRIPT_DIR%` with Argon theme packages in two ways:

A. Manual Addition to the Folder
   1. Extract Windows_Argon_Theme_Install_OpenWrt.zip to a standard folder
      (do not run directly inside a compressed ZIP preview window).
   2. Obtain a compatible OpenWrt `.apk` theme file matching
      `luci-theme-argon*.apk` (starts with `luci-theme-argon` and ends with
      `.apk`).
   3. Copy or move the `.apk` file into the same folder as `manage_argon.bat`
      (alongside the bundled `3.1.2-r1`, `3.1.3-r1`, and `3.2.0-r1` packages).
   4. Select Option `[2]` from the menu; `:LOCAL_INSTALL` dynamically scans
      `%SCRIPT_DIR%` every time it runs without needing script edits.

B. Automatic Download via Option [1]
   - Selecting Option `[1]` queries GitHub for the latest release asset matching
     `luci-theme-argon*.apk` and downloads it directly into
     `%SCRIPT_DIR%%APK_FILE%`, keeping a local copy in the directory for
     future local installs as well.

--------------------------------------------------------------------------------
4. COMPLETE ROUTINE & MENU REFERENCE
--------------------------------------------------------------------------------
[Option 1] :DOWNLOAD_AND_INSTALL
- Resets `DL_URL` and `APK_FILE`, then runs a PowerShell command
  (`powershell -NoProfile -Command ...`) calling `Invoke-RestMethod` on
  `%GITHUB_API%` with header `'User-Agent'='OpenWrt-Argon-Manager'`.
- Filters `$r.assets` where `$_.name -like 'luci-theme-argon*.apk'`, selects the
  first match, and outputs `<name>|<browser_download_url>`.
- If `DL_URL` is not defined, prints a GitHub lookup / connection message and
  Option `[2]` fallback hint, pauses, and returns to `:MENU`.
- Otherwise downloads into `"%SCRIPT_DIR%%APK_FILE%"` via
  `curl -fL --progress-bar "%DL_URL%"`. If `curl` fails (`errorlevel 1`),
  prints `"Download failed."`, pauses, and returns to `:MENU`; on success,
  jumps to `:RUN_INSTALL`.

[Option 2] :LOCAL_INSTALL
- Initializes `APK_COUNT=0` and loops over `"%SCRIPT_DIR%luci-theme-argon*.apk"`
  for existing files (`if exist "%%F"`), storing each filename (`%%~nxF`) in
  `APK_1`, `APK_2`, etc.
- Prints `"Scanning "%SCRIPT_DIR%" for luci-theme-argon*.apk... Found: %APK_COUNT%"`.
- If `%APK_COUNT% equ 0`, prints instructions to add an `.apk` or use
  Option `[1]`, pauses, and returns to `:MENU`.
- Otherwise lists `[1]` through `[%APK_COUNT%]` (`!APK_%%I!`) plus `[C] Cancel
  and return to menu` and prompts for `PICK`.
- Entering `C` or `c` (`/i "%PICK%"=="C"`) returns to `:MENU`.
- Evaluates `NUM_CHECK=PICK 2>nul` and verifies `!NUM_CHECK! geq 1` and
  `!NUM_CHECK! leq %APK_COUNT%` to set `APK_FILE`. If `APK_FILE` is not set,
  prints `"Invalid selection."`, pauses, and jumps back to `:LOCAL_INSTALL`.
- Otherwise jumps to `:RUN_INSTALL`.

[Shared Installer] :RUN_INSTALL
- Streams the selected `.apk` file over a single SSH session:
    tar -cf - "%APK_FILE%" | ssh %ROUTER_USER%@%ROUTER_IP% "tar -xf - -C /tmp
      && ls -la /tmp/luci-theme-argon*.apk && apk update && apk add
      luci-compat && apk add --allow-untrusted /tmp/luci-theme-argon*.apk
      && /etc/init.d/uhttpd restart && reboot"
- Prompts for your router `root` password once per install.
- Checks `if errorlevel 1`:
  * On error: prints `"SSH install command failed. Nothing was installed or
    rebooted."` plus a prompt to return to the menu and select option `[4]`
    `Clear SSH Host Key for %ROUTER_IP% (Fix Changed Host Warning)` if a
    `REMOTE HOST IDENTIFICATION HAS CHANGED` warning appears, pauses, and
    returns to `:MENU`.
  * On success: prints `"Install command sent. Your router is rebooting to
    apply the Argon theme."`, pauses, and returns to `:MENU`.

[Option 3] :REMOVE
- Connects over SSH once and executes:
    ssh %ROUTER_USER%@%ROUTER_IP% "apk del --no-cache luci-theme-argon;
      rm -f /tmp/luci-theme-argon*.apk; uci -q delete luci.themes.Argon;
      uci commit luci; rm -rf /tmp/luci-indexcache /tmp/luci-modulecache/;
      rm -f /etc/config/argon; /etc/init.d/uhttpd restart && reboot"
- Checks `if errorlevel 1`:
  * On error: prints `"SSH cleanup command returned an error."` plus the
    prompt to select option `[4]` `Clear SSH Host Key for %ROUTER_IP% (Fix
    Changed Host Warning)` on changed host warnings, pauses, and returns to
    `:MENU`.
  * On success: prints `"Done. Argon theme removed and router is rebooting."`,
    pauses, and returns to `:MENU`.

[Option 4] :CLEAR_HOST_KEY
- Runs `ssh-keygen -R %ROUTER_IP%` to remove stale host entries from the
  current Windows user's `known_hosts` file.
- If `errorlevel 1`, prints a failure message and returns to `:MENU`;
  otherwise confirms the host key is cleared and the next SSH connection will
  prompt to trust the new key once.

[Option 5] :END
- Runs `endlocal` and exits the batch script with `exit /b 0`.

--------------------------------------------------------------------------------
5. PREREQUISITES & TROUBLESHOOTING
--------------------------------------------------------------------------------
- Built-In Windows Tools: Requires OpenSSH Client (`ssh.exe`, `ssh-keygen.exe`),
  `tar.exe`, `curl.exe`, and PowerShell on Windows 10/11 (install OpenSSH
  Client under Settings > Apps > Optional features if `ssh` or `ssh-keygen`
  is missing).
- Router & Network: Target OpenWrt router must use `apk`, have SSH enabled at
  `%ROUTER_IP%` (default `192.168.1.1`), and have internet access during
  installs for `apk update` and `apk add luci-compat`.
- Pre-Removal Step: Switch LuCI to the Bootstrap theme first (System > System >
  Language and Style > Design > Bootstrap > Save & Apply) before running `[3]`.
- Changed Host Key Warning: If SSH reports `REMOTE HOST IDENTIFICATION HAS
  CHANGED`, both `:RUN_INSTALL` and `:REMOVE` catch `errorlevel 1` and prompt
  you to select Option `[4]` to clear the old key for `%ROUTER_IP%`.
================================================================================