<?php
$content = file_get_contents('app.js');
$fixed = "";

for ($i = 0; $i < mb_strlen($content, 'UTF-8'); $i++) {
    $char = mb_substr($content, $i, 1, 'UTF-8');
    // We want to reverse Windows-1252 -> Windows-874 mapping.
    // Try to encode as windows-1252 to get the raw byte.
    $byte = @iconv('UTF-8', 'Windows-1252//IGNORE', $char);
    
    if ($byte !== false && strlen($byte) == 1) {
        $ord = ord($byte);
        if (($ord >= 0xA1 && $ord <= 0xFB) || $ord == 0x97 || $ord == 0x8A || $ord == 0x89 || $ord == 0x92 || $ord == 0x93 || $ord == 0x94) {
            $thai_char = @iconv('CP874', 'UTF-8', $byte);
            if ($thai_char) {
                $fixed .= $thai_char;
                continue;
            }
        }
    }
    $fixed .= $char;
}

file_put_contents('app.js', $fixed);
echo "Done replacing in app.js";
