#!/bin/bash
set -euo pipefail

# cube-cycle.sh
# Cycles ArtCube programs forever. Designed to run under systemd.
#
# Notes:
# - LED_* flags belong to rpi-rgb-led-matrix (safe to share)
# - App-specific flags (like globe's -i map and -s spin speed) must NOT be shared

CUBE_DIR="/home/jerry/InfoCube"

# --- Common LED args (library flags) ---
# If you ever see the snd_bcm2835 warning, either disable Pi audio permanently
# or add: --led-no-hardware-pulse
LED_ARGS=(
  --led-rows=64
  --led-cols=64
  --led-chain=6
  --led-gpio-mapping=adafruit-hat
  --led-pwm-dither-bits=1
  --led-pwm-lsb-nanoseconds=130
  --led-pwm-bits=10
  --led-slowdown-gpio=6
  --led-brightness=75

)

# Optional: uncomment if you want to force the workaround always.
# LED_ARGS+=(--led-no-hardware-pulse)

# --- App-specific args ---
GLOBE_SPIN=60               # globe's "-s" argument (spin speed)
INFOCUBE_ARGS=()            # keep empty unless infocube needs its own args
MATRIXCUBE_ARGS=()          # keep empty unless matrixcube needs its own args
YAW=180			    #  --yaw= Argument
VERTEX=2		    # Globe Top Vertex
child_pid=""

cleanup_child() {
  if [[ -n "${child_pid}" ]] && kill -0 "${child_pid}" 2>/dev/null; then
    echo "$(date) Stopping child pid ${child_pid}"
    kill -INT "${child_pid}" 2>/dev/null || true
    sleep 1
    kill "${child_pid}" 2>/dev/null || true
    wait "${child_pid}" 2>/dev/null || true
  fi
  child_pid=""
}

on_term() {
  echo "$(date) cube-cycle received termination signal"
  cleanup_child
  exit 0
}
trap on_term INT TERM

run_for() {
  local name="$1"
  local duration="$2"
  shift 2

  echo "$(date) Starting ${name}"
  echo "CMD: $@" >&2
  cd "${CUBE_DIR}"

  "$@" &
  child_pid=$!

  local end=$((SECONDS + duration))
  while (( SECONDS < end )); do
    sleep 1
  done

  cleanup_child
  sleep 1
}

run_globe_for() {
  local label="$1"
  local duration="$2"
  local map="${3:-}"
  shift 3 || true

  if [[ -n "${map}" ]]; then
    run_for "${label}" "${duration}" ./bin/globe "${LED_ARGS[@]}" -i "${map}" -s "${GLOBE_SPIN}" -v "${VERTEX}"
  else
    # Earth: globe defaults to Earth map if -i is omitted
    run_for "${label}" "${duration}" ./bin/globe "${LED_ARGS[@]}" -s "${GLOBE_SPIN}"
  fi
}

# ---- Playlist ----
# Adjust durations to taste.
while true; do

  # Globe set (add/remove planets as you like)
  run_globe_for "Sun"      15  "maps/sun.jpg"
  run_globe_for "Mercury"  15  "maps/mercury.jpg"
  run_globe_for "Venus"    15  "maps/venus.jpg"
  run_globe_for "Earth"    15  "maps/earth.jpg"
  run_globe_for "Earth"    15  "maps/earth2.jpg"
  run_globe_for "Moon"     15  "maps/moon.jpg"
  run_globe_for "Mars"     15  "maps/mars.jpg"
  run_globe_for "Jupiter"  15  "maps/jupiter.jpg"
  run_globe_for "Neptune"  15  "maps/neptune.jpg"

  # SolarCube
  run_for "SolarCube" 30 ./bin/solarcube "${LED_ARGS[@]}" "${INFOCUBE_ARGS[@]}" --yaw="${YAW}"

  # GalaxyCube
  run_for "GalaxyCube" 30 ./bin/galaxycube "${LED_ARGS[@]}" "${INFOCUBE_ARGS[@]}" --yaw="${YAW}"

  # InfoCube
  run_for "InfoCube" 90 ./bin/infocube "${LED_ARGS[@]}" "${INFOCUBE_ARGS[@]}"

  # Matrix art
  run_for "Matrix"   30 ./bin/matrixcube "${LED_ARGS[@]}" "${MATRIXCUBE_ARGS[@]}"

  # Optional: quick face-ID check (usually not part of the art loop)
  run_for "FaceID"   5 ./bin/faceid "${LED_ARGS[@]}"

  # Optional: Banner Display
  run_for "BannerCube"   15 sudo ./bin/bannercube --led-rows=64 --led-cols=64 --led-chain=6 --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=6 --text "MusicMaster Rocks" --top-image images/jp_cube_premium_dual_top.jpg --bottom-image images/jp_cube_premium_dual_bottom_rot180.jpg --speed 15 --brightness-scale 0.7 --text-color 5,52,243 --bg-color 221,224,237


done
