// FSN#32930 en FSN#29226
// Zet FAC_LCLMDL.csv met teksten voor API-doc en Help@fingerdetails
// om naar reguliere _fac_lclmdl.src
// Nog erg experimenteel en niet in productie gebruik

var fso = new ActiveXObject("Scripting.FileSystemObject");
//var inFile = fso.OpenTextFile("FAC_LCLMDL.csv");
var inFile = fso.OpenTextFile(WScript.Arguments(0));
var alldata = inFile.ReadAll();

var  alllines = alldata.split(/\x0D\x0A/);
//WScript.Echo(alllines.length + " lines");
var cntfields = alllines[0].split(";");

WScript.Echo("/*********************************");
WScript.Echo(" * GENERATED FILE, DO NOT MODIFY *");
WScript.Echo(" *********************************/");

WScript.Echo("START_FACXSL_LCL('ASP', NULL)");
var prev_model;
for (l=1; l < alllines.length; l++)
{
    var linearr = alllines[l].split(";");
    var model = linearr[0];
    if (prev_model && model && model != prev_model)
    {
        WScript.Echo("END_FACXSL_LCL()");
        WScript.Echo("START_FACXSL_LCL('ASP', NULL)");
    }
    model = model || prev_model;
    prev_model = model;
    var fld = linearr[1];
    for (var i = 2; i < linearr.length - 1; i++)
    {
        if (linearr[i].substr(0,1) == "\"" && linearr[i].substr(linearr[i].length - 1) != "\"")
        {
            linearr[i] += ";" + linearr[i + 1];
            linearr.splice(i + 1, 1);
            i--; // deze nog een keer proberen
            continue;
        }
        if (linearr[i].substr(0,1) == "\"" && linearr[i].substr(linearr[i].length - 1) == "\"")
            linearr[i] = linearr[i].substr(1, linearr[i].length - 2);
        linearr[i] = linearr[i].replace(/\"\"/g, "\""); // Dubbele quote naar enkel

    }
    // FACXSL_LCL('lcl_facilitor_header_prefix', 'NL tekst', 'EN tekst', 'DE tekst', 'FR tekst')
    WScript.Echo("FACXSL_LCL('" + model + "." + fld + ".hint', " + safe(linearr[2]) + ", "
                                                                 + safe(linearr[3]) + ", "
                                                                 + safe(linearr[4]) + ", "
                                                                 + safe(linearr[5]) + ")");
}
WScript.Echo("END_FACXSL_LCL()");

function safe(tekst)
{
    tekst = tekst.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F]+/g, "?");
    tekst = tekst.substr(0, 2000);
    tekst = "'" + tekst.replace(/\'/g,"''") + "'"; // " syntax highlight correctie
    tekst = tekst.replace(/\n/g, "'||CHR(10)||'");
    return tekst;
}