import sys

def fix_mojibake(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        text = f.read()

    fixed_text = ""
    for char in text:
        # Check if the character is likely a corrupted Thai character (interpreted as Windows-1252).
        # We can just try to encode it as windows-1252 and decode as cp874.
        try:
            b = char.encode('windows-1252')
            # Only convert if the byte is in the typical Thai range (0xA1 to 0xFB)
            if b[0] >= 0xA1 and b[0] <= 0xFB:
                thai_char = b.decode('cp874')
                fixed_text += thai_char
            elif b[0] == 0x97: # em-dash often used in TIS-620
                thai_char = b.decode('cp874')
                fixed_text += thai_char
            else:
                fixed_text += char
        except (UnicodeEncodeError, UnicodeDecodeError):
            fixed_text += char
            
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(fixed_text)

fix_mojibake('app.js')
