
sourceDir <- function(path=getwd(), trace = TRUE, ...) {
         for (nm in list.files(path, pattern = "\\.[RrSsQq]$")) {
            if(trace) cat(nm," ")           
            source(file.path(path, nm), ...)
         }
         if(trace) cat("\n")
      }

cc = function(test=TRUE, branch="pkg", clean=FALSE, debug=FALSE) {
    old = getwd()
    homedir = paste("/home/mdowle/R/datatable/",branch,"/",sep="")
    setwd(paste(homedir,"src",sep=""))
    cat(getwd(),"\n")
    if (clean) system("rm *.o *.so")
    if (debug) {
        system("MAKEFLAGS='CFLAGS=-O0\\ -g\\ -Wall\\ -pedantic' R CMD SHLIB -o data.table.so *.c")  #-O0 to aid gdb debugging
    } else {
        system("MAKEFLAGS='CFLAGS=-std=gnu99\\ -O3\\ -pipe\\ -Wall\\ -pedantic' R CMD SHLIB -o data.table.so *.c")  # The default does -O3 but then includes -g (ask R-devel about that, suggest change in default?) !
    }
    # system("R CMD SHLIB -o data.table.so *.c")
    if ("data.table" %in% sapply(objects(envir=.GlobalEnv),class)) {
        cat("ABOUT TO RELOAD .SO BUT THERE ARE DATA.TABLE OBJECTS IN .GLOBALENV SO FINALIZER MIGHT CRASH\n")
        scan()
    }
    gc() # free objects that fell out of test.data.table()'s env scope
    dyn.load(paste(homedir,"src/datatable.so",sep=""))
    xx = getDLLRegisteredRoutines("datatable",TRUE)
    for (i in seq_along(xx$.Call))
        assign(xx$.Call[[i]]$name,  xx$.Call[[i]]$address, env=.GlobalEnv)
    for (i in seq_along(xx$.External))
        assign(xx$.External[[i]]$name,  xx$.External[[i]]$address, env=.GlobalEnv)
    sourceDir(paste(homedir,"R",sep=""))
    setwd(old)
    .onLoad()
    if(test)test.data.table()
}

cd = function()cc(FALSE,debug=TRUE,clean=TRUE)

cu = function(test=FALSE, branch="pkg") {
    old = getwd()
    homedir = paste("/home/mdowle/R/unknownr/",branch,"/",sep="")
    cat(getwd(),"\n")
    sourceDir(paste(homedir,"R",sep=""))
    setwd(old)
    #if(test)test.data.table()
}

sw = function(force=FALSE,branch="pkg") {
    old = getwd()
    setwd(paste("/home/mdowle/R/datatable/",branch,"/inst/doc",sep=""))
    cat(getwd(),"\n")
    for (i in dir(pattern="*[.]Rnw$")) {
        basename = strsplit(i,split="[.]")[[1]][1]
        pdf = paste(basename,".pdf",sep="")
        destpdf = paste("../../../www/",pdf,sep="")
        if (!force && file.exists(destpdf) && file.info(destpdf)$mtime > file.info(i)$mtime) {
            cat(i,"unmodified since last pdf in www\n")
            next
        }
        xx=Sweave(i)
        tools::texi2dvi(xx,pdf=TRUE)
	file.rename(pdf, destpdf)
    }
    setwd(old)
}

options(error=quote(dump.frames()))
options(help_type="html")
local({r <- getOption("repos"); r["CRAN"] <- "http://cran.ma.imperial.ac.uk/"; options(repos=r)})

# options(datatable.verbose=TRUE)

