// MaasStatusResponse.js
// Haalt statuswijzigingen op van de FTP server.
//
// $Revision$
// $Id$
//


function padout(number) { return (number < 10) ? "0" + number : number; }

function date2string(mydate) {
  return padout(mydate.getFullYear())  + "-" + padout(mydate.getMonth() + 1) + "-" + padout(mydate.getDate())
      + " " +  padout(mydate.getHours())  + "-" + padout(mydate.getMinutes() + 1) + "-" + padout(mydate.getSeconds());
  }

var api = WScript.Arguments(0); // api
var folder = WScript.Arguments(1); // folder
var url = WScript.Arguments(2); // url

try {
  var fh, lenght;
  //var api = "MLD2OPDR";

  fso = new ActiveXObject("Scripting.FileSystemObject");
  objRS = new ActiveXObject("ADODB.Recordset");
  objRS.CursorLocation = 3 // useClient
  objRS.Fields.Append ("Name", 200, 100) // adVarChar
  objRS.Fields.Append ("created", 7 ) // adDate
  objRS.Open();

  try
  {
     var backdir_api = ".\\BACKUP"+(new Date).getFullYear()+"\\";
     if (!fso.FolderExists(backdir_api))
       fso.CreateFolder(backdir_api);
     backdir_api += api + "\\";
     if (!fso.FolderExists(backdir_api))
       fso.CreateFolder(backdir_api);
  }
  catch (e)
  {
     WScript.Echo("Cannot create backup folder " + backDir + "\n" + e.description);
     WScript.Quit();
  }

  // plaats de bestandsnamen en creatiedata in een resultset om deze vervolgens te kunnen filteren.
  folder = folder.replace(/\//g,"\\");
  WScript.Echo("Folder: " + folder);
  var f = fso.GetFolder(folder);
  var fc = new Enumerator(f.files);
  var filecount = 0;
  for   (;   !fc.atEnd();   fc.moveNext())
  {
    filename = fc.item().name;
    WScript.Echo("File: " + filename);
    var file = fso.GetFile(folder + filename);
    var dt = new Date(file.DateCreated);
    WScript.Echo("File: " + filename  + " timestamp:" + date2string(dt));
    if (fc.item().name.indexOf(".xml") != -1) {
      filecount++;
      objRS.AddNew();
      objRS("Name") = filename;
      objRS("created") = file.DateCreated;
    }
  }
  //WScript.Echo("filecount " + filecount);
  if (filecount > 0) {
    objRS.Sort = "created ASC";
    objRS.MoveFirst();
    while (!objRS.EOF)
    {
      filename = objRS("Name").Value;
      //WScript.Echo("File (sorted): " + filename);
      objRS.MoveNext();
      var fileStream = new ActiveXObject("ADODB.Stream");
      fileStream.open();
      fileStream.CharSet = "Windows-1252"; // het doel
      fileStream.LoadFromFile(folder + filename);
      WScript.Echo("Filesize " + fileStream.Size);
      fileStream.Position = 0;
      WScript.Echo("Request1: " + fileStream.ReadText);
      fileStream.Position = 0;
      XMLReq = fileStream.ReadText;
      WScript.Echo("Request: " + XMLReq);
      WScript.Echo("URL: " + url);
      var objXMLHTTP = new ActiveXObject("MSXML2.ServerXMLHTTP.6.0");

      objXMLHTTP.open("POST",
                      url
                      );

      objXMLHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
      objXMLHTTP.send(XMLReq);

      WScript.Echo("Status: " + objXMLHTTP.status);
      WScript.Echo("StatusText: " + objXMLHTTP.statusText);
      WScript.Echo("Result: " + objXMLHTTP.responseText);

      if (objXMLHTTP.status == 200) {
        jsDate = new Date;
        dateString = padout(jsDate.getFullYear())  + "-" + padout(jsDate.getMonth() + 1) + "-" + padout(jsDate.getDate());
        timeString = padout(jsDate.getHours())  + "-" + padout(jsDate.getMinutes() + 1) + "-" + padout(jsDate.getSeconds());
        backDir = backdir_api + dateString + "_" + api + "\\";
        if (!fso.FolderExists(backDir))
          fso.CreateFolder(backDir)
        // deze file backuppen
        //WScript.Echo("from:" + filename);
        //WScript.Echo("to:" + backDir + timeString + "_" + filename);
        fso.MoveFile(folder + filename, backDir + timeString + "_" + filename);
      }
    }
  }
}
catch (e)
{
    WScript.Echo("Serieuze fout in MaasStatusResponse.js: " + e.description)
    WScript.Quit(1);
}