#!/usr/bin/env bash
# Descarga las libs precompiladas de DuckDB para linux y windows en
# cpp/vendor/duckdb/{linux,windows}/. El header duckdb.h vive en include/
# y SI esta versionado.
#
# Uso:
#   ./download_duckdb.sh           # ambas plataformas
#   ./download_duckdb.sh linux     # solo linux
#   ./download_duckdb.sh windows   # solo windows

set -euo pipefail

VERSION="v1.1.3"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TMP="$(mktemp -d)"
trap "rm -rf $TMP" EXIT

target="${1:-all}"

dl_linux() {
    echo "[duckdb] downloading linux libs..."
    curl -sL -o "$TMP/lin.zip" \
        "https://github.com/duckdb/duckdb/releases/download/${VERSION}/libduckdb-linux-amd64.zip"
    mkdir -p "$HERE/linux"
    unzip -o "$TMP/lin.zip" -d "$TMP/lin/" > /dev/null
    cp "$TMP/lin/libduckdb.so" "$HERE/linux/"
    echo "[duckdb] -> $HERE/linux/libduckdb.so ($(du -h "$HERE/linux/libduckdb.so" | cut -f1))"
}

dl_windows() {
    echo "[duckdb] downloading windows libs..."
    curl -sL -o "$TMP/win.zip" \
        "https://github.com/duckdb/duckdb/releases/download/${VERSION}/libduckdb-windows-amd64.zip"
    mkdir -p "$HERE/windows"
    unzip -o "$TMP/win.zip" -d "$TMP/win/" > /dev/null
    cp "$TMP/win/duckdb.dll" "$HERE/windows/"
    cp "$TMP/win/duckdb.lib" "$HERE/windows/"
    echo "[duckdb] -> $HERE/windows/duckdb.dll ($(du -h "$HERE/windows/duckdb.dll" | cut -f1))"
}

case "$target" in
    linux)   dl_linux ;;
    windows) dl_windows ;;
    all)     dl_linux; dl_windows ;;
    *)       echo "Uso: $0 [linux|windows|all]"; exit 1 ;;
esac
