#!/bin/bash

BASE_URL="https://dirs.entitybtw.ru/zyxel/zypkg-repo/"
DUMP_DIR="./dump"
mkdir -p "$DUMP_DIR"

VISITED_FILE="$DUMP_DIR/.visited"

fetch_dir() {
    local url="$1"
    local rel_path="${url#"$BASE_URL"}"
    
    while [[ "$rel_path" =~ \.\./ ]]; do
        rel_path=$(echo "$rel_path" | sed 's/\.\.\/../')
    done
    rel_path=$(echo "$rel_path" | sed 's|/\./|/|g; s|//\+|/|g; s|/$||')
    
    if grep -Fxq "$rel_path" "$VISITED_FILE" 2>/dev/null; then
        return
    fi
    echo "$rel_path" >> "$VISITED_FILE"
    
    local local_path="$DUMP_DIR/$rel_path"
    mkdir -p "$local_path"
    
    # Берём все ссылки на файлы и папки
    local links=$(curl -s --max-time 10 "$url" | grep -oP '(?<=href=")[^"]+')
    
    for entry in $links; do
        [[ "$entry" == "../" ]] && continue
        if [[ "$entry" == */ ]]; then
            entry="${entry%/}"
            fetch_dir "${BASE_URL}${rel_path:+$rel_path/}$entry/"
        else
            file_url="${BASE_URL}${rel_path:+$rel_path/}$entry"
            echo "downloading: $file_url"
            curl -s --max-time 20 -o "$local_path/$entry" "$file_url"
        fi
    done
}

rm -f "$VISITED_FILE"
fetch_dir "$BASE_URL"
