/**
 * Test Password Default MySQL Fingerspot Personnel
 */
const { execSync } = require('child_process');
const fs = require('fs');

console.log('=======================================================');
console.log(' TESTING FINGERSPOT DEFAULT MYSQL PASSWORD');
console.log('=======================================================');

const MYSQL_EXES = [
    'C:\\laragon\\bin\\mysql\\mysql-8.4.3-winx64\\bin\\mysql.exe',
    'C:\\laragon\\bin\\mysql\\mysql-8.0.30-winx64\\bin\\mysql.exe',
    'C:\\laragon\\bin\\mysql\\mysql-5.7.33-winx64\\bin\\mysql.exe',
    'C:\\Program Files (x86)\\Fingerspot Personnel\\mysql.exe',
];

let mysqlCmd = 'mysql';
for (const p of MYSQL_EXES) {
    if (fs.existsSync(p)) {
        mysqlCmd = `"${p}"`;
        break;
    }
}

const PASSWORDS = [
    'Coba lagi lain kali 2015',
    'Coba lagi lain kali 20158*nyqT/Vs4',
    'root8*nyqT/Vs4',
    'fingerspot',
    'personnel'
];

for (const pass of PASSWORDS) {
    try {
        const cmd = `${mysqlCmd} -h 127.0.0.1 -P 3309 -u root -p"${pass}" -e "SHOW DATABASES;"`;
        console.log(`Trying password "${pass}"...`);
        const output = execSync(cmd, { encoding: 'utf8', timeout: 4000 });
        console.log(`\n🎉 SUKSES DISCOVERY! PASSWORD TERKUNCI DITEMUKAN!`);
        console.log(`Password: "${pass}"\n`);
        console.log(output);

        // List tables in fin_pro
        try {
            const cmd2 = `${mysqlCmd} -h 127.0.0.1 -P 3309 -u root -p"${pass}" -e "SHOW TABLES FROM fin_pro;"`;
            const tables = execSync(cmd2, { encoding: 'utf8', timeout: 4000 });
            console.log(`\nTabel-tabel di Database fin_pro:\n${tables}`);
        } catch (e2) {
            console.log(`(Fin_pro check: ${e2.message.split('\n')[0]})`);
        }

        break;
    } catch (e) {
        console.log(` ❌ ${e.message.split('\n')[0]}`);
    }
}

console.log('=======================================================\n');
