#!/bin/bash

staelu=("HST" "MM_GW" "MM_Recknitz_GW" "MM_RX" "MS" "UEM" "WM")
subdirs=("Arbeitsplatz" "ZeitreihenEditiert" "ZeitreihenRoh")

docker_container="fispegel-eel"
download_base_url="https://download.softwareschneiderei.de/lubw/fis-pegel/lung-data"


# Function to download, extract, and copy files into Docker container
process_stalu() {
    local stalu=$1
    local subdir=$2

    # URL
    url="${download_base_url}/${subdir}/${stalu}.7z"

    # Download file
    wget $url -O ${subdir}/${stalu}.7z

    # Extract file
    7z x ${subdir}/${stalu}.7z -o${subdir}/{stalu}

    # Remove downloaded file
    rm -f ${subdir}/${stalu}.7z

    # Copy to Docker container
    docker exec ${docker_container} mkdir -p /app/import/${subdir}
    docker cp ${subdir}/${stalu} ${docker_container}:/app/import/${subdir}/

    # Remove extracted directory
    rm -rf ${subdir}/${stalu}
}

# Create subdirectories if not existing
for subdir in "${subdirs[@]}"; do
  mkdir -p ${subdir}
done

# Process StÄLU
for stalu in "${staelu[@]}"; do
    for subdir in "${subdirs[@]}"; do
        process_stalu $stalu $subdir
    done
done
