const fs = require('fs');
const iconv = require('iconv-lite');

const mangled = 'ظ…. ظ…ط­ظ…ظˆط¯ ط§ظ„ظ…ظٹظ‡ظٹ';

// 1. Get the characters
// 2. Encode to bytes using win1256
const bytes = iconv.encode(mangled, 'win1256');

// 3. Decode bytes using utf8
const recovered = iconv.decode(bytes, 'utf8');

console.log("Recovered:", recovered);

// Now try on the actual file!
const indexContent = fs.readFileSync('d:/برنامج مخازن/index.html', 'utf8');
const indexBytes = iconv.encode(indexContent, 'win1256');
const indexRecovered = iconv.decode(indexBytes, 'utf8');

console.log("Title in recovered file:");
const match = indexRecovered.match(/<title>(.*?)<\/title>/);
if(match) console.log(match[1]);

fs.writeFileSync('d:/برنامج مخازن/index.html', indexRecovered, 'utf8');
console.log("Wrote recovered index.html");
