#!/bin/bash
# ============================================================================
# Wrapper for the email pipeline. Calls Python wrapper inside python-runner
# container. Logs to dated file. Cleans up logs older than 30 days.
#
# Install via User Scripts plugin or /etc/cron.d/email_pipeline:
#   0 6,18 * * * /mnt/user/Scripts/run_pipeline.sh
# ============================================================================

set -u

LOG_DIR="/mnt/user/Scripts/logs"
TIMESTAMP=$(date +%Y%m%d_%H%M)
LOG_FILE="${LOG_DIR}/pipeline_${TIMESTAMP}.log"
LATEST_LINK="${LOG_DIR}/pipeline_latest.log"
RETENTION_DAYS=30

mkdir -p "$LOG_DIR"

echo "=== Email pipeline run @ $(date '+%Y-%m-%d %H:%M:%S') ===" >> "$LOG_FILE"

# Make sure the container is running
if ! docker inspect -f '{{.State.Running}}' python-runner 2>/dev/null | grep -q true; then
    echo "ERROR: python-runner container is not running" >> "$LOG_FILE"
    docker start python-runner >> "$LOG_FILE" 2>&1 || exit 1
    sleep 5
fi

docker exec python-runner python /scripts/0_run_pipeline_v1.0.py --quiet >> "$LOG_FILE" 2>&1
RET=$?

echo "" >> "$LOG_FILE"
echo "=== Wrapper finished @ $(date '+%Y-%m-%d %H:%M:%S') exit=$RET ===" >> "$LOG_FILE"

# Update "latest" symlink for easy tailing
ln -sf "$LOG_FILE" "$LATEST_LINK"

# Cleanup logs older than RETENTION_DAYS
find "$LOG_DIR" -name 'pipeline_*.log' -type f -mtime +${RETENTION_DAYS} -delete

exit $RET
