#!/bin/bash
# Script to compile .po files to .mo files for WordPress plugin localization

echo "Compiling translation files..."

# Check if msgfmt is available
if ! command -v msgfmt &> /dev/null; then
    echo "Error: msgfmt command not found. Please install gettext tools."
    echo "On macOS: brew install gettext && brew link gettext --force"
    echo "On Ubuntu/Debian: sudo apt-get install gettext"
    exit 1
fi

# Directory containing .po files
LANGUAGES_DIR="languages"

# Counter for compiled files
COMPILED=0
FAILED=0

# Compile all .po files to .mo files
for po_file in "$LANGUAGES_DIR"/*.po; do
    if [ -f "$po_file" ]; then
        # Get filename without extension
        base_name=$(basename "$po_file" .po)
        mo_file="$LANGUAGES_DIR/$base_name.mo"
        
        echo "Compiling $po_file -> $mo_file"
        
        if msgfmt -o "$mo_file" "$po_file"; then
            echo "✅ Successfully compiled: $base_name"
            ((COMPILED++))
        else
            echo "❌ Failed to compile: $base_name"
            ((FAILED++))
        fi
    fi
done

echo ""
echo "================================"
echo "Compilation complete!"
echo "Successfully compiled: $COMPILED files"
echo "Failed: $FAILED files"
echo "================================"

if [ $FAILED -eq 0 ]; then
    exit 0
else
    exit 1
fi

