// $Revision$
// $Id$
//
// Controleer of de flexfiles volgens de database ook op het filesysteem voorkomen
// Een parameter: UDL voor database connectie

// var withsha = WScript.Arguments(0);

var fso = new ActiveXObject("Scripting.FileSystemObject");
function __Log(s)
{
    WScript.Echo(s);
    var logPath = "../../../temp/" + CustId;
    var flog = fso.OpenTextFile(logPath + "/gen_verify_flexfiles.js.log", 8 /* ForAppending */, true /* create */);
    flog.WriteLine(s);
    flog.Close();
}

var udlstr = 'File Name=../Oracle.udl';
var Oracle = new ActiveXObject("ADODB.Connection");
Oracle.Open(udlstr);

try
{
    sql = "SELECT fac_version_cust FROM fac_version";
    oRs = Oracle.Execute(sql);
    var CustId = oRs(0).Value;
    oRs.Close();
    __Log("Starting at " + new Date());
    __Log("CustId: " + CustId);
    var flexfolder = "../../../../Branch20201_Data/" + CustId + "/";
    var flexfolder = "../../../../FPlace5i_Data/" + CustId + "/";

    var fullpath = fso.GetAbsolutePathName(flexfolder);
    __Log("flexfolder: " + fullpath);

    sql = "BEGIN fac.initsession(NULL); END;";
    oRs = Oracle.Execute(sql);

    var sql = "SELECT COUNT(*) cnt FROM fac_v_bijlagen"
            + " WHERE fac_bijlagen_verwijder IS NULL"
            + "   AND fac_bijlagen_refkey > 0";
    var oRs = Oracle.Execute(sql);
    var totalfiles = oRs("cnt").Value;
    oRs.Close();

    sql = "SELECT fac_bijlagen_key"
        + "     , fac_bijlagen_disk_directory"
        + "     , disk_filename"
        + "     , fac_bijlagen_file_size"
        + "     , fac_bijlagen_digest"
        + "  FROM fac_v_bijlagen"
        + " WHERE fac_bijlagen_verwijder IS NULL"
        + "   AND fac_bijlagen_refkey > 0"
        + " ORDER BY fac_bijlagen_module, fac_bijlagen_refkey, fac_bijlagen_kenmerk_key";
    oRs = Oracle.Execute(sql);
    var cnt = 0;
    var fail = 0;
    while (!oRs.eof)
    {
        // Verwijder bijlage van schijf.
        var fullfile = oRs("fac_bijlagen_disk_directory").Value + "/" + oRs("disk_filename").Value;
        if (!fso.FileExists(flexfolder + fullfile))
        {
            __Log("Missing " + fullfile);
            fail ++;
        }
        else
        {
            var fsoFile = fso.GetFile(flexfolder + fullfile);
            var size = fsoFile.Size;
            var sizeb64 = Math.ceil(fsoFile.Size /3) * 4; // Melding 62471
            var sizeMailb64 = sizeb64 + 2*Math.ceil(sizeb64 / 76); // CR+LF elke 76 karakters
            if (size != oRs("fac_bijlagen_file_size").Value && (sizeMailb64 != oRs("fac_bijlagen_file_size").Value))
            {
                fail++;
                __Log("Size (found " + size + ", sizeMailb64 " + sizeMailb64 + ", database " + oRs("fac_bijlagen_file_size").Value + ") " + fullfile);
            }
            //var date = new Date(fsoFile.DateLastModified);
            //var digest = oCrypto.hex_sha1_file(flexfolder + fullfile);
        }
        oRs.MoveNext();
        cnt ++;
        if ((cnt % 1000) == 0)
            WScript.Echo("Done " + cnt + "/" + totalfiles + " = " + Math.round(100 * cnt / totalfiles) + "%");
    }
    oRs.Close();
    WScript.Echo("Done " + cnt + "/" + totalfiles + " = " + Math.round(100 * cnt / totalfiles) + "%");
    WScript.Echo("Fail " + fail);
    __Log("flexfolder: " + fullpath);
}
catch(e)
{
    __Log("ERROR: " + e.description)
    throw e;
}

__Log("Done at " + new Date());

