#!/bin/bash
# Script to check if cron is running and the crontab is installed correctly

echo "=== Cron Checker Script ==="
echo "Date: $(date)"

# Create necessary log directories
mkdir -p /app/logs
mkdir -p /app/app/logs
touch /app/app/link_checker_daemon.log

# Check if cron is running without using ps
echo "Checking if cron is running..."
if pgrep cron >/dev/null 2>&1 || [ -f /var/run/crond.pid ] || service cron status >/dev/null 2>&1; then
    echo "✅ Cron is running"
else
    echo "❌ Cron is NOT running. Attempting to start..."
    service cron start 
    # Use a different method to check if it started
    if service cron status >/dev/null 2>&1; then
        echo "✅ Successfully started cron"
    else
        echo "❌ Failed to start cron"
    fi
fi

# Check crontab
echo "Checking crontab..."
if crontab -l 2>/dev/null | grep -q "MSD Broken Link Checker"; then
    echo "✅ Crontab is installed correctly"
    crontab -l
else
    echo "❌ Crontab is NOT installed correctly. Reinstalling..."
    
    # Check if permanent crontab file exists
    if [ -f "/app/crontab.txt" ]; then
        echo "Using permanent crontab file..."
        crontab /app/crontab.txt
    else
        # Create temporary crontab file
        CRON_FILE=$(mktemp)
        echo "# MSD Broken Link Checker cron jobs" > $CRON_FILE
        echo "* * * * * /app/cron_test.sh" >> $CRON_FILE
        echo "0 5 * * 1,3,5 cd /app/app && python app_daemon.py --excel URLs.xlsx --max-retries 3 --retry-timeout-multiplier 2.0" >> $CRON_FILE
        echo "30 5 * * 1,3,5 cd /app/app && python app_daemon.py --excel URLs_without_login.xlsx --without-login" >> $CRON_FILE
        
        # Create a permanent crontab file for future use
        echo "Creating permanent crontab file for future use..."
        cp $CRON_FILE /app/crontab.txt
        
        # Install crontab
        crontab $CRON_FILE
        rm $CRON_FILE
        
        # Verify installation
        if crontab -l 2>/dev/null | grep -q "MSD Broken Link Checker"; then
            echo "✅ Successfully reinstalled crontab:"
            crontab -l
        else
            echo "❌ Failed to reinstall crontab"
        fi
    fi
fi

# Check for test log
echo "Checking for test cron job execution..."
if [ -f "/app/logs/cron_test.log" ]; then
    echo "✅ Cron test log found. Last entries:"
    tail -5 /app/logs/cron_test.log
else
    echo "❌ Cron test log not found. Creating test script..."
    # Create test script
    echo "#!/bin/bash
echo \"Cron job executed at \$(date)\" >> /app/logs/cron_test.log" > /app/cron_test.sh
    chmod +x /app/cron_test.sh
    echo "Created test script. Please wait a minute to see if it executes."
fi

# Check for daemon log
echo "Checking for daemon execution log..."
if [ -f "/app/app/link_checker_daemon.log" ]; then
    echo "✅ Daemon log found. Last entries:"
    tail -5 /app/app/link_checker_daemon.log
else
    echo "❌ Daemon log not found or not yet created"
fi

# Do NOT run app_daemon.py directly to avoid sending unwanted emails
echo "Note: Skipping direct execution test of app_daemon.py to avoid sending emails"

echo "=== End of Cron Check ===" 