#!/bin/bash

# === Default values ===
TARGET_PATH=""
FLAGS=()
DOWNLOAD_ALL=true

# === Download links ===
DEPTH_URL="https://syn-mediverse.cs.uni-freiburg.de/datasets/depth.zip"
IMG_URL="https://syn-mediverse.cs.uni-freiburg.de/datasets/img.zip"
LABEL_URL="https://syn-mediverse.cs.uni-freiburg.de/datasets/label.zip"

# === Helper function ===
download_and_extract() {
    local name="$1"
    local url="$2"
    echo "Downloading $name.zip..."
    wget -q --show-progress "$url" -O "$name.zip"
    echo "Extracting $name.zip..."
    unzip -q "$name.zip" -d .
    rm "$name.zip"
    echo "$name.zip processed."
}

# === Parse arguments ===
while [[ $# -gt 0 ]]; do
    case "$1" in
        --depth)
            FLAGS+=("depth")
            DOWNLOAD_ALL=false
            shift
            ;;
        --img)
            FLAGS+=("img")
            DOWNLOAD_ALL=false
            shift
            ;;
        --label)
            FLAGS+=("label")
            DOWNLOAD_ALL=false
            shift
            ;;
        --all)
            DOWNLOAD_ALL=true
            shift
            ;;
        *)
            # Assume this is the target path
            TARGET_PATH="$1"
            shift
            ;;
    esac
done

# === Ensure target path is set ===
if [[ -z "$TARGET_PATH" ]]; then
    echo "Usage: $0 /target/path [--depth] [--img] [--label] [--all]"
    exit 1
fi

# === Create target directory ===
mkdir -p "$TARGET_PATH/syn-mediverse_v1.0"
cd "$TARGET_PATH/syn-mediverse_v1.0" || exit 1

# === Handle downloads ===
if $DOWNLOAD_ALL || [[ " ${FLAGS[@]} " =~ " depth " ]]; then
    download_and_extract "depth" "$DEPTH_URL"
fi

if $DOWNLOAD_ALL || [[ " ${FLAGS[@]} " =~ " img " ]]; then
    download_and_extract "img" "$IMG_URL"
fi

if $DOWNLOAD_ALL || [[ " ${FLAGS[@]} " =~ " label " ]]; then
    download_and_extract "label" "$LABEL_URL"
fi

echo "✅ Done. Files extracted to: $TARGET_PATH/syn-mediverse_v1.0"
