#!/bin/bash
# SUDANNA v23 — Cron Setup
# Run as: bash cron/cron_setup.sh /path/to/s26

APP_PATH="${1:-/home/USER/public_html/s26}"
PHP_BIN="${PHP_BIN:-php}"
LOG_DIR="$APP_PATH/storage/logs"

mkdir -p "$LOG_DIR"

# Current crontab
CURRENT=$(crontab -l 2>/dev/null || echo "")

# Lines to add
RESERV="*/5 * * * * $PHP_BIN $APP_PATH/cron/expire_reservations.php >> $LOG_DIR/cron.log 2>&1"
BACKUP="0 2 * * * $PHP_BIN $APP_PATH/cron/backup_db.php >> $LOG_DIR/backup.log 2>&1"

UPDATED="$CURRENT"
echo "$CURRENT" | grep -q "expire_reservations" || UPDATED="$UPDATED"$'\n'"$RESERV"
echo "$CURRENT" | grep -q "backup_db"           || UPDATED="$UPDATED"$'\n'"$BACKUP"

echo "$UPDATED" | crontab -

echo ""
echo "✅ Cron jobs installed:"
echo "   expire_reservations: every 5 minutes"
echo "   backup_db: daily 02:00"
echo ""
echo "Verify: crontab -l"
crontab -l | grep -E "expire_reservations|backup_db"
