var metaMarked =    require('meta-marked'),
    fs =            require('fs-extra'),
    yamljs =        require('yamljs'),
    fswf =          require('safe-write-file');

var SOURCE_PATH = "src/",
    SOURCE_INFO_FILE = "info.yml",
    SOURCE_EXTENSION = "md",
    SOURCE_COVER_FILE = "cover.png",
    DIST_PATH = "dist/",
    DIST_INFO_FILE = "info.json",
    DIST_COVER_FILE = "cover.png",
    DIST_EXTENSION = "html",
    DIST_S3 = "https://s3-us-west-2.amazonaws.com/com.cryart.sabbathschool";


var processLang = function(path){
  var quarterlies = fs.readdirSync(path).reverse(),
      lang_info = [];

  for (var i = 0; i < quarterlies.length; i++){
    var quarterly_path = path + "/" + quarterlies[i];
    if (!fs.lstatSync(quarterly_path).isDirectory()) continue;
    if (!fs.lstatSync(quarterly_path + "/" + SOURCE_INFO_FILE).isFile()) continue;
    if (!fs.lstatSync(quarterly_path + "/" + SOURCE_COVER_FILE).isFile()) continue;

    var quarterly = fs.readdirSync(quarterly_path);

    var quarterly_info = yamljs.load(quarterly_path + "/" + SOURCE_INFO_FILE);
    quarterly_info.lessons = [];

    fs.copySync(quarterly_path + "/" + SOURCE_COVER_FILE, quarterly_path.replace(SOURCE_PATH, DIST_PATH) + "/" + DIST_COVER_FILE);
    quarterly_info.cover = DIST_S3 + "/" + quarterly_path.replace(SOURCE_PATH, "") + "/" + DIST_COVER_FILE;

    for (var j = 0; j < quarterly.length; j++){
      var extension = quarterly[j].split(".").pop();
      if (extension != SOURCE_EXTENSION) continue;

      var day_path = quarterly_path.replace(SOURCE_PATH, DIST_PATH) + "/" + quarterly[j].replace(SOURCE_EXTENSION, DIST_EXTENSION),
          quarterly_md = metaMarked(fs.readFileSync(quarterly_path + "/" + quarterly[j], "utf-8"));

      fswf(day_path, quarterly_md.html);
      quarterly_md.meta.path = day_path;
      quarterly_info.lessons.push(quarterly_md.meta);
    }
    lang_info.push(quarterly_info);

  }

  fswf(path.replace(SOURCE_PATH, DIST_PATH) + "/" + DIST_INFO_FILE, JSON.stringify(lang_info));
};

var languages = fs.readdirSync(SOURCE_PATH);

for (var i = 0; i < languages.length; i++){
  if (!fs.lstatSync(SOURCE_PATH + languages[i]).isDirectory()) continue;
  processLang(SOURCE_PATH + languages[i]);
}



@@ -21,17 +21,17 @@
 require(
-'
+%22
 meta-mar
@@ -33,17 +33,17 @@
 a-marked
-'
+%22
 ),%0A    f
@@ -69,17 +69,17 @@
 ire(
-'
+%22
 fs-extra
 '),%0A
@@ -74,17 +74,17 @@
 fs-extra
-'
+%22
 ),%0A    y
@@ -110,16 +110,16 @@
 ire(
-'
+%22
 yamljs
-'
+%22
 ),%0A 
@@ -145,17 +145,17 @@
 require(
-'
+%22
 safe-wri
@@ -165,9 +165,9 @@
 file
-'
+%22
 );%0A%0A
@@ -1801,24 +1801,134 @@
 .path = 
-day_path
+DIST_S3 + %22/%22 + quarterly_path.replace(SOURCE_PATH, %22%22) + %22/%22 + quarterly%5Bj%5D.replace(SOURCE_EXTENSION, DIST_EXTENSION)
 ;%0A      

Add proper path value to each lesson using DIST Deploy path