#!/bin/bash

# XML Validation Script for Wasot
# Validates XML files against the schema

show_help() {
    echo "Usage: $0 <xml_file>"
    echo ""
    echo "Validates an XML file against the Wasot schema"
    echo ""
    echo "Arguments:"
    echo "  xml_file    Path to the XML file to validate"
    echo ""
    echo "Examples:"
    echo "  $0 output.xml"
    echo "  $0 examples/demo.xml"
    echo ""
    echo "Options:"
    echo "  -h, --help  Show this help message"
}

# Check for help flag
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
    show_help
    exit 0
fi

# Check if argument is provided
if [ $# -eq 0 ]; then
    echo "Error: No XML file specified"
    echo ""
    show_help
    exit 1
fi

xml_file="$1"

# Check if file exists
if [ ! -f "$xml_file" ]; then
    echo "Error: File '$xml_file' not found"
    exit 1
fi

# Check if schema exists
schema_file="./schema/schema.xsd"
if [ ! -f "$schema_file" ]; then
    echo "Error: Schema file '$schema_file' not found"
    exit 1
fi

echo "Validating '$xml_file' against schema..."

# Run validation
if cargo run validate --schema="$schema_file" "$xml_file"; then
    echo "✓ Validation successful"
    exit 0
else
    echo "✗ Validation failed"
    exit 1
fi
