#!/bin/bash

# Setup Cron Job for DAT Scraper
# This script sets up a cron job to run the scraper every 10 minutes

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RUN_SCRIPT="$SCRIPT_DIR/run_scraper.sh"

# Make the run script executable
chmod +x "$RUN_SCRIPT"

# Get the current user
CURRENT_USER=$(whoami)

# Create the cron job entry (runs every 10 minutes: at :00, :10, :20, :30, :40, :50 of every hour)
CRON_JOB="*/10 * * * * $RUN_SCRIPT"

# Check if cron job already exists
if crontab -l 2>/dev/null | grep -q "$RUN_SCRIPT"; then
    echo "Cron job already exists. Removing old entry..."
    crontab -l 2>/dev/null | grep -v "$RUN_SCRIPT" | crontab -
fi

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

echo "✓ Cron job setup complete!"
echo ""
echo "Cron job details:"
echo "  Schedule: Every 10 minutes (at :00, :10, :20, :30, :40, :50 of every hour)"
echo "  Script: $RUN_SCRIPT"
echo ""
echo "To view your cron jobs, run: crontab -l"
echo "To remove the cron job, run: crontab -e (then delete the line)"
echo ""
echo "Logs will be saved to: $SCRIPT_DIR/logs/"
echo "XLSX files will be saved with date and time in filename (e.g., scraped_data_2026-01-27_14-30-45.xlsx)"

