#!/usr/bin/env nu

# Distribution Packaging Script
# Nushell version of dist-pack.sh
# Creates tar.gz archives for project distribution

def main [os_arch?: string] {
    # Validate input parameter
    if ($os_arch | is-empty) {
        print $"(ansi red)Error:(ansi reset) No OS ARCH provided (example: linux-amd64)"
        exit 1
    }

    # Configuration - following project's configuration-driven approach
    let target_path = $"dist/website-($os_arch).tar.gz"
    let target_list = "scripts/dist-list-files"

    # Ensure dist directory exists
    mkdir dist

    # Check if file list exists
    if not ($target_list | path exists) {
        print $"(ansi red)Error:(ansi reset) File list not found: ($target_list)"
        exit 1
    }

    # Read file list and filter out empty lines and comments
    let files_to_pack = (
        open $target_list
        | lines
        | where ($it | str trim | str length) > 0
        | where not ($it | str starts-with "#")
    )

    print $"(ansi blue)📦 Packing files from ($target_list)...(ansi reset)"
    print $"(ansi blue)🎯 Target: ($target_path)(ansi reset)"

    # Create the archive
    try {
        tar --exclude='.DS_Store' -czf $target_path -T $target_list
        print "--------------------------------------------------------------"
        print $"(ansi green)✅ ($target_list) PACKED IN ($target_path)(ansi reset)"

        # Show archive info
        let archive_size = (ls $target_path | get size | first)
        print $"(ansi blue)📊 Archive size: ($archive_size)(ansi reset)"

    } catch {
        print $"(ansi red)❌ Failed to create archive: ($target_path)(ansi reset)"
        exit 1
    }
}
