#!/bin/sh
set -e

CONFIG_FILE="${CONFIG_FILE:-/config/config.yaml}"
GENERATE_INTERVAL="${GENERATE_INTERVAL:-300}"  # seconds between regenerations; 0 = run once and exit

if [ ! -f "$CONFIG_FILE" ]; then
    echo "ERROR: config file not found at $CONFIG_FILE" >&2
    exit 1
fi

run_generate() {
    echo "[$(date -u +%FT%TZ)] Running gallery generate..."
    gallery --config "$CONFIG_FILE" generate --verbose
}

run_generate

if [ "$GENERATE_INTERVAL" -eq 0 ]; then
    exit 0
fi

while true; do
    sleep "$GENERATE_INTERVAL"
    run_generate
done
