#!/bin/bash

# Lade globale Konfiguration
CONFIG_FILE_PATH="$(dirname "$0")/config.sh"
if [ -f "$CONFIG_FILE_PATH" ]; then
    source "$CONFIG_FILE_PATH"
else
    echo "FEHLER: Konfigurationsdatei config.sh nicht unter ${CONFIG_FILE_PATH} gefunden!"
    exit 1
fi

# === Abgeleitete Variablen ===
SCRIPT_BASENAME=$(basename "$0" .sh)
LOG_FILE="${LOG_DIR_BASE}/${SCRIPT_BASENAME}.log"
LOCK_FILE="${LOCK_FILE_BASE_DIR}/${SCRIPT_BASENAME}.lock"
WEB_CONTENT_STATIC_PATH="${WEB_CONTENT_BASE_PATH}/${WEB_CONTENT_STATIC_SUBDIR}"
CACHE_BUSTER=$(date +%s)
CURRENT_YEAR=$(date '+%Y')
ACTUAL_BANNER_IMG_URL_PATH="${FALLBACK_BANNER_IMG_URL}"

# === Logging Funktion ===
log_message() { local msg; msg="$(date '+%Y-%m-%d %H:%M:%S') - $1"; echo "${msg}" | tee -a "$LOG_FILE"; }

# === Hilfsfunktionen für Platzhalter ===
create_placeholder_file() { local fp="$1"; local dc="$2"; if [ ! -f "$fp" ]; then mkdir -p "$(dirname "$fp")"; echo -e "$dc" > "$fp"; log_message "Platzhalterdatei: $fp"; fi; }
get_config_value_from_file() { local cf="$1"; local k="$2"; local dv="${3:-}"; local v; if [ ! -f "$cf" ]; then echo "$dv"; return; fi; v=$(grep -E "^\s*${k}\s*=" "$cf"|tail -n 1|sed -e 's/\s*#.*//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'|cut -d'=' -f2-|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/^"//' -e 's/"$//' -e "s/^'//" -e "s/'$//"); if [ -n "$v" ]; then echo "$v"; else echo "$dv"; fi;}

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 "FEHLER: 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
}

# === Template Rendering Funktion (FINAL, ROBUST) ===
render_template() {
    local template_path="$1"
    local output_path="$2"
    shift 2
    local replacements=("$@")

    if [ ! -f "$template_path" ]; then
        log_message "FEHLER: Template-Datei nicht gefunden: ${template_path}"
        return 1
    fi

    local template_content
    template_content=$(<"$template_path")

    # Wende jede Ersetzung nacheinander an.
    # Diese Methode ist robuster als eine einzelne große Ersetzung.
    for ((i=0; i<${#replacements[@]}; i+=2)); do
        local key_name="${replacements[i]}"
        local key="%%${key_name}%%"
        local value="${replacements[i+1]}"
        
        local new_content=""
        # Wir zerlegen den String manuell, um mehrzeilige Ersetzungen zu ermöglichen.
        # Dies ersetzt alle Vorkommen des Schlüssels.
        while [[ "$template_content" == *"$key"* ]]; do
            # Füge den Teil vor dem Schlüssel und den Ersatzwert hinzu
            new_content+="${template_content%%$key*}${value}"
            # Entferne den Teil, der bereits verarbeitet wurde
            template_content="${template_content#*$key}"
        done
        # Füge den verbleibenden Rest des Inhalts hinzu
        template_content="${new_content}${template_content}"
    done

    echo "$template_content" > "$output_path"
}

# === HTML/CSS Erzeugungsfunktionen ===
generate_css() {
    local css_file_path="${WEB_ROOT_PATH}/style.css"
    log_message "Erzeuge/Aktualisiere ${css_file_path} aus Template..."
    render_template "${TEMPLATE_DIR_PATH}/css.template" "$css_file_path" \
        "css_banner_image_path" "$ACTUAL_BANNER_IMG_URL_PATH" \
        "CACHE_BUSTER" "$CACHE_BUSTER"
    if [ $? -eq 0 ]; then log_message "CSS-Datei erfolgreich erstellt."; else log_message "FEHLER CSS."; fi
}

generate_html_header() {
    local current_page_title="$1"
    local relative_path_prefix="${2:-.}"
    local active_page_id="${3:-}"
    local active_class_home=""; local active_class_worlds=""; local active_class_downloads=""
    case "$active_page_id" in
        home)      active_class_home="active" ;;
        worlds)    active_class_worlds="active" ;;
        downloads) active_class_downloads="active" ;;
    esac
    
    local header_file
    header_file=$(mktemp)
    render_template "${TEMPLATE_DIR_PATH}/html_header.template" "$header_file" \
        "current_page_title" "$current_page_title" \
        "relative_path_prefix" "$relative_path_prefix" \
        "active_class_home" "$active_class_home" \
        "active_class_worlds" "$active_class_worlds" \
        "active_class_downloads" "$active_class_downloads" \
        "CACHE_BUSTER" "$CACHE_BUSTER" \
        "SITE_TITLE" "$SITE_TITLE"
    cat "$header_file"
    rm "$header_file"
}

generate_html_footer() {
    local footer_file
    footer_file=$(mktemp)
    render_template "${TEMPLATE_DIR_PATH}/html_footer.template" "$footer_file" \
        "CURRENT_YEAR" "$CURRENT_YEAR" \
        "SITE_TITLE" "$SITE_TITLE" \
        "SITE_OWNER_NAME" "$SITE_OWNER_NAME"
    cat "$footer_file"
    rm "$footer_file"
}

generate_homepage() { local p="${WEB_ROOT_PATH}/index.html"; local c="${WEB_CONTENT_STATIC_PATH}/startseite_content.html"; log_message "Erzeuge ${p}..."; generate_html_header "Willkommen" "." "home" > "$p"; if [ -f "$c" ]; then cat "$c" >> "$p"; else echo "<h2>Willkommen!</h2>" >> "$p"; fi; generate_html_footer >> "$p"; }
generate_impressum() { local p="${WEB_ROOT_PATH}/impressum.html"; local c="${WEB_CONTENT_STATIC_PATH}/impressum_content.html"; log_message "Erzeuge ${p}..."; generate_html_header "Impressum" "" > "$p"; if [ -f "$c" ]; then cat "$c" >> "$p"; else echo "<h2>Impressum</h2><p>Betreiber: ${SITE_OWNER_NAME}<br>Kontakt: <a href='mailto:${SITE_OWNER_EMAIL}'>${SITE_OWNER_EMAIL}</a></p>" >> "$p"; fi; generate_html_footer >> "$p"; }
generate_downloads_page() { local p="${WEB_ROOT_PATH}/downloads.html"; local c="${WEB_CONTENT_STATIC_PATH}/downloads_content.html"; log_message "Erzeuge ${p}..."; generate_html_header "Downloads" "." "downloads" > "$p"; if [ -f "$c" ]; then cat "$c" >> "$p"; else cat >>"$p" <<EOF
<h2>Downloads</h2><p>Offizielle Seite: <a href="https://www.luanti.org/downloads/" target="_blank">luanti.org</a></p>
EOF
fi; generate_html_footer >> "$p"; }
generate_datenschutz_page() { local p="${WEB_ROOT_PATH}/datenschutz.html"; local c="${WEB_CONTENT_STATIC_PATH}/datenschutz_content.html"; log_message "Erzeuge ${p}..."; generate_html_header "Datenschutz" "" > "$p"; if [ -f "$c" ]; then cat "$c" >> "$p"; else echo "<h2>Datenschutzerklärung</h2><p>Platzhalter.</p>" >> "$p"; fi; generate_html_footer >> "$p"; }

generate_worlds_overview() {
    local overview_file="${WEB_ROOT_PATH}/worlds.html"
    log_message "Erzeuge Weltenübersicht: ${overview_file}..."
    generate_html_header "Weltenübersicht" "." "worlds" > "$overview_file"
    echo "<h2>Unsere Welten</h2>" >> "$overview_file"
    
    local discovered_worlds_count=0; shopt -s nullglob
    local world_key_dirs=("${MINETESTMAPPER_WORLD_DATA_BASE_PATH}"/*/)
    shopt -u nullglob

    if [ ${#world_key_dirs[@]} -eq 0 ]; then
        log_message "WARNUNG: Keine Welt-Verzeichnisse in ${MINETESTMAPPER_WORLD_DATA_BASE_PATH}."
        echo "<p>Keine Welten verfügbar.</p>" >> "$overview_file"
    else
        local overview_entry_template_path="${TEMPLATE_DIR_PATH}/worlds_overview_entry.template"
        for world_data_dir_loop_overview in "${world_key_dirs[@]}"; do
            local current_world_key
            current_world_key=$(basename "$world_data_dir_loop_overview")
            local world_mt_file="${world_data_dir_loop_overview}world.mt"
            local web_conf_file="${world_data_dir_loop_overview}web.conf"
            if [ ! -f "$world_mt_file" ] || [ ! -f "$web_conf_file" ]; then continue; fi
            
            local world_display_name_ov
            world_display_name_ov=$(get_config_value_from_file "$web_conf_file" "WORLD_DISPLAY_NAME")
            local world_short_desc_ov
            world_short_desc_ov=$(get_config_value_from_file "$web_conf_file" "WORLD_SHORT_DESCRIPTION" "$DEFAULT_WORLD_SHORT_DESCRIPTION")
            
            unset ADMIN_NAME; source "$web_conf_file" &>/dev/null
            local admin_name_ov
            if [ ${#ADMIN_NAME[@]} -gt 0 ]; then admin_name_ov=$(IFS=', '; echo "${ADMIN_NAME[*]}"); else admin_name_ov="$SITE_OWNER_NAME"; fi
            
            local current_map_png_filename_for_preview_ov
            current_map_png_filename_for_preview_ov=$(get_config_value_from_file "$web_conf_file" "WEB_MAP_PNG_FILENAME" "$DEFAULT_WEB_MAP_PNG_FILENAME")

            if [ -z "$world_display_name_ov" ]; then 
                local mt_world_name_from_mt_ov
                mt_world_name_from_mt_ov=$(grep -E "^\s*world_name\s*=" "$world_mt_file" | tail -n 1 | cut -d'=' -f2 | xargs)
                if [ -n "$mt_world_name_from_mt_ov" ]; then world_display_name_ov="$mt_world_name_from_mt_ov"; 
                else world_display_name_ov="Welt ${current_world_key}"; fi
            fi
            
            local detail_page_filename="world_${current_world_key}.html"
            local preview_img_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/${current_map_png_filename_for_preview_ov}"
            local preview_img_abs_path="${WEB_ROOT_PATH}/${preview_img_rel_path}"
            local online_status_text="offline"; local online_status_class="offline"
            local status_file_for_overview="${WEB_ROOT_PATH}/${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/online_status.txt"
            if [ -f "$status_file_for_overview" ]; then
                local status_line_overview
                status_line_overview=$(cat "$status_file_for_overview")
                if [[ "$status_line_overview" == "online"* ]]; then online_status_text="online"; online_status_class="online"; fi
            fi
            
            local preview_img_html
            if [ -f "$preview_img_abs_path" ]; then
                preview_img_html="<img src='${preview_img_rel_path}?v=${CACHE_BUSTER}' alt='Vorschau ${world_display_name_ov}'>"
            else
                preview_img_html="<img src='images/placeholder_map.png?v=${CACHE_BUSTER}' alt='Keine Vorschau für ${world_display_name_ov}'>"
            fi
            
            local temp_overview_entry_file
            temp_overview_entry_file=$(mktemp)
            render_template "$overview_entry_template_path" "$temp_overview_entry_file" \
                "detail_page_filename" "$detail_page_filename" \
                "preview_img_html" "$preview_img_html" \
                "world_display_name_ov" "$world_display_name_ov" \
                "online_status_class" "$online_status_class" \
                "online_status_text" "$online_status_text" \
                "admin_name_ov" "$admin_name_ov" \
                "world_short_desc_ov" "$world_short_desc_ov"
            cat "$temp_overview_entry_file" >> "$overview_file"
            rm "$temp_overview_entry_file"
            
            discovered_worlds_count=$((discovered_worlds_count + 1))
        done
        if [ "$discovered_worlds_count" -eq 0 ]; then echo "<p>Keine Welten mit gültiger Konfiguration gefunden.</p>" >> "$overview_file"; fi
    fi
    generate_html_footer >> "$overview_file"
    if [ $? -eq 0 ]; then log_message "Weltenübersicht erstellt."; else log_message "FEHLER Weltenübersicht."; fi
}

generate_world_detail_page() {
    local current_world_key="$1"; local detail_page_file="${WEB_ROOT_PATH}/world_${current_world_key}.html"
    local current_minetest_world_path="${MINETESTMAPPER_WORLD_DATA_BASE_PATH}${current_world_key}" 
    local world_mt_file="${current_minetest_world_path}/world.mt"; local web_conf_file="${current_minetest_world_path}/web.conf"
    if [ ! -f "$world_mt_file" ] || [ ! -f "$web_conf_file" ]; then log_message "FEHLER: world.mt oder web.conf für ${current_world_key} fehlt."; return 1; fi

    local map_info_file="${BASE_SCRIPT_DIR}/${RAW_MAP_BASE_SUBDIR}/${current_world_key}/map_info.txt"
    local MAP_WIDTH=0; local MAP_HEIGHT=0; local MAP_EXTENT_MIN_X=0; local MAP_EXTENT_MIN_Z=0; local MAP_EXTENT_WIDTH=0; local MAP_EXTENT_HEIGHT=0
    if [ -f "$map_info_file" ]; then
        local map_dim_val; map_dim_val=$(get_config_value_from_file "$map_info_file" "map_dimension")
        if [[ "$map_dim_val" == *"x"* ]]; then
            MAP_WIDTH=$(echo "$map_dim_val" | cut -d'x' -f1)
            MAP_HEIGHT=$(echo "$map_dim_val" | cut -d'x' -f2)
        fi
        local map_ext_val; map_ext_val=$(get_config_value_from_file "$map_info_file" "map_extent")
        if [[ "$map_ext_val" == *":"* && "$map_ext_val" == *"+"* ]]; then
            local min_coords; min_coords=$(echo "$map_ext_val" | cut -d'+' -f1)
            local extent_dims; extent_dims=$(echo "$map_ext_val" | cut -d'+' -f2,3)
            MAP_EXTENT_MIN_X=$(echo "$min_coords" | cut -d':' -f1)
            MAP_EXTENT_MIN_Z=$(echo "$min_coords" | cut -d':' -f2)
            MAP_EXTENT_WIDTH=$(echo "$extent_dims" | cut -d'+' -f1)
            MAP_EXTENT_HEIGHT=$(echo "$extent_dims" | cut -d'+' -f2)
        fi
    else
        log_message "WARNUNG: ${map_info_file} für '${current_world_key}' nicht gefunden."
    fi

    local WORLD_DISPLAY_NAME_PAGE; WORLD_DISPLAY_NAME_PAGE=$(get_config_value_from_file "$web_conf_file" "WORLD_DISPLAY_NAME")
    if [ -z "$WORLD_DISPLAY_NAME_PAGE" ]; then
        WORLD_DISPLAY_NAME_PAGE=$(get_config_value_from_file "$world_mt_file" "world_name" "Welt ${current_world_key}")
    fi

    local SERVER_ADDRESS_PAGE; SERVER_ADDRESS_PAGE=$(get_config_value_from_file "$web_conf_file" "SERVER_ADDRESS" "$DEFAULT_SERVER_ADDRESS")
    local SERVER_PORT_PAGE; SERVER_PORT_PAGE=$(get_config_value_from_file "$web_conf_file" "SERVER_PORT" "$DEFAULT_SERVER_PORT")
    local SERVER_ACCESS_INFO_PAGE; SERVER_ACCESS_INFO_PAGE=$(get_config_value_from_file "$web_conf_file" "SERVER_ACCESS_INFO" "$DEFAULT_SERVER_ACCESS_INFO")

    unset ADMIN_NAME ADMIN_SKIN_URL ADMIN_EMAIL ADMIN_DISCORD ADMIN_MATRIX ADMIN_STEAM ADMIN_TEAMSPEAK ADMIN_MUMBLE WORLD_LONG_DESCRIPTION WORLD_GAME_RULES
    source "$web_conf_file"
    local WORLD_LONG_DESCRIPTION_PAGE="${WORLD_LONG_DESCRIPTION:-$DEFAULT_WORLD_LONG_DESCRIPTION}"
    local WORLD_GAME_RULES_PAGE="${WORLD_GAME_RULES:-}"
    
    local STATUS_TEXT_FALLBACK_PAGE="$DEFAULT_SERVER_STATUS_TEXT_FALLBACK"
    local CURRENT_WEB_MAP_PNG_FILENAME; CURRENT_WEB_MAP_PNG_FILENAME=$(get_config_value_from_file "$web_conf_file" "WEB_MAP_PNG_FILENAME" "$DEFAULT_WEB_MAP_PNG_FILENAME")
    local CURRENT_ARCHIVE_SUBDIR_NAME; CURRENT_ARCHIVE_SUBDIR_NAME=$(get_config_value_from_file "$web_conf_file" "ARCHIVE_SUBDIR_NAME" "$DEFAULT_ARCHIVE_SUBDIR_NAME")
    
    local MT_GAMEID="$DEFAULT_GAMEID"; local MT_ENABLE_DAMAGE="false"; local MT_CREATIVE_MODE="false"
    declare -A parsed_mod_packs; declare -a parsed_standalone_mods
    while IFS='=' read -r key value || [ -n "$key" ]; do
        key=$(echo "$key"|xargs); value=$(echo "$value"|xargs)
        case "$key" in
            gameid) MT_GAMEID="$value" ;;
            enable_damage) MT_ENABLE_DAMAGE="$value" ;;
            creative_mode) MT_CREATIVE_MODE="$value" ;;
            load_mod_*) local mod_id="${key#load_mod_}"; mod_id=$(echo "$mod_id"|xargs)
                if [[ "$value" =~ ^mods/([^/]+)/.+ ]]; then local pack_n="${BASH_REMATCH[1]}"; parsed_mod_packs["$pack_n"]="${parsed_mod_packs[$pack_n]} ${mod_id}";
                elif [[ "$value" =~ ^mods/([^/]+)$ ]]; then parsed_standalone_mods+=("$mod_id");
                elif [ -n "$value" ]; then parsed_standalone_mods+=("$mod_id"); fi ;;
        esac;
    done < "$world_mt_file"
    
    local MODS_HTML=""
    if [ ${#parsed_mod_packs[@]} -gt 0 ] || [ ${#parsed_standalone_mods[@]} -gt 0 ]; then
        MODS_HTML+="<ul class='mod-list'>"
        local sorted_pack_names=(); if [ ${#parsed_mod_packs[@]} -gt 0 ]; then mapfile -t sorted_pack_names < <(printf '%s\n' "${!parsed_mod_packs[@]}" | sort); fi
        for pack_name in "${sorted_pack_names[@]}"; do
            MODS_HTML+="<li>+ <a href='https://content.luanti.org/packages/?q=${pack_name}' target='_blank' rel='noopener noreferrer'>${pack_name}</a><ul>"
            local mods_in_pack_str="${parsed_mod_packs[$pack_name]}"
            local sorted_mods_in_pack=(); if [ -n "$mods_in_pack_str" ]; then mapfile -t sorted_mods_in_pack < <(echo "$mods_in_pack_str" | tr ' ' '\n' | grep -v '^\s*$' | sort); fi
            for mod_in_pack in "${sorted_mods_in_pack[@]}"; do
                [ -n "$mod_in_pack" ] && MODS_HTML+="<li>- <a href='https://content.luanti.org/packages/?q=${mod_in_pack}' target='_blank' rel='noopener noreferrer'>${mod_in_pack}</a></li>"
            done
            MODS_HTML+="</ul></li>"
        done
        local sorted_standalone_mods=(); if [ ${#parsed_standalone_mods[@]} -gt 0 ]; then mapfile -t sorted_standalone_mods < <(printf '%s\n' "${parsed_standalone_mods[@]}" | sort); fi
        for solo_mod in "${sorted_standalone_mods[@]}"; do
            [ -n "$solo_mod" ] && MODS_HTML+="<li>- <a href='https://content.luanti.org/packages/?q=${solo_mod}' target='_blank' rel='noopener noreferrer'>${solo_mod}</a></li>"
        done
        MODS_HTML+="</ul>"
    else
        MODS_HTML="<p>Keine Mod-Informationen in world.mt gefunden.</p>"
    fi
    
    # KORREKTUR: Logik zur Archiv-Suche wiederhergestellt (wie im Original-Skript)
    local ARCHIVE_HTML=""; local available_archive_dates_js_array="[]"; local -a available_archive_dates_bash=();
    local archive_scan_base_path="${WEB_ROOT_PATH}/${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/${CURRENT_ARCHIVE_SUBDIR_NAME}";
    if [ -d "$archive_scan_base_path" ]; then
        shopt -s nullglob;
        for y_d in "${archive_scan_base_path}"/*/; do
            if [ -d "$y_d" ]; then
                local y=$(basename "$y_d");
                for m_d in "${y_d}"*/; do
                    if [ -d "$m_d" ]; then
                        local m=$(basename "$m_d");
                        for d_f in "${m_d}"*.png; do
                            if [ -f "$d_f" ]; then
                                local d=$(basename "$d_f" .png);
                                if [[ "$y" =~ ^[0-9]{4}$ && "$m" =~ ^[0-9]{2}$ && "$d" =~ ^[0-9]{2}$ ]]; then
                                    available_archive_dates_bash+=("${y}-${m}-${d}");
                                fi;
                            fi;
                        done;
                    fi;
                done;
            fi;
        done;
        shopt -u nullglob;
    fi;
    if [ ${#available_archive_dates_bash[@]} -gt 0 ]; then
        local sorted_dates_str; sorted_dates_str=$(printf '%s\n' "${available_archive_dates_bash[@]}" | sort -r);
        local js_dates_temp_array=(); if [ -n "$sorted_dates_str" ]; then mapfile -t js_dates_temp_array < <(echo "$sorted_dates_str"); fi;
        local js_array_content="";
        if [ ${#js_dates_temp_array[@]} -gt 0 ]; then
            for date_str_loop in "${js_dates_temp_array[@]}"; do
                if [ -n "$date_str_loop" ]; then js_array_content+="\"${date_str_loop}\","; fi;
            done;
            js_array_content=${js_array_content%,};
        fi;
        available_archive_dates_js_array="[${js_array_content}]";
        local temp_archive_html_file; temp_archive_html_file=$(mktemp)
        local archive_world_rel_to_webroot="/${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/${CURRENT_ARCHIVE_SUBDIR_NAME}"
        render_template "${TEMPLATE_DIR_PATH}/world_detail_archive.template" "$temp_archive_html_file" \
            "current_world_key" "$current_world_key" \
            "available_archive_dates_js_array" "$available_archive_dates_js_array" \
            "archive_world_rel_to_webroot" "$archive_world_rel_to_webroot" \
            "CACHE_BUSTER" "$CACHE_BUSTER"
        ARCHIVE_HTML=$(<"$temp_archive_html_file")
        rm "$temp_archive_html_file"
    else
        ARCHIVE_HTML="<p>Keine Archivbilder für diese Welt verfügbar.</p>";
    fi

    local web_map_preview_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/${CURRENT_WEB_MAP_PNG_FILENAME}"
    local web_online_status_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/online_status.txt"
    local web_last_update_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/last_update.txt"
    local web_players_txt_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/players.txt"
    local web_weather_txt_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/weather.txt"
    local web_unknown_nodes_rel_path="${WEB_MAPS_BASE_SUBDIR}/${current_world_key}/unknown_nodes.txt"
    local unknown_nodes_abs_path="${WEB_ROOT_PATH}/${web_unknown_nodes_rel_path}"

    local creative_text="Aus"; local creative_text_class="offline"; if [ "$MT_CREATIVE_MODE" = "true" ]; then creative_text="An"; creative_text_class="online"; fi
    local damage_text="Aus"; local damage_text_class="online"; if [ "$MT_ENABLE_DAMAGE" = "true" ]; then damage_text="An"; damage_text_class="offline"; fi
    
    local ADMIN_BOXES_HTML=""
    local num_admins=${#ADMIN_NAME[@]}
    if [ $num_admins -eq 0 ]; then
        ADMIN_BOXES_HTML="<div class='admin-box'><div class='admin-contact-block'><img src='/${SITE_OWNER_ADMIN_SKIN_URL:-$DEFAULT_PLAYER_SKIN_URL}?v=${CACHE_BUSTER}' alt='Admin Skin' class='admin-skin-icon'><div class='admin-text-details'><p><strong class='admin-player-name'>${SITE_OWNER_NAME}</strong></p><div class='contact-links'><a href='mailto:${SITE_OWNER_EMAIL}' class='contact-button' data-title='E-Mail: ${SITE_OWNER_EMAIL}'>Email</a></div></div></div></div>"
    else
        for i in "${!ADMIN_NAME[@]}"; do
            local name="${ADMIN_NAME[$i]}"; local skin_url="${ADMIN_SKIN_URL[$i]:-$DEFAULT_PLAYER_SKIN_URL}"; local email="${ADMIN_EMAIL[$i]}"; local discord="${ADMIN_DISCORD[$i]}"; local matrix="${ADMIN_MATRIX[$i]}"; local steam="${ADMIN_STEAM[$i]}"; local teamspeak="${ADMIN_TEAMSPEAK[$i]}"; local mumble="${ADMIN_MUMBLE[$i]}"
            local contact_links_html=""
            [ -n "$email" ]     && contact_links_html+="<a href='mailto:${email}' class='contact-button' data-title='E-Mail: ${email}'>Email</a>"
            [ -n "$discord" ]   && contact_links_html+="<a href='https://discordapp.com/users/${discord}' target='_blank' rel='noopener noreferrer' class='contact-button' data-title='Discord: ${discord}'>Discord</a>"
            [ -n "$matrix" ]    && contact_links_html+="<a href='https://matrix.to/#/${matrix}' target='_blank' rel='noopener noreferrer' class='contact-button' data-title='Matrix: ${matrix}'>Matrix</a>"
            [ -n "$steam" ]     && contact_links_html+="<a href='${steam}' target='_blank' rel='noopener noreferrer' class='contact-button' data-title='Steam Profil'>Steam</a>"
            [ -n "$teamspeak" ] && contact_links_html+="<a href='ts3server://${teamspeak}' class='contact-button' data-title='Teamspeak: ${teamspeak}'>Teamspeak</a>"
            [ -n "$mumble" ]    && contact_links_html+="<a href='mumble://${mumble}' class='contact-button' data-title='Mumble: ${mumble}'>Mumble</a>"
            ADMIN_BOXES_HTML+="<div class='admin-box'><div class='admin-contact-block'><img src='/${skin_url}?v=${CACHE_BUSTER}' alt='Admin Skin ${name}' class='admin-skin-icon' onerror=\"this.onerror=null;this.src='/${DEFAULT_PLAYER_SKIN_URL}?v=${CACHE_BUSTER}';\"><div class='admin-text-details'><p><strong class='admin-player-name'>${name}</strong></p></div></div>"; if [ -n "$contact_links_html" ]; then ADMIN_BOXES_HTML+="<div class='contact-links'>${contact_links_html}</div>"; fi; ADMIN_BOXES_HTML+="</div>"
        done
    fi
    
    local map_sub_info_link=""
    if [ -f "$unknown_nodes_abs_path" ]; then
        map_sub_info_link="<span class='map-file-link'><a href='/${web_unknown_nodes_rel_path}?v=${CACHE_BUSTER}' target='_blank'>Fehlende Map-Nodes</a></span>"
    else
        map_sub_info_link="<span class='map-file-link'>&nbsp;</span>"
    fi

    {
        generate_html_header "Welt: ${WORLD_DISPLAY_NAME_PAGE}" ""
        
        local temp_body_file
        temp_body_file=$(mktemp)
        render_template "${TEMPLATE_DIR_PATH}/world_detail_page.template" "$temp_body_file" \
            "WORLD_DISPLAY_NAME_PAGE" "$WORLD_DISPLAY_NAME_PAGE" \
            "current_world_key" "$current_world_key" \
            "STATUS_TEXT_FALLBACK_PAGE" "$STATUS_TEXT_FALLBACK_PAGE" \
            "MT_GAMEID" "$MT_GAMEID" \
            "creative_text_class" "$creative_text_class" \
            "creative_text" "$creative_text" \
            "damage_text_class" "$damage_text_class" \
            "damage_text" "$damage_text" \
            "SERVER_ADDRESS_PAGE" "$SERVER_ADDRESS_PAGE" \
            "SERVER_PORT_PAGE" "$SERVER_PORT_PAGE" \
            "SERVER_ACCESS_INFO_PAGE" "$SERVER_ACCESS_INFO_PAGE" \
            "ADMIN_BOXES_HTML" "$ADMIN_BOXES_HTML" \
            "web_online_status_rel_path" "$web_online_status_rel_path" \
            "web_last_update_rel_path" "$web_last_update_rel_path" \
            "CACHE_BUSTER" "$CACHE_BUSTER" \
            "web_players_txt_rel_path" "$web_players_txt_rel_path" \
            "MAP_WIDTH" "$MAP_WIDTH" \
            "MAP_HEIGHT" "$MAP_HEIGHT" \
            "MAP_EXTENT_MIN_X" "$MAP_EXTENT_MIN_X" \
            "MAP_EXTENT_MIN_Z" "$MAP_EXTENT_MIN_Z" \
            "MAP_EXTENT_WIDTH" "$MAP_EXTENT_WIDTH" \
            "MAP_EXTENT_HEIGHT" "$MAP_EXTENT_HEIGHT" \
            "DEFAULT_PLAYER_SKIN_URL" "$DEFAULT_PLAYER_SKIN_URL" \
            "web_map_preview_rel_path" "$web_map_preview_rel_path" \
            "web_weather_txt_rel_path" "$web_weather_txt_rel_path" \
            "WORLD_LONG_DESCRIPTION_PAGE" "$WORLD_LONG_DESCRIPTION_PAGE" \
            "WORLD_GAME_RULES_PAGE" "$WORLD_GAME_RULES_PAGE" \
            "web_unknown_nodes_rel_path" "$web_unknown_nodes_rel_path" \
            "ARCHIVE_HTML" "$ARCHIVE_HTML" \
            "MODS_HTML" "$MODS_HTML" \
            "map_sub_info_link" "$map_sub_info_link"
        cat "$temp_body_file"
        rm "$temp_body_file"

        generate_html_footer
    } > "$detail_page_file"
    
    if [ $? -eq 0 ]; then log_message "Detailseite ${WORLD_DISPLAY_NAME_PAGE} erstellt."; else log_message "FEHLER Detailseite ${WORLD_DISPLAY_NAME_PAGE}."; fi
}


# === Hauptlogik für Webseitengenerierung ===
exec 200>"$LOCK_FILE"
flock -n 200 || { echo "$(date '+%Y-%m-%d %H:%M:%S') - Script ${SCRIPT_BASENAME}.sh ist bereits aktiv (Lock: ${LOCK_FILE}). Beende." | tee -a "$LOG_FILE"; exit 1; }
trap 'rm -f "$LOCK_FILE"; log_message "Script ${SCRIPT_BASENAME}.sh beendet."' EXIT
mkdir -p "$LOG_DIR_BASE"; mkdir -p "${WEB_ROOT_PATH}"
log_message "Script ${SCRIPT_BASENAME}.sh gestartet."

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

DEFAULT_MINETEST_WORLD_DATA_DIR_FOR_CONF="${MINETESTMAPPER_WORLD_DATA_BASE_PATH}${DEFAULT_WORLD_NAME_KEY}"
if [ -d "$DEFAULT_MINETEST_WORLD_DATA_DIR_FOR_CONF" ] && [ -f "${DEFAULT_MINETEST_WORLD_DATA_DIR_FOR_CONF}/world.mt" ]; then
    if [ ! -f "${DEFAULT_MINETEST_WORLD_DATA_DIR_FOR_CONF}/web.conf" ]; then log_message "Erstelle Beispiel web.conf für '${DEFAULT_WORLD_NAME_KEY}'..."; create_placeholder_web_conf "${DEFAULT_MINETEST_WORLD_DATA_DIR_FOR_CONF}/web.conf"; fi; fi
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>"

log_message "Starte Generierung der statischen Webseiten-Dateien..."
log_message "Kopiere Webseiten-Assets (Bilder, Icons)..."
WEB_IMAGES_TARGET_DIR="${WEB_ROOT_PATH}/images"
WEB_PLAYER_IMAGES_TARGET_DIR="${WEB_IMAGES_TARGET_DIR}/players"
SOURCE_IMAGES_DIR="${WEB_CONTENT_BASE_PATH}/${WEB_CONTENT_IMAGES_SOURCE_SUBDIR:-images_source}" 
SOURCE_PLAYER_IMAGES_TARGET_DIR="${SOURCE_IMAGES_DIR}/players" 
mkdir -p "$WEB_IMAGES_TARGET_DIR"; mkdir -p "$WEB_PLAYER_IMAGES_TARGET_DIR"
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; shopt -u nullglob; log_message "Allgemeine Bilder kopiert/aktualisiert.";
else log_message "WARNUNG: Quellverz. allgemeine Bilder nicht gefunden: ${SOURCE_IMAGES_DIR}"; fi
if [ -d "$SOURCE_PLAYER_IMAGES_DIR" ]; then shopt -s nullglob; for src_file in "$SOURCE_PLAYER_IMAGES_DIR"/*.png ; do [ -f "$src_file" ] && cp -u "$src_file" "$WEB_PLAYER_IMAGES_TARGET_DIR/"; done; shopt -u nullglob; log_message "Spieler-Skins kopiert/aktualisiert.";
else log_message "WARNUNG: Quellverz. Spieler-Skins nicht gefunden: ${SOURCE_PLAYER_IMAGES_DIR}"; fi

if [ -n "$STATIC_BANNER_FILENAME" ]; then ACTUAL_BANNER_IMG_URL_PATH="/images/${STATIC_BANNER_FILENAME}"; if [ ! -f "${WEB_ROOT_PATH}${ACTUAL_BANNER_IMG_URL_PATH}" ]; then log_message "WARNUNG: Statisches Banner '${STATIC_BANNER_FILENAME}' nicht in '${WEB_ROOT_PATH}/images/' gefunden. Fallback: ${FALLBACK_BANNER_IMG_URL}"; ACTUAL_BANNER_IMG_URL_PATH="${FALLBACK_BANNER_IMG_URL}"; else log_message "Verwende statisches Banner: ${ACTUAL_BANNER_IMG_URL_PATH}"; fi
else log_message "Kein STATIC_BANNER_FILENAME, verwende Fallback: ${FALLBACK_BANNER_IMG_URL}"; ACTUAL_BANNER_IMG_URL_PATH="${FALLBACK_BANNER_IMG_URL}"; fi

generate_css; generate_homepage; generate_impressum; generate_downloads_page; generate_datenschutz_page; generate_worlds_overview

processed_world_count=0; shopt -s nullglob
site_world_key_dirs_detail_loop=("${MINETESTMAPPER_WORLD_DATA_BASE_PATH}"/*/)
shopt -u nullglob
if [ ${#site_world_key_dirs_detail_loop[@]} -gt 0 ]; then
    for site_world_data_dir_item in "${site_world_key_dirs_detail_loop[@]}"; do 
        site_current_world_key_item=$(basename "$site_world_data_dir_item")
        if [ -z "$site_current_world_key_item" ]; then log_message "WARNUNG: Leerer Welt-Schlüssel '${site_world_data_dir_item}'."; continue; fi
        if [ -f "${site_world_data_dir_item}world.mt" ] && [ -f "${site_world_data_dir_item}web.conf" ]; then
            target_web_world_dir_item="${WEB_ROOT_PATH}/${WEB_MAPS_BASE_SUBDIR}/${site_current_world_key_item}"
            mkdir -p "$target_web_world_dir_item"
            touch "${target_web_world_dir_item}/areas.txt"; touch "${target_web_world_dir_item}/players.txt"; touch "${target_web_world_dir_item}/weather.txt"
            log_message "Platzhalter areas.txt, players.txt, weather.txt für Welt '${site_current_world_key_item}' sichergestellt."
            generate_world_detail_page "$site_current_world_key_item"; processed_world_count=$((processed_world_count + 1))
        else log_message "WARNUNG: Für ${site_world_data_dir_item} (Key: ${site_current_world_key_item}) fehlt world.mt oder web.conf.";fi
    done
    log_message "${processed_world_count} Welt-Detailseiten generiert/aktualisiert."
else log_message "Keine Weltverzeichnisse in ${MINETESTMAPPER_WORLD_DATA_BASE_PATH} gefunden."; fi
log_message "Webseiten-Generierung abgeschlossen."
exit 0
