#!/usr/bin/env bash
# Validate all deployment and automation scripts

set -e

cd "$(dirname "$0")/../.."

echo "=== ✅ Validating All Scripts ==="

ERRORS=0

# Check all deployment scripts are executable
echo "Checking deployment scripts..."
for script in scripts/deployment/*.sh; do
    if [ -f "$script" ]; then
        if [ ! -x "$script" ]; then
            echo "⚠️  Making executable: $script"
            chmod +x "$script"
        fi
    fi
done

# Check all automation scripts are executable
echo "Checking automation scripts..."
for script in scripts/automation/*.sh; do
    if [ -f "$script" ]; then
        if [ ! -x "$script" ]; then
            echo "⚠️  Making executable: $script"
            chmod +x "$script"
        fi
    fi
done

# Validate script syntax
echo "Validating script syntax..."
for script in scripts/**/*.sh; do
    if [ -f "$script" ]; then
        if ! bash -n "$script" 2>&1; then
            echo "❌ Syntax error in: $script"
            ERRORS=$((ERRORS + 1))
        fi
    fi
done

if [ $ERRORS -eq 0 ]; then
    echo "✅ All scripts validated successfully"
    exit 0
else
    echo "❌ Found $ERRORS script errors"
    exit 1
fi
