var config = require('./config');
var azure = require('azure-storage');
var fs = require('fs');
var dir = require('node-dir');
var moment = require('moment');
var commandLineArgs = require("command-line-args");
 
var cli = commandLineArgs([
    { name: "pathtobackup", alias: "p", type: String },
    { name: "daystokeep", alias: "d", type: Number }
]);

var usage = cli.getUsage({
	title: "BackupBackup",
    description: "Uploads a directory of files to Azure. Optionally deletes old files (if daystokeep is specified)."
});
var options = cli.parse();

if(options.pathtobackup === undefined) {
	console.log(usage);
	process.exit(1);	
}

console.log("********** BackupBackup! **********");
console.log("Container: " + config.azureContainerName);
console.log("Storage Account: " + config.azureStorageAccount);
console.log();

//create a blob service set explicit credentials
var blobService = azure.createBlobService(config.azureStorageAccount, config.azureAccessKey);

var backupDirectory = options.pathtobackup;

var doDelete = false;
if(options.daystokeep !== undefined) {
	var  deleteThresholdDate = moment().subtract(Number(options.daystokeep), "days");
	doDelete = true;
	console.log("Will delete any files in source directory older than " + options.daystokeep + " days (" + deleteThresholdDate.format() + ")");
}

dir.files(backupDirectory, function(err, files) {
	if (err) throw err;

	//we have an array of files now, so now we'll iterate that array
	files.forEach(function(path) {
		// if older than 1 week delete (assume it's already gone to Azure)
		if(fs.stat(path, function(err, stats) {
			if (err) throw err;
			var dateLastModified = moment(stats.mtime)
			if(doDelete && (dateLastModified < deleteThresholdDate)) {
				console.log("Deleting old file [" + path + "]");
				fs.unlink(path);
			}
			else {
				console.log("Uploading to Azure [" + path + "]");
				UploadFileToAzure(path, backupDirectory);
			}
		}));
	})
});

function UploadFileToAzure(filename, backupDir) {

	// Remove the backup directory (preserving any subdirectories), and trailing slashes
	var filenammeAzure = filename.replace(backupDir.replace("/", "\\"),"").replace(/^[\/\\]|[\/\\]$/g, '');
	blobService.createBlockBlobFromLocalFile(config.azureContainerName, filenammeAzure, filename, function(error, result, response){
	  if(!error){
	  	console.log("File uploaded [" + filenammeAzure + "]");
	  }
	  else {
	  	console.log("Uh oh! Failed to upload [" + filenammeAzure + "] - result [" + error + "]");
	  }
	});

}

@@ -1819,62 +1819,8 @@
 e %7B%0A
-%09%09%09%09console.log(%22Uploading to Azure %5B%22 + path + %22%5D%22);%0A
 %09%09%09%09
@@ -1861,16 +1861,16 @@
 ctory);%0A
+
 %09%09%09%7D%0A%09%09%7D
@@ -2027,25 +2027,24 @@
 %09var filenam
-m
 eAzure = fil
@@ -2124,16 +2124,326 @@
 , '');%0A%09
+%0A%09blobService.doesBlobExist(config.azureContainerName, filenameAzure, function(error, result) %7B%0A%09%09if (!error) %7B%0A%09%09%09if (result) %7B%0A%09%09%09%09console.log(%22File  %5B%22 + filenameAzure + %22%5D already exists in directory...%22);%0A%09%09%09%7D else %7B%0A%09%09%09%09console.log(%22File does not exist, uploading to Azure %5B%22 + filename + %22%5D%22);%0A%09%09%09%09%0A%09%09%09%09
 blobServ
@@ -2505,25 +2505,24 @@
 ame, filenam
-m
 eAzure, file
@@ -2563,18 +2563,20 @@
 onse)%7B%0A%09
-  
+%09%09%09%09
 if(!erro
@@ -2580,18 +2580,20 @@
 rror)%7B%0A%09
-  
+%09%09%09%09
 %09console
@@ -2616,33 +2616,32 @@
 ded %5B%22 + filenam
-m
 eAzure + %22%5D%22);%0A%09
@@ -2644,23 +2644,27 @@
 );%0A%09
-  %7D%0A%09  
+%09%09%09%09%7D%0A%09%09%09%09%09
 else %7B%0A%09
   %09c
@@ -2659,18 +2659,20 @@
 else %7B%0A%09
-  
+%09%09%09%09
 %09console
@@ -2713,17 +2713,16 @@
  filenam
-m
 eAzure +
@@ -2758,16 +2758,39 @@
 );%0A%09
-  %7D%0A%09%7D);%0A
+%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D);
 %0A%7D%0A

Add check if blob already exists before trying to upload it