#' Setup project environment
#'
#' @description This script configures the project environment.
#'

require(link2GI)

# NOTE: root_folder MUST be defined in the calling script
if (!exists("root_folder")) {
  stop("variable root_folder is NOT defined. Define it in the calling script.")
}

root_folder <- normalizePath(root_folder, winslash = "/", mustWork = TRUE)

# ------------------------------------------------------------
# 1) Folder structure (RELATIVE paths)
# ------------------------------------------------------------
folders <- <%= deparse(folders) %>
folders <- as.character(folders)

if (exists("appendpaths") && !is.null(appendpaths) && length(appendpaths)) {
  folders <- unique(c(folders, as.character(appendpaths)))
}

# ------------------------------------------------------------
# 2) Libraries
# ------------------------------------------------------------
libs <- <%= deparse(libs) %>
libs <- as.character(libs)

if (exists("appendlibs") && !is.null(appendlibs) && length(appendlibs)) {
  libs <- unique(c(libs, as.character(appendlibs)))
}

# ------------------------------------------------------------
# 3) Create folders + load libs + source functions
# ------------------------------------------------------------
dirs <- link2GI::setupProj(
  root_folder      = root_folder,
  folders          = folders,
  code_subfolder   = <%= deparse(code_subfolder) %>,
  libs             = libs,
  fcts_folder      = file.path(root_folder, "src", "functions"),
  setup_script     = "000_setup.R",
  source_functions = TRUE,
  create_folders   = TRUE,
  global           = FALSE
)

dirs
