#!/bin/bash
# Script to install the cron jobs on the host server
# Run this script on the Dokku server as root

set -e

echo "===> Installing MSD Broken Link Checker cron jobs..."

# Copy the crontab file
echo "===> Copying crontab file to /etc/cron.d/msd-broken-link-checker"
cp dokku-crontab /etc/cron.d/msd-broken-link-checker

# Set proper permissions
echo "===> Setting proper permissions"
chmod 644 /etc/cron.d/msd-broken-link-checker
chown root:root /etc/cron.d/msd-broken-link-checker

# Verify installation
echo "===> Verifying installation"
if [ -f "/etc/cron.d/msd-broken-link-checker" ]; then
    echo "✅ Crontab installed successfully at /etc/cron.d/msd-broken-link-checker"
    echo "Crontab contents:"
    cat /etc/cron.d/msd-broken-link-checker
else
    echo "❌ Crontab installation failed"
    exit 1
fi

echo "===> Cron jobs have been installed on the host server."
echo "===> They will run at:"
echo "     - 5:00 AM every Monday, Wednesday, and Friday for URLs.xlsx"
echo "     - 5:30 AM every Monday, Wednesday, and Friday for URLs_without_login.xlsx"
echo ""
echo "===> You can test the jobs by running:"
echo "     dokku run msd-broken-link-checker cd /app/app && python app_daemon.py --excel URLs.xlsx --test 1 --max-retries 1"
echo ""
echo "===> Installation complete!" 