#!/bin/bash
# ============================================
# Copernicus DEM Download Script for GeoEdu Service
# ============================================
#
# WICHTIG: Dieses Script startet einen Download von ca. 20-40 GB!
# Nur nach expliziter Freigabe ausfuehren!
#
# Quelle: Copernicus Data Space Ecosystem
# Daten: GLO-30 DEM (30m Aufloesung)
# Lizenz: Copernicus Data (frei, Attribution erforderlich)
# Attribution: © Copernicus Service Information
#
# Voraussetzungen:
# - Copernicus Data Space Account (kostenlos)
# - Credentials in ~/.netrc oder als Umgebungsvariablen
#
# Nutzung:
#   ./download_dem.sh [--dry-run] [--bbox west,south,east,north]
#
# Beispiel (nur Bayern):
#   ./download_dem.sh --bbox 8.97,47.27,13.84,50.56
#
# ============================================

set -e

# Configuration
DATA_DIR="${DEM_DATA_DIR:-/app/data/dem}"
COPERNICUS_URL="https://dataspace.copernicus.eu"

# Germany bounding box (default)
BBOX_WEST="${BBOX_WEST:-5.87}"
BBOX_SOUTH="${BBOX_SOUTH:-47.27}"
BBOX_EAST="${BBOX_EAST:-15.04}"
BBOX_NORTH="${BBOX_NORTH:-55.06}"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}============================================${NC}"
echo -e "${YELLOW}GeoEdu Service - Copernicus DEM Download${NC}"
echo -e "${YELLOW}============================================${NC}"
echo ""
echo -e "Quelle:      ${GREEN}Copernicus Data Space${NC}"
echo -e "Daten:       ${GREEN}GLO-30 DEM (30m Aufloesung)${NC}"
echo -e "Groesse:     ${YELLOW}~20-40 GB (Deutschland komplett)${NC}"
echo -e "Lizenz:      ${GREEN}Copernicus Data (frei)${NC}"
echo -e "Attribution: ${GREEN}© Copernicus Service Information${NC}"
echo ""

# Parse arguments
DRY_RUN=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        --bbox)
            IFS=',' read -r BBOX_WEST BBOX_SOUTH BBOX_EAST BBOX_NORTH <<< "$2"
            shift 2
            ;;
        *)
            echo "Unbekannte Option: $1"
            exit 1
            ;;
    esac
done

echo "Bounding Box:"
echo "  West:  $BBOX_WEST"
echo "  Sued:  $BBOX_SOUTH"
echo "  Ost:   $BBOX_EAST"
echo "  Nord:  $BBOX_NORTH"
echo ""

# Calculate required tiles
# Copernicus DEM tiles are 1°x1° cells
calc_tiles() {
    local west=$(echo "$BBOX_WEST" | cut -d. -f1)
    local south=$(echo "$BBOX_SOUTH" | cut -d. -f1)
    local east=$(echo "$BBOX_EAST" | cut -d. -f1)
    local north=$(echo "$BBOX_NORTH" | cut -d. -f1)

    # Adjust for negative values
    west=$((west < 0 ? west : west))

    local count=0
    for lat in $(seq $south $north); do
        for lon in $(seq $west $east); do
            count=$((count + 1))
        done
    done
    echo $count
}

TILE_COUNT=$(calc_tiles)
ESTIMATED_SIZE=$((TILE_COUNT * 50)) # ~50 MB per tile

echo "Benoetigte Tiles: $TILE_COUNT"
echo "Geschaetzte Groesse: ~${ESTIMATED_SIZE} MB"
echo ""

if [ "$DRY_RUN" = true ]; then
    echo -e "${YELLOW}[DRY-RUN] Kein Download wird durchgefuehrt.${NC}"
    echo ""
    echo "Wuerde folgende Tiles herunterladen:"
    echo ""

    for lat in $(seq $(echo "$BBOX_SOUTH" | cut -d. -f1) $(echo "$BBOX_NORTH" | cut -d. -f1)); do
        for lon in $(seq $(echo "$BBOX_WEST" | cut -d. -f1) $(echo "$BBOX_EAST" | cut -d. -f1)); do
            lat_prefix=$([ $lat -ge 0 ] && echo "N" || echo "S")
            lon_prefix=$([ $lon -ge 0 ] && echo "E" || echo "W")
            printf "  %s%02d%s%03d.tif\n" "$lat_prefix" "${lat#-}" "$lon_prefix" "${lon#-}"
        done
    done
    echo ""
    exit 0
fi

# Confirm download
echo -e "${RED}ACHTUNG: Download startet ~${ESTIMATED_SIZE} MB Daten!${NC}"
echo ""
read -p "Download starten? (j/N) " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[JjYy]$ ]]; then
    echo "Download abgebrochen."
    exit 1
fi

# Create data directory
mkdir -p "$DATA_DIR"
cd "$DATA_DIR"

# Check for AWS CLI (preferred method for Copernicus)
if ! command -v aws &> /dev/null; then
    echo -e "${YELLOW}HINWEIS: AWS CLI nicht installiert.${NC}"
    echo "Installiere mit: pip install awscli"
    echo ""
    echo "Alternative: Manueller Download von:"
    echo "  https://dataspace.copernicus.eu/explore-data/data-collections/copernicus-contributing-missions/collections-description/COP-DEM"
    exit 1
fi

# Download tiles
echo ""
echo -e "${GREEN}Starte Download...${NC}"

DOWNLOADED=0
FAILED=0

for lat in $(seq $(echo "$BBOX_SOUTH" | cut -d. -f1) $(echo "$BBOX_NORTH" | cut -d. -f1)); do
    for lon in $(seq $(echo "$BBOX_WEST" | cut -d. -f1) $(echo "$BBOX_EAST" | cut -d. -f1)); do
        lat_prefix=$([ $lat -ge 0 ] && echo "N" || echo "S")
        lon_prefix=$([ $lon -ge 0 ] && echo "E" || echo "W")

        lat_abs=${lat#-}
        lon_abs=${lon#-}

        filename=$(printf "%s%02d%s%03d.tif" "$lat_prefix" "$lat_abs" "$lon_prefix" "$lon_abs")

        if [ -f "$filename" ]; then
            echo "  ✓ $filename (bereits vorhanden)"
            DOWNLOADED=$((DOWNLOADED + 1))
            continue
        fi

        echo "  ↓ $filename"

        # Copernicus DEM S3 bucket path
        # Format: s3://copernicus-dem-30m/Copernicus_DSM_COG_10_N47_00_E008_00_DEM/
        s3_path="s3://copernicus-dem-30m/Copernicus_DSM_COG_10_${lat_prefix}${lat_abs}_00_${lon_prefix}${lon_abs}_00_DEM/"

        if aws s3 cp "${s3_path}Copernicus_DSM_COG_10_${lat_prefix}${lat_abs}_00_${lon_prefix}${lon_abs}_00_DEM.tif" "$filename" --no-sign-request 2>/dev/null; then
            DOWNLOADED=$((DOWNLOADED + 1))
        else
            echo -e "    ${YELLOW}(nicht verfuegbar)${NC}"
            FAILED=$((FAILED + 1))
        fi
    done
done

echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN}Download abgeschlossen${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo "Heruntergeladen: $DOWNLOADED Tiles"
echo "Nicht verfuegbar: $FAILED Tiles"
echo "Speicherort: $DATA_DIR"
echo ""
echo -e "${YELLOW}Naechster Schritt:${NC}"
echo "  Die Tiles werden automatisch vom geo-service geladen."
