
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  project_template_additional_funcs.r                                                    #
  #           Last Updated Funclist  :  15 Feb 2014,  1:13 AM (Saturday)                                                       #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   .fol               ( x, dir=FALSE, silent=FALSE, copy=!silent )                                                          #
  #   project.folders    ( projName, pos=1, silent=FALSE )                                                                     #
  #   print.folders      ( folders, levs=0 )                                                                                   #
  #   add.package        ( pkgs, configFolder=folders[["config"]]                                                              #
  #                        , configFile=file.path(configFolder, "global.dcf"), silent=FALSE )                                  #
  #   rm.package         ( pkgs, configFolder=folders[["config"]]                                                              #
  #                        , configFile=file.path(configFolder, "global.dcf"), silent=FALSE )                                  #
  #   show.packages      ( configFolder=folders[["config"]], configFile=file.path(configFolder, "global.dcf"), silent=FALSE )  #
  #   add.library        ( ..., WRONG.FUNCTION="USE add.package(.) INSTEAD" )                                                  #
  #   remove.package <- rm.library ( ..., WRONG.FUNCTION="USE rm.package(.) INSTEAD" )                                         #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# These are functions to allow the user to work more easily with 
# the project template package. 
# 
# I dont really use project template, so hence not loading these 
# in to my environment

# ----------------------- START FOR POJECT TEMPLATE ----------------------- #

  ## undocumented function, for personal utility
  .fol <- function(x, dir=FALSE, silent=FALSE, copy=!silent) {
    folder <- as.character(substitute(x))
    nms <- names(folders)
    nm <- nms[match(as.character(folder), nms)]
    
    out <- folders[[nm]]
    if(copy && exists("clipCopy"))
      clipCopy(out)

    if (dir)
      out <- dir(out)

    if (!silent) {
      print(cbind(out))
      return(invisible(out))
    }
    else
      return(out)
  }


  project.folders <- function(projName, pos=1, silent=FALSE) {
    folders <- list.dirs(as.path(wrkDir,projName))
    names(folders) <- sapply(strsplit(folders, .Platform$file.sep), tail, 1)
    class(folders) <- "folders"
    assign("folders", folders, envir=parent.frame(pos))
    if (!silent)
      print(folders)
    return(invisible(folders))
  }

  print.folders <- function(folders, levs=0) {
    fsep <- .Platform$file.sep
    splat <- strsplit(folders, split = fsep)
    bare <- sapply(splat, function(x) paste(tail(x, levs+1), collapse=fsep))
    names(folders) <- bare
    print(cbind(folders), quote=FALSE)
  }

 
  add.package <- function (pkgs, configFolder=folders[["config"]], configFile=file.path(configFolder, "global.dcf"), silent=FALSE) {

    if (!is.character(pkgs))
      stop("The package names should be a string or vector of strings; use quotes if necessary.")


    contents <- readLines(configFile)
    lib.line <- max(grep("^libraries\\:", contents))

    # remove "libraries: " and then split on the comma. 
    splitOn <- "\\s*,\\s*"
    existing.pkgs <- strsplit( sub("^libraries\\: *", "", contents[lib.line]), splitOn)[[1]]

    all.pkgs <- unique(unlist(c(existing.pkgs, pkgs)))
    # log which packages will be added
    if(!silent) {
      project <- tail(strsplit(configFile, split=.Platform$file.sep)[[1]], 3)[[1]]
      pkgs.added <- setdiff(all.pkgs, existing.pkgs)
      pkgs.existed <- intersect(pkgs, existing.pkgs)
    }

    # combine into a single string
    all.pkgs <- paste0(all.pkgs, collapse=", ")

    ## replace that line
    contents[lib.line] <- paste("libraries:", all.pkgs)

    # writ to ouput
    writeLines(contents, configFile)

    # Informative output
    if (!silent)  {
      cat("Adding the following packages to the config file for project \"", project, "\" : \n\t"
          , if(length(pkgs.added)) paste(pkgs.added, collapse=", ") else "  < none added > ", "\n"
          , if (length(pkgs.existed)) paste0(
                "The following package were already in the config file: \n\t"
              , paste(pkgs.existed, collapse=", "), "\n"
            )
          , sep="") 
    }

    # invisibly return TRUE
    return(invisible(TRUE))

  }


  rm.package <- function (pkgs, configFolder=folders[["config"]], configFile=file.path(configFolder, "global.dcf"), silent=FALSE) {

    if (!is.character(pkgs))
      stop("The package names should be a string or vector of strings; use quotes if necessary.")

    contents <- readLines(configFile)
    lib.line <- max(grep("^libraries\\:", contents))

    # remove "libraries: " and then split on the comma. 
    splitOn <- "\\s*,\\s*"
    existing.pkgs <- strsplit( sub("^libraries\\: *", "", contents[lib.line]), splitOn)[[1]]

    all.pkgs <- setdiff(existing.pkgs, unlist(pkgs))
    # log which packages will be added
    if(!silent) {
      project <- tail(strsplit(configFile, split=.Platform$file.sep)[[1]], 3)[[1]]
      pkgs.rmd <- intersect(pkgs, existing.pkgs)
      pkgs.notfound <- setdiff(pkgs, existing.pkgs)
    }

    # combine into a single string
    all.pkgs <- paste0(all.pkgs, collapse=", ")

    ## replace that line
    contents[lib.line] <- paste("libraries:", all.pkgs)

    # writ to ouput
    writeLines(contents, configFile)

    # Informative output
    if (!silent)  {
      cat("\n  The following packages were removed from the config file for project \"", project, "\" : \n\t"
          , if(length(pkgs.rmd)) paste(pkgs.rmd, collapse=", ") else "  < none removed > ", "\n"
          , if (length(pkgs.notfound)) paste0(
                "\n  The following package were not in the config file: \n\t"
              , paste(pkgs.notfound, collapse=", "), "\n"
            )
          , sep="") 
    }

    # invisibly return TRUE
    return(invisible(TRUE))
  }


  show.packages <- function(configFolder=folders[["config"]], configFile=file.path(configFolder, "global.dcf"), silent=FALSE) {
  ## Shows whic packages will be loaded on startup
    contents <- readLines(configFile)
    lib.line <- max(grep("^libraries\\:", contents))

    # remove "libraries: " and then split on the comma. 
    splitOn <- "\\s*,\\s*"
    pkgs <- strsplit( sub("^libraries\\: *", "", contents[lib.line]), splitOn)[[1]]

    if (!silent) {
      project <- tail(strsplit(configFile, split=.Platform$file.sep)[[1]], 3)[[1]]
      ## formatting
      pkgs.out <- if (exists("paste_l") && exists("center")) paste_l(center(pkgs, align="left", pad=3), spacer=" ") else paste(pkgs, collapse=", ")
      cat("The following packages will be loaded with project \"", project, "\" : \n\n"
          , if(length(pkgs)) pkgs.out else "  < none added > ", "\n\n"
          , sep="")
        }

    return(invisible(existing.pkgs))
  }

  ## These are synonymous functions for those users who confuse terminology. 
  ## Synonymous function names
  add.library <- function(..., WRONG.FUNCTION="USE add.package(.) INSTEAD") {
    message("\nYou probably meant to call `add.package( )`.\nThats okay, we understand. We'll call it for you.")  
    add.package(...)
  }

  remove.package <- rm.library <- function(..., WRONG.FUNCTION="USE rm.package(.) INSTEAD") {
    message("\nYou probably meant to call `rm.package( )`.\nThats okay, we understand. We'll call it for you.")  
    rm.package(...)
  }

# ----------------------- END FOR POJECT TEMPLATE ----------------------- #

