#!/bin/bash

# DAT Scraper Cron Wrapper Script
# This script is designed to be run from cron at 9:00 AM daily

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

# Set up environment variables
export DISPLAY=:0
export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"

# Log file with date
LOG_DIR="$SCRIPT_DIR/logs"
mkdir -p "$LOG_DIR"
LOG_FILE="$LOG_DIR/scraper_$(date +%Y-%m-%d_%H-%M-%S).log"

# Python path (adjust if needed)
PYTHON_PATH=$(which python3)

# Run the scraper in headless mode
echo "==========================================" >> "$LOG_FILE"
echo "DAT Scraper started at $(date)" >> "$LOG_FILE"
echo "==========================================" >> "$LOG_FILE"
echo "" >> "$LOG_FILE"

# Run the Python script with headless flag
"$PYTHON_PATH" "$SCRIPT_DIR/scrape.py" --headless >> "$LOG_FILE" 2>&1

EXIT_CODE=$?

echo "" >> "$LOG_FILE"
echo "==========================================" >> "$LOG_FILE"
echo "DAT Scraper finished at $(date)" >> "$LOG_FILE"
echo "Exit code: $EXIT_CODE" >> "$LOG_FILE"
echo "==========================================" >> "$LOG_FILE"

# Exit with the same code as the Python script
exit $EXIT_CODE

