#!/bin/sh
# Shell worker for the Whatnot Printing installer.
# Installs the self-contained (no-Python-required) print helper binary and its
# launchd agent. The GUI installer calls these subcommands and shows progress.
#
# The frozen helper ships inside the app at Contents/Resources/whatnot-print-helper/
# and is copied to ~/Library/Application Support/WhatnotPrintHelper/bin/.

CMD="$1"
SELFDIR="$(cd "$(dirname "$0")" && pwd)"
SUPPORT="$HOME/Library/Application Support/WhatnotPrintHelper"
BIN_DIR="$SUPPORT/bin"
HELPER_EXE="$BIN_DIR/whatnot-print-helper"
PLIST="$HOME/Library/LaunchAgents/com.whatnot.capture.printhelper.plist"
LOG="$HOME/Library/Logs/whatnot-print-helper.log"

list_printers() { lpstat -p 2>/dev/null | awk '/^printer / {print $2}'; }

case "$CMD" in
  list)
    list_printers
    ;;

  best)
    list_printers | grep -iE 'thermal|rollo|label' | head -1
    ;;

  files)
    PRINTER="$2"
    # Copy the self-contained helper (folder with its own Python runtime).
    mkdir -p "$BIN_DIR"
    rm -rf "$BIN_DIR"
    mkdir -p "$SUPPORT"
    cp -R "$SELFDIR/whatnot-print-helper" "$BIN_DIR"
    chmod +x "$HELPER_EXE" 2>/dev/null || true
    mkdir -p "$HOME/Library/LaunchAgents"
    cat > "$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>com.whatnot.capture.printhelper</string>
  <key>ProgramArguments</key>
  <array><string>$HELPER_EXE</string></array>
  <key>EnvironmentVariables</key>
  <dict>
    <key>PATH</key><string>/usr/bin:/bin:/usr/sbin:/sbin</string>
    <key>ROLLO_PRINTER</key><string>$PRINTER</string>
    <key>PRINT_MODE</key><string>pdf</string>
    <key>LABEL_PAGE_SIZE</key><string>2x1</string>
    <key>PRINT_DPI</key><string>203dpi</string>
  </dict>
  <key>RunAtLoad</key><true/>
  <key>KeepAlive</key><true/>
  <key>StandardOutPath</key><string>$LOG</string>
  <key>StandardErrorPath</key><string>$LOG</string>
</dict>
</plist>
EOF
    ;;

  start)
    launchctl bootout "gui/$(id -u)" "$PLIST" 2>/dev/null || true
    launchctl bootstrap "gui/$(id -u)" "$PLIST" 2>/dev/null || true
    launchctl enable "gui/$(id -u)/com.whatnot.capture.printhelper" 2>/dev/null || true
    launchctl kickstart -k "gui/$(id -u)/com.whatnot.capture.printhelper" 2>/dev/null || true
    ;;

  ping)
    # Single quick health probe: prints 1 if online, else 0.
    if curl -sf --max-time 1 http://127.0.0.1:3737/health >/dev/null 2>&1; then echo 1; else echo 0; fi
    ;;

  wait)
    i=0
    while [ "$i" -lt 40 ]; do
      if curl -sf --max-time 1 http://127.0.0.1:3737/health >/dev/null 2>&1; then echo 1; exit 0; fi
      i=$((i + 1))
      sleep 0.25
    done
    echo 0
    ;;

  configure)
    PRINTER="$2"
    curl -sf --max-time 5 -X POST http://127.0.0.1:3737/configure \
      -H "Content-Type: application/json" \
      -d "{\"printerName\":\"$PRINTER\"}" >/dev/null 2>&1 || true
    lpoptions -p "$PRINTER" -o PageSize=2x1 -o Resolution=203dpi -o roMediaTracking=Gap >/dev/null 2>&1 || true
    ;;
esac
