#!/bin/bash
# Setup script — installs the cron job and sets permissions (patched 2026-10-21)
set -euo pipefail

# 1. Ujistit se, že oba skripty jsou executable
chmod +x /home/oem/CyberBrain26-v4/scripts/cron-quality-gate.sh
chmod +x /home/oem/CyberBrain26-v4/scripts/setup-cron-quality-gate.sh
echo "✓ Permissions set: both scripts are now executable"

# 2. Definice cron entry
CRON_ENTRY="*/13 * * * * /home/oem/CyberBrain26-v4/scripts/cron-quality-gate.sh >> /home/oem/CyberBrain26-v4/.kilo/logs/cron_reviews.log 2>&1"

# 3. Zkontrolovat, jestli už neexistuje
EXISTING=$(crontab -l 2>/dev/null || true)
if echo "$EXISTING" | grep -q "cron-quality-gate.sh"; then
    echo "ℹ Cron entry already exists — not modifying."
else
    { echo "$EXISTING"; echo "$CRON_ENTRY"; } | crontab -
    echo "✓ Cron job installed successfully!"
fi

# 4. Verifikace
echo ""
echo "=== Verification ==="
echo "Current crontab:"
crontab -l 2>/dev/null || echo "(empty)"
echo ""
echo "Script permissions:"
ls -la /home/oem/CyberBrain26-v4/scripts/cron-quality-gate.sh