#!/bin/bash
# validate-dos.sh - Validate batch files for DOS 6.22 compatibility
# Run before deploying to DOS machines

ERRORS=0

echo "DOS 6.22 Batch File Validator"
echo "=============================="
echo ""

for file in *.BAT; do
    [ -f "$file" ] || continue
    echo "Checking: $file"

    # Rule 1: No CALL :label
    if grep -qE "CALL\s+:" "$file" 2>/dev/null; then
        echo "  ERROR: CALL :label subroutine found (Rule 1)"
        ERRORS=$((ERRORS+1))
    fi

    # Rule 2: No %DATE% or %TIME%
    if grep -qE "%DATE%|%TIME%" "$file" 2>/dev/null; then
        echo "  ERROR: %DATE% or %TIME% found (Rule 2)"
        ERRORS=$((ERRORS+1))
    fi

    # Rule 3: No square brackets in ECHO
    if grep -qE "ECHO.*\[" "$file" 2>/dev/null; then
        # Exclude REM lines
        if grep -vE "^REM" "$file" | grep -qE "ECHO.*\[" 2>/dev/null; then
            echo "  ERROR: Square brackets in ECHO (Rule 3)"
            ERRORS=$((ERRORS+1))
        fi
    fi

    # Rule 6: No 2>NUL
    if grep -qE "2>NUL|2> *NUL" "$file" 2>/dev/null; then
        echo "  ERROR: 2>NUL stderr redirect found (Rule 6)"
        ERRORS=$((ERRORS+1))
    fi

    # Rule 8: No :EOF
    if grep -qE "GOTO\s+:?EOF" "$file" 2>/dev/null; then
        echo "  ERROR: :EOF label reference found (Rule 8)"
        ERRORS=$((ERRORS+1))
    fi

    # Rule 15: No SETLOCAL/ENDLOCAL
    if grep -qiE "SETLOCAL|ENDLOCAL" "$file" 2>/dev/null; then
        echo "  ERROR: SETLOCAL/ENDLOCAL found (Rule 15)"
        ERRORS=$((ERRORS+1))
    fi

    # Rule 19: Check for LF-only line endings (no CR)
    if file "$file" | grep -qv "CRLF"; then
        if file "$file" | grep -q "ASCII"; then
            echo "  WARNING: May have Unix line endings (Rule 19)"
        fi
    fi

    # Rule 20: Trailing spaces in SET statements
    if grep -qE "^SET [A-Za-z_]+=[^ ]* +$" "$file" 2>/dev/null; then
        echo "  ERROR: Trailing space in SET statement (Rule 20)"
        grep -nE "^SET [A-Za-z_]+=[^ ]* +$" "$file" | sed 's/^/    Line /'
        ERRORS=$((ERRORS+1))
    fi

    # Rule 20: Any trailing whitespace
    if grep -qE " +$" "$file" 2>/dev/null; then
        TRAILING=$(grep -cE " +$" "$file")
        echo "  WARNING: $TRAILING lines have trailing whitespace (Rule 20)"
    fi

    # Check filename is 8.3
    BASENAME=$(basename "$file" .BAT)
    if [ ${#BASENAME} -gt 8 ]; then
        echo "  ERROR: Filename exceeds 8 characters (Rule 14)"
        ERRORS=$((ERRORS+1))
    fi

done

echo ""
echo "=============================="
if [ $ERRORS -eq 0 ]; then
    echo "All checks passed!"
else
    echo "Found $ERRORS error(s)"
fi
echo ""

# Auto-fix option
if [ "$1" = "--fix" ]; then
    echo "Applying fixes..."
    for file in *.BAT; do
        [ -f "$file" ] || continue
        # Strip trailing whitespace
        sed -i '' 's/[[:space:]]*$//' "$file"
        # Convert to DOS line endings
        perl -pi -e 's/\r?\n/\r\n/g' "$file"
        echo "  Fixed: $file"
    done
    echo "Done. Re-run without --fix to verify."
fi

exit $ERRORS
