#!/bin/bash

# Make Scripts Executable
# This script makes all shell scripts in the scripts directory executable

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Logging functions
log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_header() {
    echo -e "${BLUE}=== $1 ===${NC}"
}

print_usage() {
    echo "Usage: $0 [options]"
    echo
    echo "Options:"
    echo "  -v, --verbose    Enable verbose output"
    echo "  -h, --help       Show this help message"
    echo
    echo "This script makes all shell scripts in the scripts directory executable."
}

# Parse command line arguments
VERBOSE=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -h|--help)
            print_usage
            exit 0
            ;;
        *)
            log_error "Unknown option: $1"
            print_usage
            exit 1
            ;;
    esac
done

print_header "Making Scripts Executable"

# Find all shell scripts
SHELL_SCRIPTS=$(find "$SCRIPT_DIR" -type f \( -name "*.sh" -o -name "*.bash" \) 2>/dev/null)

if [ -z "$SHELL_SCRIPTS" ]; then
    log_warn "No shell scripts found in $SCRIPT_DIR"
    exit 0
fi

# Count scripts
SCRIPT_COUNT=$(echo "$SHELL_SCRIPTS" | wc -l)
log "Found $SCRIPT_COUNT shell scripts"

# Make scripts executable
MADE_EXECUTABLE=0
ALREADY_EXECUTABLE=0

while IFS= read -r script; do
    if [ -f "$script" ]; then
        # Check if already executable
        if [ -x "$script" ]; then
            ALREADY_EXECUTABLE=$((ALREADY_EXECUTABLE + 1))
            if $VERBOSE; then
                log "Already executable: $(basename "$script")"
            fi
        else
            # Make executable
            chmod +x "$script"
            if [ $? -eq 0 ]; then
                MADE_EXECUTABLE=$((MADE_EXECUTABLE + 1))
                if $VERBOSE; then
                    log "Made executable: $(basename "$script")"
                fi
            else
                log_error "Failed to make executable: $(basename "$script")"
            fi
        fi
    fi
done <<< "$SHELL_SCRIPTS"

print_header "Summary"
echo "Total scripts found: $SCRIPT_COUNT"
echo "Already executable: $ALREADY_EXECUTABLE"
echo "Made executable: $MADE_EXECUTABLE"

if [ $MADE_EXECUTABLE -gt 0 ]; then
    log_success "Made $MADE_EXECUTABLE scripts executable"
else
    log "All scripts were already executable"
fi

# List all executable scripts by category
if $VERBOSE; then
    echo
    print_header "Executable Scripts by Category"

    for category in databases setup tools utils; do
        category_dir="$SCRIPT_DIR/$category"
        if [ -d "$category_dir" ]; then
            echo
            echo "📁 $category/"
            find "$category_dir" -type f \( -name "*.sh" -o -name "*.bash" \) -executable 2>/dev/null | sort | while read -r script; do
                echo "  ✓ $(basename "$script")"
            done
        fi
    done

    # Root level scripts
    echo
    echo "📁 scripts/"
    find "$SCRIPT_DIR" -maxdepth 1 -type f \( -name "*.sh" -o -name "*.bash" \) -executable 2>/dev/null | sort | while read -r script; do
        echo "  ✓ $(basename "$script")"
    done
fi

log_success "All scripts are now executable"
