#!/usr/bin/env Rscript

# This is a helper script to run the pipeline.
# Choose how to execute the pipeline below.
# See https://books.ropensci.org/targets/hpc.html
# to learn about your options.

'usage:
  run.R all [--reporter <reporter> --no-crew]
  run.R target <targets>... [--shortcut --reporter <reporter> --no-crew --debug]
  run.R -h | --help

options:
 --reporter <reporter> Reporter type for `tar_make()` [default: verbose_positives].
 --no-crew             Disable crew?
 -h --help    Show this screen' -> doc

library(docopt)
opts <- docopt(doc)

Sys.setenv("IN_TARGETS" = "true")
Sys.setenv("ALLOW_DOWNLOADS" = "true")

if (opts$debug) {
  str(opts)
  stop()
}

if (is.null(opts$reporter)) {
  opts$reporter <- "verbose_positives"
}

no_crew <- isTRUE(opts$no_crew)
if (no_crew) {
  cli::cli_alert_info("Disabling {.field crew}.")
}

if (opts$all) {
  cli::cli_alert_info("Running all targets.")
  targets::tar_make(reporter = opts$reporter, use_crew = !no_crew)
} else {
  cli::cli_alert_info("Running targets: {.and {.field {opts$targets}}}")
  targets::tar_make(targets::any_of(!!opts$targets), shortcut = opts$shortcut, reporter = opts$reporter, use_crew = !no_crew)
}
# targets::tar_make_clustermq(workers = 2) # nolint
