#!/bin/bash
set -e

# Manual deploy only — this device is not managed via hosts/piha/services.yaml.
# Run by hand on piha after wiring the board.
cd "$(dirname "$0")"

if [ ! -f secrets.yaml ]; then
  echo "ERROR: secrets.yaml missing. Copy secrets.yaml.example to secrets.yaml and fill in wifi_ssid/wifi_password/fallback_password." >&2
  exit 1
fi

USB=false
if [ "$1" = "--usb" ]; then
  USB=true
fi

if [ "$USB" = true ]; then
  if [ ! -e /dev/ttyUSB0 ]; then
    echo "ERROR: /dev/ttyUSB0 not found. Plug in the board via USB for the first flash." >&2
    exit 1
  fi
  echo ">>> First flash over USB (/dev/ttyUSB0)..."
  docker compose run --rm --device /dev/ttyUSB0 esphome run gree-ir-blaster.yaml --device /dev/ttyUSB0
else
  echo ">>> OTA flash over WiFi..."
  docker compose run --rm esphome run gree-ir-blaster.yaml
fi
