#!/usr/bin/env bash
set -euo pipefail

# Setup DNS Challenge Verification Cron Job
# This script adds a cron job to verify pending DNS-01 challenges
# for wildcard SSL certificates every minute.

CRON_JOB="* * * * * /usr/bin/php /root/whp/scripts/verify-dns-challenges.php >> /var/log/whp/dns-challenges.log 2>&1"
CRON_MARKER="verify-dns-challenges"

# Create log directory if it doesn't exist
if [ ! -d /var/log/whp ]; then
    mkdir -p /var/log/whp
fi

# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "${CRON_MARKER}"; then
    echo "DNS challenge verification cron job already exists, skipping."
    exit 0
fi

# Add cron job
(crontab -l 2>/dev/null || true; echo "${CRON_JOB}") | crontab -

echo "DNS challenge verification cron job installed successfully."
exit 0
