#!/bin/bash

# Strip all comments from scripts before NPM publish
# This protects IP by removing implementation details

set -e

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

echo "Stripping comments from scripts..."

# Function to strip bash comments
strip_bash_comments() {
    local file="$1"
    local temp_file="${file}.tmp"
    
    # Remove comment lines and inline comments
    # Keep shebang line
    awk '
    NR==1 && /^#!/ { print; next }
    /^[[:space:]]*#/ { next }
    {
        gsub(/#.*$/, "")
        gsub(/[[:space:]]+$/, "")
        if (length($0) > 0) print
    }
    ' "$file" > "$temp_file"
    
    mv "$temp_file" "$file"
}

# Strip comments from install.sh
if [[ -f "${PROJECT_ROOT}/scripts/install.sh" ]]; then
    echo "Processing install.sh..."
    strip_bash_comments "${PROJECT_ROOT}/scripts/install.sh"
fi

# Strip comments from uninstall.sh
if [[ -f "${PROJECT_ROOT}/scripts/uninstall.sh" ]]; then
    echo "Processing uninstall.sh..."
    strip_bash_comments "${PROJECT_ROOT}/scripts/uninstall.sh"
fi

# Strip comments from apply.sh
if [[ -f "${PROJECT_ROOT}/enhancements/apply.sh" ]]; then
    echo "Processing apply.sh..."
    strip_bash_comments "${PROJECT_ROOT}/enhancements/apply.sh"
fi

echo "Comments stripped successfully"