#!/usr/bin/env bash
# $Id: CreateTestDataArchive 185 2026-08-11 08:48:27Z peter $
#
# CreateTestDataArchive - Create test data archive for dvi3v
#
# Copyright (C) 2026  Peter Felecan
# Copyright (C) 2026  Anthropic AI (Claude Sonnet 5)
#
# Licensed under GNU GPL v3

set -e

SCRIPT_NAME="$(basename "$0")"

show_help() {
    cat << EOF
${SCRIPT_NAME} - Create a test data archive for dvi3v

Copyright (C) 2026 Peter Felecan, Anthropic AI (Claude Sonnet 5)
Licensed under GNU GPL v3

Usage: ${SCRIPT_NAME} [-c CATEGORY] [-h|--help]

Bundles TestResults/ (or, with -c, just one category under it) into a
timestamped dvi3v-TestData-*.tar.xz archive, together with
TestResults/VERSION.org (created from a template on first run if it does
not exist yet). Run from the project root, i.e. the directory that
contains TestResults/.

Options:
  -c CATEGORY     Archive only TestResults/CATEGORY instead of all of
                  TestResults/. CATEGORY is a path relative to
                  TestResults/, e.g. RealWorld/SeamRipper; a '/' in
                  CATEGORY becomes '_' in the archive filename.

  -h, --help      Show this help and exit

Examples:
  # Archive everything under TestResults/
  ${SCRIPT_NAME}

  # Archive only TestResults/RealWorld/SeamRipper
  ${SCRIPT_NAME} -c RealWorld/SeamRipper
EOF
}

# Optional: category parameter
CATEGORY=""
while [ $# -gt 0 ]; do
    case "$1" in
        -h|--help)
            show_help
            exit 0
            ;;
        -c)
            if [ -z "${2:-}" ]; then
                echo "Error: -c requires a CATEGORY argument" >&2
                exit 1
            fi
            CATEGORY="${2}-"
            shift 2
            ;;
        *)
            echo "Error: Unknown option: $1" >&2
            echo "Try '${SCRIPT_NAME} --help' for usage." >&2
            exit 1
            ;;
    esac
done

# Get date and time for archive name
DATETIME=$(date +%Y-%m-%d-%H%M)

ARCHIVE="dvi3v-TestData-$(echo ${CATEGORY} | tr '/' '_')${DATETIME}.tar.xz"

echo "========================================"
echo "Creating Test Data Archive"
echo "========================================"
echo "Date/Time: $DATETIME"
if [ -n "$CATEGORY" ]; then
    echo "Category: ${CATEGORY%%-}"
fi
echo "Archive: $ARCHIVE"
echo ""

# Check if TestResults directory exists
if [ ! -d TestResults ]; then
    echo "Error: TestResults/ directory not found"
    echo "Run this script from the project root directory"
    exit 1
fi

# Check if VERSION.org exists in TestResults
if [ ! -f TestResults/VERSION.org ]; then
    echo "Warning: TestResults/VERSION.org not found"
    echo "Creating template..."
    
    PROJECT_VERSION=$(grep -v '^#' VERSION | head -1 | tr -d ' ')
    
    cat > TestResults/VERSION.org << EOF
# TestResults/VERSION.org
# This file tracks which project version this test data belongs to

- Project Version :: ${PROJECT_VERSION}
- Test Data Date/Time :: ${DATETIME}
- Tests Included ::
  - (List test categories and object counts)
  - (Example: Baseline geometric shapes - 8 objects)
- Notes ::
  - (Add relevant notes about this test batch)
  - (Example: Printed on Prusa XL, 0.6mm nozzle, PETG)
EOF
    
    echo "✓ Created TestResults/VERSION.org - please edit before uploading"
fi

# Create archive
if [ -n "$CATEGORY" ]; then
    # Category-specific archive
    CAT_DIR="TestResults/${CATEGORY%%-}"
    if [ ! -d "$CAT_DIR" ]; then
        echo "Error: Category directory not found: $CAT_DIR"
        exit 1
    fi
    tar cJf "$ARCHIVE" "$CAT_DIR" TestResults/VERSION.org
else
    # Full TestResults
    tar cJf "$ARCHIVE" TestResults/
fi

echo ""
echo "✓ Created: $ARCHIVE"
ls -lh "$ARCHIVE"
echo ""
echo "Archive contains:"
tar tJf "$ARCHIVE" | wc -l | xargs echo "  Total files:"
echo ""
echo "Files by type:"
tar tJf "$ARCHIVE" | grep -E '\.(jpg|png)$' | wc -l | xargs echo "  Images (jpg/png):"
tar tJf "$ARCHIVE" | grep '\.svg$' | wc -l | xargs echo "  SVG outputs:"
tar tJf "$ARCHIVE" | grep '\.org$' | wc -l | xargs echo "  Org files:"
echo ""
echo "This archive contains test execution data."
echo "Upload alongside project version: $(grep -v '^#' VERSION | head -1)"
echo "========================================"
