const fs = require('fs');
const { exec } = require('child_process');

let versionSource = fs.readFileSync('src/version.js', {encoding:'utf-8'});
let regex = /[\'][0-9\.]+[\']/g;

let version = versionSource.match(regex)[0].replace(/[\']/g, '');
let versions = version.split('.');

let inc = [0,0,0,1];

for(var index = 3; index >= 0; index--) {
    versions[index] = parseInt(versions[index]) + parseInt(inc[index]);
    if(versions[index] > 999 && index > 0) {
        versions[index] = 1;
        versions[index-1] = parseInt(versions[index-1]) + 1;
    }
}

versionSource = versionSource.replace(regex, `\'${versions.join('.')}\'`);
fs.writeFileSync('src/version.js', versionSource, { encoding: 'utf-8' });

console.log('Version increased. last version :', version, ' => new version :', versions.join('.'));

/*
exec(`src/version_push.bat ${version} ${versions.join('.')}`, (error, stdout, stderr) => {
    if(error) {
        console.log(`error: ${error.message}`);
        throw Error(error.message);
    }
    if(stderr) {
        console.log(`stderr: ${stderr}`);
        throw Error(stderr);
    }
    console.log(`${stdout}`);
    console.log('Version pushed.');
});
*/