<%
/*
     $Revision$
     $Id$

    File:           fac_verify_sha.inc
    Description:    Berekent SHA checksum over alle files

                    Werkt in combinatie met de geautomatiseerde mkRelease
                    die in de appl-folder een .sha file met checksums plaatst.

*/

function findSHAinfo()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var oCrypto = new ActiveXObject("SLNKDWF.Crypto");
    var oAbout = new ActiveXObject("SLNKDWF.About");
    var tmStart = oAbout.usTimer;

    // zoek de nieuwste sha file in de appl folder. Als het goed is, is er maar
    // eentje maar mensen overschrijven nog wel eens een appl/ ipv vervangen
    var maxrev = -1;
    var shafile;
    var startFolder = fso.GetFolder(Server.MapPath("../"));
    var fc1 = new Enumerator(startFolder.Files);
    for (; !fc1.atEnd(); fc1.moveNext())
    {
      var fil = fc1.item();
      var filename = fil.Name;
      if (filename.match(/svnrevision_r\d*\.sha\.inc/i))
      {
        var rev = parseInt(filename.substring(13), 10);
        if (rev > maxrev)
        {
            maxrev = rev;
            shafile = filename;
        }
      }
    }
    if (maxrev < 0)
        return null;

    var shainfo = { svnrev: maxrev,
                    shafile: shafile,
                    retrieved: new Date(),
                    ok: [],
                    changed: [],
                    unknown: [],
                    missing: []
                  };

    var fsha = fso.GetFile(Server.MapPath("../" + shafile));
    shainfo.shadate = new Date(fsha.DateLastModified);
    var fsha = fso.OpenTextFile(Server.MapPath("../" + shafile), 1); // readonly
    var allsha = fsha.ReadAll().split("\n");
    if (allsha.length == 1)
        var allsha = allsha.split(/\x0A/); // Misschien alleen UNIX LF's?
    fsha.Close();
    var allfiles = { };
    // Some known safe files
    allfiles["../appl/" + shafile] = 1;
    allfiles["../web.config"] = 1;
    allfiles["../default.asp"] = 1;

    for (var i in allsha)
    {
        var regel = allsha[i];
        var spl = regel.split(" ");
        if (spl.length == 2)
        {
            var sha_should = spl[0];
            var fname = spl[1].substring(1, spl[1].length);
            fname = fname.replace(/\\/g,'/');
            allfiles[fname.toLowerCase()] = 1;
            var file = { fname: fname.replace(/^\.\.\//,"/") };

            if (!fso.FileExists(Server.MapPath("../" + fname)))
            {
                shainfo.missing.push (file);
            }
            else
            {
                var ff = fso.GetFile(Server.MapPath("../" + fname));
                file.date = new Date(ff.DateLastModified);
                try
                {
                    var sha_is = oCrypto.hex_sha1_file(Server.MapPath("../" + fname));
                    if (sha_should != sha_is)
                    {
                        file.message = "SHA mismatch";
                        shainfo.changed.push (file);
                    }
                    else
                    {
                        shainfo.ok.push (file);
                    }
                }
                catch(e)
                {
                    file.message = "Error reading it: " + e.description;
                    shainfo.changed.push (file);
                }
            }
        }
    }

    // Overbodige files
    var rootdir = Server.MapPath("../../") + "/";
    function scanFolder(scanDir, lvl)
    {
        if (scanDir.match(/CUST\/$/i))
            return;
        if (scanDir.match(/TEMP\/$/i))
            return;
        if (scanDir.match(/.svn\/$/i))
            return;
        startFolder = fso.GetFolder(rootdir + scanDir)
        var fc1 = new Enumerator(startFolder.Files);
        for (; !fc1.atEnd(); fc1.moveNext())
        {
          var fil = fc1.item();
          var filename = fil.Name;

          if (!(("../" + scanDir + filename).toLowerCase() in allfiles))
          {
            // We zouden kunnen zoeken of zijn sha bij de 'missing' voorkomt
            // wat een rename aangeeft.
            //var sha1 = oCrypto.hex_sha1_file(rootdir + scanDir + filename);
            shainfo.unknown.push ({ fname: "/" + scanDir + filename, date: new Date(fil.DateLastModified) });
          }
        }
        // En nu recursief alle subfolders.
        var fc1 = new Enumerator(startFolder.SubFolders);
        for (; !fc1.atEnd(); fc1.moveNext())
        {
          fil = fc1.item();
          scanFolder(scanDir + fil.Name + "/", lvl+1);
        }
    }
    scanFolder("", 0);

    shainfo.duration = Math.floor((oAbout.usTimer - tmStart) / 1000);

    return shainfo;
}
%>
