// The following variables control where wget.exe is located and usernames and passwords
// Note: set to "" for no username and password
var strWgetLocation = "c:\\pdf\\wget.exe";
var strHttpUsername = "domain\\username";	// Or, take out the 'domain\\' part.
var strHttpPassword = "password_here";

// The following variable control the target locations
var strTargetDirectory = "c:\\pdf\\";
var strTargetExtension = ".pdf";

// The following variables control the locations to backup
var strSourceDirectory = "\\\\server_name\\c$\\inetpub\\wwwroot\\pmwiki\\wiki.d\\";
var strHttpLocation = "http://server_web_address/pmwiki/pmwiki.php?n=";
var strHttpAction = "?action=pdf";

// The following variables are the prefix and suffix of what wget names the file that it
// downloads.  This is already set for pmwiki with action=pdf, but can be changed.
var strTempPrefix = "pmwiki.php@n=";
var strTempSuffix = "%3Faction=pdf";

// Amount of time to wait before executing the next line in milliseconds
var sleepTime = 5000;

// -------------------------
// Actual script starts here
// -------------------------

// Function that checks for commas in a given string (if a filename has a 
// comma in it, it's been deleted, so don't downlod it).
function checkForComma(str) {
	for (i=0; i<str.length; i++)
	{
		if (str.charAt(i) == ",") { return false; }
	}
	return true;
}

// Figure out if we are using authentication
var strAuthenticationString
if (strHttpUsername == "")
{
	strAuthenticationString = "";
}
else
{
	strAuthenticationString = "--http-user=" + strHttpUsername +
		" --http-password=" + strHttpPassword;
}

// Create a Shell object
var objShell = new ActiveXObject("WScript.Shell");

// Create new filesystem object and temporary variables
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fsoNew = new ActiveXObject("Scripting.FileSystemObject");
var msg = "";

// Get the folder object
var srcFolder=fso.GetFolder(strSourceDirectory);
var files = new Enumerator(srcFolder.files);
var currentFile;
var currentFileNew;

// Loop through files
for(; !files.atEnd(); files.moveNext())
{
	// Don't execute for files with commas in their names
	if (checkForComma(files.item().Name) && files.item().Name.charAt(0) != ".")
	{
		// Download the file from the website
		WScript.Echo(strWgetLocation + " " + strAuthenticationString + " " + 
			strHttpLocation + files.item().Name + strHttpAction);
		objShell.Run(strWgetLocation + " " + strAuthenticationString + " " + 
			strHttpLocation + files.item().Name + strHttpAction);
		
		// Take a short nap to let the program finish
		WScript.Sleep(sleepTime);
		
		// Prepare to rename and move newly created file
		currentFile = fsoNew.getFile(strTempPrefix + files.item().Name + 
			strTempSuffix);
		currentFileNew = strTargetDirectory + files.item().Name + 
			strTargetExtension;
			
		// Delete existing file if needed.
		if (fso.FileExists(currentFileNew)) 
		{ 
			fso.DeleteFile(currentFileNew);
		}
		currentFile.Move(currentFileNew);
	}	
}