#!/bin/bash
set -euo pipefail

# Script to add execute permission for all .sh and .py files under tools/
# Usage: ./tools/chmod-scripts.sh

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TOOLS_DIR="$ROOT_DIR/tools"

if [ ! -d "$TOOLS_DIR" ]; then
  echo "Error: tools directory not found at $TOOLS_DIR" >&2
  exit 1
fi

echo "Root directory: $ROOT_DIR"
echo "Tools directory: $TOOLS_DIR"
echo "Adding execute permission to all .sh and .py files under $TOOLS_DIR..."
echo ""

echo "Processing .sh files..."
find "$TOOLS_DIR" -type f -name "*.sh" -exec chmod +x {} +

echo "Processing .py files..."
find "$TOOLS_DIR" -type f -name "*.py" -exec chmod +x {} +

echo ""
echo "Done! Execute permissions added to all .sh and .py files under tools/"
echo ""

# Count and verify files
echo "Verification:"
sh_count=$(find "$TOOLS_DIR" -type f -name "*.sh" 2>/dev/null | wc -l)
py_count=$(find "$TOOLS_DIR" -type f -name "*.py" 2>/dev/null | wc -l)

echo "  Total .sh files found: $sh_count"
echo "  Total .py files found: $py_count"

if [ "$sh_count" -gt 0 ] || [ "$py_count" -gt 0 ]; then
  echo "  ✓ Execute permissions updated successfully!"
else
  echo "  ⚠ Warning: No .sh or .py files found"
fi
