var custid = WScript.Arguments(0);
var flex   = WScript.Arguments(1);
scanFlexFolder(custid, flex);


function scanFlexFolder(custid, flexpath)
{
    var scanDir = "../../"+custid+"/"+flexpath+(flexpath.slice(flexpath.length-1)=="/"?"":"/");
    WScript.Echo("------------- ["+ scanDir + "] -------------")

    var udl = "../oracle.udl";
    var Oracle = new ActiveXObject("ADODB.Connection");
    try
    {
        Oracle.Open("File Name=" + udl);

        try
        {
            var sql = "DELETE FROM UWVA_FLEXFILES";
            Oracle.Execute(sql);
            __Log(sql);
            //
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            if (fso.FolderExists(scanDir))
                scanFolder(scanDir, 0);
            else
                __Log("Folder does not exist: "+scanDir);
            //
            Oracle.Close();
        }
        catch (e) {};
    }
    catch (e)
    {
        __Log("Cannot open connection with udl: " + udl);
        WScript.Quit(1);
    }
    //
    WScript.Quit(0);


    function scanFolder(scanDir, lvl)
    {
        //WScript.Echo(lvl+" scanDir:"+scanDir);
        var startFolder = fso.GetFolder(scanDir);
        var fc1 = new Enumerator(startFolder.SubFolders);
        for (; !fc1.atEnd(); fc1.moveNext())
        {
            var fol = fc1.item();
            var fpath = scanDir + fol.Name + "/";
            //
            scanFolder(fpath, lvl+1);
            //
            var currFolder = fso.GetFolder(fpath);
            var fc2 = new Enumerator(currFolder.Files);
            for (; !fc2.atEnd(); fc2.moveNext())
            {
                var fil = fc2.item();
                var fname = fil.Name;
                var fsize = fil.Size;
                var pathArr = fpath.split("/");
                //
                var kenmerk_key = pathArr[pathArr.length-2];
                var deel_key    = pathArr[pathArr.length-3];
                var module      = pathArr[pathArr.length-5].toUpperCase();
                var prefix      = "";
                //
                var err = "";
                if (!parseInt(kenmerk_key))
                {
                    err = "kenmerk";
                }
                if (!parseInt(deel_key))
                {
                    if (!parseInt(deel_key.slice(1)))
                        err = "object";
                    else
                    {
                        prefix = deel_key.slice(0,1).toUpperCase();
                        deel_key = parseInt(deel_key.slice(1));
                    }
                }

                if (!err)
                {
                    try
                    {
                        var sql_i = "INSERT INTO UWVA_FLEXFILES"
                                  + " (module, prefix, ins_deel_key, ins_kenmerk_key, file_name, file_size)"
                                  + " VALUES"
                                  + "(" + safe.quoted_sql(module)
                                  + "," + safe.quoted_sql(prefix)
                                  + "," + deel_key
                                  + "," + kenmerk_key
                                  + "," + safe.quoted_sql(fname)
                                  + "," + fsize
                                  + ")";
                        Oracle.Execute(sql_i);
                    }
                    catch (e)
                    {
                        WScript.Echo("Niet toegevoegd: ", module, prefix, deel_key, kenmerk_key, fname, fsize);
                    };
                }
                else
                    WScript.Echo("Geen "+err+" in pad: "+ fpath);
            }
        }
    }
}
