#!/bin/bash

# Directory to save downloaded files
BASE_DIR="/var/www/efservers.com/html/games"
LOG_FILE="download_log.txt"

# Create necessary directories
mkdir -p "$BASE_DIR/baseEF"
mkdir -p "$BASE_DIR/q3maps"

# Create or clear the log file
> $LOG_FILE

# Download the HTML file
wget -q -O maps.html https://stvef.org/files/map-pk3s-2024-07-05.html

# Extract URLs from the HTML file and download files
grep -oP 'http://74.91.116.133/\K[^"]+' maps.html | while read -r FILE_PATH; do
  # Determine the directory to save the file
  DIR=$(dirname "$FILE_PATH")
  mkdir -p "$BASE_DIR/$DIR"
  
  # Check if the file already exists
  if [[ ! -f "$BASE_DIR/$FILE_PATH" ]]; then
    # Download the file and log the action
    wget -q -P "$BASE_DIR/$DIR" "http://74.91.116.133/$FILE_PATH"
    echo "Downloaded: $FILE_PATH" >> $LOG_FILE
  #else
  #  echo "Exists: $FILE_PATH" >> $LOG_FILE
  fi
done

# Clean up
rm maps.html

echo "Download completed. Check $LOG_FILE for details."
