#!/usr/bin/env bash
# Strip Rust-related files from a pizzarr source tree in place.
#
# The CRAN tier ships pure R. This script removes src/, the configure
# scripts, extendr machinery, and related metadata so what's left is
# equivalent to what would be shipped to CRAN.
#
# Used by:
#   tools/cran-build.sh        — on a tmp copy, before R CMD build
#   .github/workflows/rhub.yaml — on the GHA checkout, before run-check
#
# Usage:
#   bash tools/cran-strip.sh [pkg-dir]
#
# Default pkg-dir is $PWD. The script expects DESCRIPTION and NAMESPACE
# in pkg-dir. Uses only POSIX grep/awk — no sed -i portability traps.

set -euo pipefail

PKG_DIR="${1:-$PWD}"
cd "$PKG_DIR"

if [ ! -f DESCRIPTION ] || [ ! -f NAMESPACE ]; then
  echo "ERROR: $PKG_DIR is not an R package root (missing DESCRIPTION or NAMESPACE)" >&2
  exit 1
fi

# Remove Rust/extendr source and build hooks.
rm -rf src/
rm -f configure configure.win
rm -f cleanup cleanup.win
rm -f tools/config.R tools/msrv.R

# DESCRIPTION: drop the multi-line SystemRequirements field and the
# Config/rextendr/version line.
awk '
  /^SystemRequirements:/ { skip = 1; next }
  skip && /^[[:space:]]/ { next }
  { skip = 0; print }
' DESCRIPTION > DESCRIPTION.tmp
mv DESCRIPTION.tmp DESCRIPTION

grep -v '^Config/rextendr/version:' DESCRIPTION > DESCRIPTION.tmp
mv DESCRIPTION.tmp DESCRIPTION

# Replace extendr-wrappers.R with a minimal stub. No DLL exists on the
# CRAN tier, so the only surviving export is zarrs_compiled_features(),
# which errors; is_zarrs_available() catches that and returns FALSE.
if [ -f R/extendr-wrappers.R ]; then
  cat > R/extendr-wrappers.R << 'WRAPPER_EOF'
# Generated by extendr: Do not edit by hand
# Stub for CRAN tier (no Rust shared library)

# nolint start

#' @usage NULL
NULL

#' Return compiled zarrs feature flags as a character vector.
#'
#' On the CRAN tier (pure R), this function always errors. The error
#' is caught by \code{is_zarrs_available()}, which returns \code{FALSE}.
#' @export
zarrs_compiled_features <- function() {
  stop("zarrs backend not compiled (pure R install)")
}

# nolint end
WRAPPER_EOF
fi

# NAMESPACE: drop useDynLib, drop exports of Rust-only zarrs_* functions.
# zarrs_compiled_features is the only zarrs_ export that survives.
grep -v '^useDynLib' NAMESPACE > NAMESPACE.tmp
mv NAMESPACE.tmp NAMESPACE

awk '
  /^export\(zarrs_/ && !/zarrs_compiled_features/ { next }
  { print }
' NAMESPACE > NAMESPACE.tmp
mv NAMESPACE.tmp NAMESPACE

# Remove Rd pages for Rust-only functions.
rm -f man/zarrs_close_store.Rd man/zarrs_create_array.Rd \
      man/zarrs_get_subset.Rd man/zarrs_node_exists.Rd \
      man/zarrs_open_array_metadata.Rd man/zarrs_runtime_info.Rd \
      man/zarrs_set_codec_concurrent_target.Rd \
      man/zarrs_set_http_batch_range_requests.Rd \
      man/zarrs_set_nthreads.Rd man/zarrs_set_subset.Rd

# Include vignettes in the shipped tarball. They are .Rbuildignored on
# the r-universe tier, so strip that line for the CRAN tier.
if [ -f .Rbuildignore ]; then
  grep -v '^\^vignettes\$' .Rbuildignore > .Rbuildignore.tmp
  mv .Rbuildignore.tmp .Rbuildignore
fi

# Drop tools/ if now empty.
rmdir tools 2>/dev/null || true
