#!/bin/bash
set -e

CUBE_DIR="/home/jerry/InfoCube"
DEFAULT_DURATION=120

# Common LED arguments
LED_ARGS="--led-rows=64 --led-cols=64 --led-chain=6 --led-gpio-mapping=adafruit-hat --led-slowdown-gpio=6 -s 60 --led-brightness=90"

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

  echo "$(date) Starting $name"

  cd "$CUBE_DIR"

  "$@" &
  local pid=$!

  sleep "$duration"

  echo "$(date) Stopping $name (pid $pid)"
  kill -INT "$pid" 2>/dev/null || true
  sleep 1
  kill "$pid" 2>/dev/null || true
  wait "$pid" 2>/dev/null || true

  sleep 1
}

while true; do

  run_for "Earth"   90  ./bin/globe $LED_ARGS
  run_for "Weather" 180 ./bin/infocube $LED_ARGS
  run_for "Jupiter" 90  ./bin/globe -i maps/jupiter.jpg $LED_ARGS
  run_for "Weather" 180 ./bin/infocube $LED_ARGS
  run_for "Moon"    90  ./bin/globe -i maps/moon.jpg $LED_ARGS
  run_for "Weather" 180 ./bin/infocube $LED_ARGS

  # Example future app:
  # run_for "MatrixText" 90 ./cube_text --mode scroll --speed 3 $LED_ARGS

done
