#!/usr/bin/env bash
# =============================================================================
# Installs (or updates) cron entries for the experiments DB backup.
# Safe to run multiple times — replaces existing ExpDB entries.
# =============================================================================

SCRIPT="$(cd "$(dirname "$0")" && pwd)/backup.sh"

if [[ ! -x "$SCRIPT" ]]; then
    chmod +x "$SCRIPT"
fi

# Remove any previous ExpDB backup lines, then append fresh ones
TMPFILE=$(mktemp)
crontab -l 2>/dev/null | grep -v "# ExpDB-backup" > "$TMPFILE" || true

cat >> "$TMPFILE" <<EOF

# ExpDB-backup — daily change-detect backup at 02:00
0 2 * * * $SCRIPT daily >> /home/sam/synology/Backups/Experiments-DB-Backup/backup.log 2>&1 # ExpDB-backup
# ExpDB-backup — weekly unconditional backup every Sunday at 03:00
0 3 * * 0 $SCRIPT weekly >> /home/sam/synology/Backups/Experiments-DB-Backup/backup.log 2>&1 # ExpDB-backup
EOF

crontab "$TMPFILE"
rm -f "$TMPFILE"

echo "Cron entries installed:"
crontab -l | grep "ExpDB-backup"
