#!/bin/sh

set -ex

# Check if the archive already exists
if [ -f "src/duckdb.tar" ]; then
  echo "duckdb.tar already exists. Skipping packing."
  exit 0
fi

# Use gtar if available (e.g., on macOS via Homebrew), otherwise tar.
# gtar avoids storing Apple-specific extended attributes that cause
# warnings when extracting on Linux.
TAR="$(which gtar 2>/dev/null || which tar)"

# Pack the vendored sources into an uncompressed tar, which configure
# unpacks again; the .tar.gz that R CMD build writes compresses it.
# Why: handbook/build/source-build/README.md.
if [ -d .git ]; then git clean -fdx src; fi
cd src
$TAR cf duckdb.tar duckdb
rm -rf duckdb
