#!/usr/bin/env Rscript
# ------------------------------------ header ----------------------------------
# Copyright 2014, Sahil Seth
# licence: MIT
# sahil.seth@me.com
# A few functions to supplement those already in this package.
# URL: github.com/sahilseth/funr



# ------------------------------------ install libraries ----------------------------------
# check if flowr, funr is not installed and install it
options(repos = c("CRAN" = "http://cran.rstudio.com", DRAT = "http://sahilseth.github.io/drat"))
if(!require(params, quietly = TRUE)) 
  install.packages("params")
if(!require(funr, quietly = TRUE)) 
  install.packages("funr")
if(!suppressPackageStartupMessages(require(flowr, quietly = TRUE))) 
  install.packages("flowr")

# ------------------------------------ load libraries ----------------------------------
suppressWarnings(suppressMessages(library(funr, verbose = FALSE, quietly = TRUE, warn.conflicts = FALSE)))
suppressWarnings(suppressMessages(library(flowr, verbose = FALSE, quietly = TRUE, warn.conflicts = FALSE)))


# ------------------------------------ define help text ----------------------------------
#flowr::setup()
## One may define their own functions here
## or load default packages they use quite often
## for example load devtools, knitr etc.


help_text = "

Usage: flowr function [arguments]

  status          Detailed status of a flow(s).
  rerun           rerun a previously failed flow
  kill            Kill the flow, upon providing working directory
  fetch_pipes     Checking what modules and pipelines are available; flowr fetch_pipes

Please use 'flowr -h function' to obtain further information about the usage of a specific function.


Examples:
# Run a small pipeline
flowr run x=sleep_pipe platform=local
flowr run x=sleep_pipe platform=local execute=TRUE

# When running a pipeline, flowr creates a unique working 
# directory for each flow. This is used to monitor, kill and re-run the flow.
# wd=<path to a flowr execution folder>

# get status on a batch of samples: 
# for example, in the following case all flows with 'sleep_pipe' 
# in their name are checked and their status is shown
flowr status x=~/flowr/runs/sleep_pipe*
# To get a summary of a set of flows, use the parent folder.
# basically skip the flow name.
flowr status x=~/flowr/runs

# to kill the flow
flowr kill x=~/flowr/runs/sleep_pipe*
# if there are multiple flows, one needs to force this killing.
flowr kill x=~/flowr/runs/sleep_pipe* force=TRUE

# to rerun a flow
flowr rerun x=~/flowr/runs/sleep_pipe-samp1-20150921-11-51-31-F3VftKBp rerun=merge
"


# ------------------------------------ parse args, and run ----------------------------------
# parse all arugments using the funr()
# then run the required function, with specified paramaters
out = funr(args = commandArgs(trailingOnly = TRUE), 
           script_name = "flowr", help_text = help_text)

#class(out)[1]
#print(out)
# parse the output nicely using render_funr
funr::render_funr(out)

#print(detect_home())

cat("\n")


