#!/bin/bash
# ⬇️ Download Europe PBF file
# Downloads the latest Europe OSM PBF file from Geofabrik.

set -euo pipefail

log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}

# Configuration
DATA_DIR="$HOME/overpass_data"
PLANET_FILE="$DATA_DIR/europe-latest.osm.pbf"
REGION_URL="https://download.geofabrik.de/europe-latest.osm.pbf"

log "============================================="
log "🌍 EUROPE PBF DOWNLOAD"
log "============================================="
log "Target: $PLANET_FILE"
log "Source: $REGION_URL"
log ""

mkdir -p "$DATA_DIR"

if [ -f "$PLANET_FILE" ] && [ -s "$PLANET_FILE" ]; then
    FILE_SIZE=$(ls -lh "$PLANET_FILE" | awk '{print $5}')
    log "✅ Europe PBF file already exists ($FILE_SIZE)"
    log "   Delete it manually if you want to re-download."
else
    log "⬇️  Downloading Europe map (~28 GB)..."
    log "   This will take 1-3 hours depending on your connection."
    log ""
    
    # Use curl with resume support and progress
    curl -L -C - --progress-bar -o "$PLANET_FILE" "$REGION_URL"
    
    log ""
    log "✅ Download complete!"
fi

# Show file info
if [ -f "$PLANET_FILE" ]; then
    FILE_SIZE=$(ls -lh "$PLANET_FILE" | awk '{print $5}')
    log ""
    log "📦 File info:"
    log "   Path: $PLANET_FILE"
    log "   Size: $FILE_SIZE"
fi

log ""
log "Next step: Run 'Filter PBF' to extract roadtrip POIs"

