# Distribution Module - Collection and Packaging
# Commands for collecting built plugins and creating distribution packages

# 📦 COLLECTION COMMANDS

# Collect built plugins for distribution (UPDATED: now includes nushell + system plugins)
[no-cd]
collect:
    @echo "📦 Collecting plugins for distribution (nushell + all plugins)..."
    @{{justfile_directory()}}/scripts/run.sh collect_full_binaries.nu --release --force

# List available built plugins
[no-cd]
collect-list:
    @echo "📋 Available built plugins:"
    @{{justfile_directory()}}/scripts/run.sh collect_install.nu --list

# List available platforms for collection
[no-cd]
collect-platforms:
    @echo "📊 Available platforms for collection:"
    @{{justfile_directory()}}/scripts/run.sh collect_install.nu --list-platforms

# Collect specific platform (UPDATED: now includes nushell + system plugins)
[no-cd]
collect-platform PLATFORM:
    @echo "📦 Collecting {{PLATFORM}} plugins (nushell + all plugins)..."
    @{{justfile_directory()}}/scripts/run.sh collect_full_binaries.nu --platform {{PLATFORM}} --release --force

# Collect all available platforms (UPDATED: now includes nushell + system plugins)
[no-cd]
collect-all:
    @echo "🌍 Collecting all platforms (nushell + all plugins)..."
    @{{justfile_directory()}}/scripts/run.sh collect_full_binaries.nu --all-platforms --release --force

# Force collection (overwrite existing)
[no-cd]
collect-force:
    @echo "📦 Force collecting plugins (nushell + all plugins)..."
    @{{justfile_directory()}}/scripts/run.sh collect_full_binaries.nu --release --force

# Legacy: Collect only custom plugins (without nushell/system plugins)
[no-cd]
collect-plugins-only-legacy:
    @echo "📦 Collecting custom plugins only (legacy mode)..."
    @{{justfile_directory()}}/scripts/run.sh collect_install.nu --force

# 📋 PACKAGING COMMANDS

# Create distribution package
[no-cd]
pack:
    @echo "📦 Creating distribution package..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --force

# Show what would be packaged
[no-cd]
pack-list:
    @echo "📋 Files that would be packaged:"
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --list

# List available platforms for packaging
[no-cd]
pack-platforms:
    @echo "📊 Available platforms for packaging:"
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --list-platforms

# Package specific platform
[no-cd]
pack-platform PLATFORM:
    @echo "📦 Packaging {{PLATFORM}}..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --platform {{PLATFORM}}

# Package all platforms
[no-cd]
pack-all:
    @echo "🌍 Packaging all platforms..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --all-platforms

# Package with checksums
[no-cd]
pack-checksums:
    @echo "🔐 Packaging with checksums..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --all-platforms --checksums

# Force packaging (overwrite existing)
[no-cd]
pack-force:
    @echo "📦 Force packaging..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --force

# Package to custom output directory
[no-cd]
pack-output OUTPUT:
    @echo "📦 Packaging to {{OUTPUT}}..."
    @{{justfile_directory()}}/scripts/run.sh pack_dist.nu --output {{OUTPUT}}

# 🚀 RELEASE WORKFLOWS

# Complete cross-platform build and package workflow
[no-cd]
release-cross:
    @echo "🚀 Complete cross-platform release workflow..."
    @just validate-nushell
    @just build-cross-all
    @just collect-all
    @just pack-checksums

# Quick release for current platform
[no-cd]
release-quick:
    @echo "🚀 Quick release for current platform..."
    @just validate-nushell
    @just build
    @just collect
    @just pack

# Release with custom target
[no-cd]
release-target TARGET:
    @echo "🚀 Release for {{TARGET}}..."
    @just validate-nushell
    @just build-cross {{TARGET}}
    @just collect-platform {{TARGET}}
    @just pack-platform {{TARGET}}

# 📂 DIRECTORY MANAGEMENT

# Show distribution directory contents
[no-cd]
distro-show:
    @echo "📂 Distribution directory contents:"
    @if [ -d "distribution" ]; then \
        find distribution -type f -exec ls -lh {} \; | head -20; \
    else \
        echo "No distribution directory found"; \
    fi

# Show archive directory contents
[no-cd]
distro-archives:
    @echo "📂 Archive directory contents:"
    @if [ -d "bin_archives" ]; then \
        ls -lh bin_archives/; \
    else \
        echo "No bin_archives directory found"; \
    fi

# Clean distribution directories
[no-cd]
distro-clean:
    @echo "🧹 Cleaning distribution directories..."
    @rm -rf distribution bin_archives
    @echo "✅ Cleaned distribution and bin_archives directories"

# Archive size statistics
[no-cd]
distro-stats:
    @echo "📊 Distribution Statistics:"
    @if [ -d "bin_archives" ]; then \
        echo "Archive sizes:"; \
        du -h bin_archives/* 2>/dev/null | sort -h || echo "No archives found"; \
        echo ""; \
        echo "Total archive size:"; \
        du -sh bin_archives 2>/dev/null || echo "No archives directory"; \
    else \
        echo "No bin_archives directory found"; \
    fi

# 🔍 VERIFICATION COMMANDS

# Verify checksums
[no-cd]
distro-verify:
    @echo "🔍 Verifying checksums..."
    @if [ -f "bin_archives/checksums.txt" ]; then \
        cd bin_archives && sha256sum -c checksums.txt; \
    else \
        echo "No checksums file found"; \
    fi

# Test archive contents
[no-cd]
distro-test ARCHIVE:
    @echo "🔍 Testing archive contents: {{ARCHIVE}}"
    @if [ -f "bin_archives/{{ARCHIVE}}" ]; then \
        case "{{ARCHIVE}}" in \
            *.tar.gz) tar -tzf "bin_archives/{{ARCHIVE}}" ;; \
            *.zip) unzip -l "bin_archives/{{ARCHIVE}}" ;; \
            *) echo "Unsupported archive format" ;; \
        esac; \
    else \
        echo "Archive not found: bin_archives/{{ARCHIVE}}"; \
    fi

# Test all archives
[no-cd]
distro-test-all:
    @echo "🔍 Testing all archives..."
    @for archive in bin_archives/*.tar.gz bin_archives/*.zip; do \
        if [ -f "$$archive" ]; then \
            echo "Testing $$(basename "$$archive")..."; \
            case "$$archive" in \
                *.tar.gz) tar -tzf "$$archive" >/dev/null && echo "✅ OK" || echo "❌ FAILED" ;; \
                *.zip) unzip -t "$$archive" >/dev/null && echo "✅ OK" || echo "❌ FAILED" ;; \
            esac; \
        fi; \
    done

# 📊 DISTRIBUTION INFORMATION

# Show distribution help
[no-cd]
distro-help:
    @echo "📦 Distribution Module Help"
    @echo "=========================="
    @echo ""
    @echo "COLLECTION (UPDATED: now includes nushell + system plugins):"
    @echo "  collect                  - Collect nushell + all plugins for distribution"
    @echo "  collect-list             - List available built plugins"
    @echo "  collect-platforms        - List available platforms"
    @echo "  collect-platform PLATFORM - Collect nushell + plugins for specific platform"
    @echo "  collect-all              - Collect nushell + plugins for all platforms"
    @echo "  collect-force            - Force collection (overwrite)"
    @echo "  collect-plugins-only-legacy - Legacy: collect only custom plugins"
    @echo ""
    @echo "PACKAGING:"
    @echo "  pack                     - Create distribution package"
    @echo "  pack-list                - Show what would be packaged"
    @echo "  pack-platforms           - List packaging platforms"
    @echo "  pack-platform PLATFORM  - Package specific platform"
    @echo "  pack-all                 - Package all platforms"
    @echo "  pack-checksums           - Package with checksums"
    @echo "  pack-force               - Force packaging"
    @echo "  pack-output OUTPUT       - Package to custom directory"
    @echo ""
    @echo "RELEASE WORKFLOWS:"
    @echo "  release-cross            - Complete cross-platform release"
    @echo "  release-quick            - Quick release for current platform"
    @echo "  release-target TARGET    - Release for specific target"
    @echo ""
    @echo "DIRECTORY MANAGEMENT:"
    @echo "  distro-show              - Show distribution contents"
    @echo "  distro-archives          - Show archive contents"
    @echo "  distro-clean             - Clean distribution directories"
    @echo "  distro-stats             - Show distribution statistics"
    @echo ""
    @echo "VERIFICATION:"
    @echo "  distro-verify            - Verify checksums"
    @echo "  distro-test ARCHIVE      - Test specific archive"
    @echo "  distro-test-all          - Test all archives"
    @echo ""
    @echo "EXAMPLES:"
    @echo "  just collect-platform linux-amd64"
    @echo "  just pack-checksums"
    @echo "  just release-cross"
    @echo "  just distro-test linux-amd64-nushell-plugins.tar.gz"
