#!/bin/bash
# Clean invisible Unicode characters from .env files
# This removes U+200E (Left-to-Right Mark) and other common invisible characters

set -e

ENV_FILE="${1:-.env}"

if [ ! -f "$ENV_FILE" ]; then
    echo "Error: File $ENV_FILE not found"
    echo "Usage: $0 [env-file-path]"
    exit 1
fi

echo "Cleaning $ENV_FILE..."

# Create a backup
cp "$ENV_FILE" "${ENV_FILE}.backup"
echo "Created backup: ${ENV_FILE}.backup"

# Remove common invisible Unicode characters:
# - U+200E (Left-to-Right Mark) - E2 80 8E
# - U+200F (Right-to-Left Mark) - E2 80 8F
# - U+200B (Zero Width Space) - E2 80 8B
# - U+FEFF (Zero Width No-Break Space / BOM) - EF BB BF
# - U+202A-202E (Directional formatting characters)

sed -i 's/\xE2\x80\x8E//g' "$ENV_FILE"  # Remove U+200E
sed -i 's/\xE2\x80\x8F//g' "$ENV_FILE"  # Remove U+200F
sed -i 's/\xE2\x80\x8B//g' "$ENV_FILE"  # Remove U+200B
sed -i 's/\xEF\xBB\xBF//g' "$ENV_FILE"  # Remove BOM
sed -i 's/\xE2\x80\xAA//g' "$ENV_FILE"  # Remove U+202A
sed -i 's/\xE2\x80\xAB//g' "$ENV_FILE"  # Remove U+202B
sed -i 's/\xE2\x80\xAC//g' "$ENV_FILE"  # Remove U+202C
sed -i 's/\xE2\x80\xAD//g' "$ENV_FILE"  # Remove U+202D
sed -i 's/\xE2\x80\xAE//g' "$ENV_FILE"  # Remove U+202E

echo "Cleaned successfully!"
echo "If you want to restore the backup: mv ${ENV_FILE}.backup $ENV_FILE"
