#!/bin/bash
# 02_init.sh - Initialisierungsfunktionen für Verzeichnisse und Assets

create_placeholder_web_conf() {
    local target_path="$1"
    local template_file="${EXAMPLE_TEMPLATE_DIR_PATH}/web.conf.template"
    if [ -f "$template_file" ]; then
        cp "$template_file" "$target_path"
        log_message "Beispiel-Konfiguration web.conf von Template nach ${target_path} kopiert."
    else
        log_message "WARNUNG: Beispiel-Konfigurations-Template nicht gefunden unter ${template_file}!"
        local world_key=$(basename "$(dirname "$target_path")")
        local content="# Minimale Konfiguration für die Welt '${world_key}'\n\nWORLD_DISPLAY_NAME=\"${world_key}\""
        create_placeholder_file "$target_path" "$content"
    fi
}

initialize_site_structure_and_assets() {
    log_message "Prüfe und erstelle Webseiten-Inhaltsverzeichnisse und Platzhalter..."
    mkdir -p "${WEB_ROOT_PATH}"
    mkdir -p "${WEB_CONTENT_BASE_PATH}"
    mkdir -p "${WEB_CONTENT_STATIC_PATH}"
    mkdir -p "${TEMPLATE_DIR_PATH}"
    mkdir -p "${EXAMPLE_TEMPLATE_DIR_PATH}"

    create_placeholder_file "${WEB_CONTENT_STATIC_PATH}/startseite_content.html" "<h2>Willkommen!</h2><p>Inhalt hier.</p>"
    create_placeholder_file "${WEB_CONTENT_STATIC_PATH}/impressum_content.html" "<h2>Impressum</h2><p>Impressum. Betreiber: ${SITE_OWNER_NAME}, Kontakt: ${SITE_OWNER_EMAIL}</p>"
    create_placeholder_file "${WEB_CONTENT_STATIC_PATH}/downloads_content.html" "<h2>Downloads</h2><p>Infos. Offiziell: <a href='https://www.luanti.org/downloads/'>luanti.org</a></p>"
    create_placeholder_file "${WEB_CONTENT_STATIC_PATH}/datenschutz_content.html" "<h2>Datenschutzerklärung</h2><p>Hier deinen Datenschutztext einfügen.</p>"

    local default_world_data_dir="${MINETESTMAPPER_WORLD_DATA_BASE_PATH}${DEFAULT_WORLD_NAME_KEY}"
    if [ -d "$default_world_data_dir" ] && [ -f "${default_world_data_dir}/world.mt" ]; then
        if [ ! -f "${default_world_data_dir}/web.conf" ]; then
            log_message "Erstelle Beispiel web.conf für '${DEFAULT_WORLD_NAME_KEY}'..."
            create_placeholder_web_conf "${default_world_data_dir}/web.conf"
        fi
    fi

    log_message "Kopiere Webseiten-Assets (Bilder, Icons)..."
    local web_images_target_dir="${WEB_ROOT_PATH}/images"
    local source_images_dir="${WEB_CONTENT_BASE_PATH}/${WEB_CONTENT_IMAGES_SOURCE_SUBDIR:-images_source}"
    mkdir -p "$web_images_target_dir"
    mkdir -p "${web_images_target_dir}/players"

    if [ -d "$source_images_dir" ]; then
        shopt -s nullglob
        for src_file in "$source_images_dir"/* ; do [ -f "$src_file" ] && cp -u "$src_file" "$web_images_target_dir/"; done
        for src_file in "$source_images_dir"/players/*.png ; do [ -f "$src_file" ] && cp -u "$src_file" "${web_images_target_dir}/players/"; done
        shopt -u nullglob
        log_message "Bilder und Spieler-Skins kopiert/aktualisiert."
    else
        log_message "WARNUNG: Quellverzeichnis für Bilder nicht gefunden: ${source_images_dir}"
    fi
}
