#!/usr/bin/env Rscript --vanilla

#Knittr all doc and doc/tutorial

library("caret")
library("mlr")

knit_subdir = "doc/knitted" #dir where the posts are published
input_subdir = "doc/temp" #dir where the new Rmd files can be found
figure_dir = "figs"


KnitPost = function(input, destination) {
    require(knitr)
    this_wd = getwd()
    dir.create(dirname(destination), showWarnings = FALSE)
    setwd(dirname(destination))
    fig.path = paste0(figure_dir,"/", sub(".Rmd$", "", basename(input)), "/")
    opts_chunk$set(fig.path = fig.path)
    render_markdown()
    knit(file.path(this_wd,input), envir = parent.frame(), output=basename(destination))
    setwd(this_wd)
}

this_wd = getwd()
files = list.files(input_subdir, recursive=TRUE)
ending = substr(files, nchar(files)-3, nchar(files))
files = files[ending==".Rmd"]
for(file in files) {
  destination = file.path(knit_subdir, paste(substr(file,start=0,stop=nchar(file)-3), "md", sep=""))
  KnitPost(input=file.path(input_subdir,file), destination=destination)
}

warnings()
