args <- commandArgs(trailingOnly = FALSE)
file_arg <- grep("^--file=", args, value = TRUE)
analysis_dir <- if (length(file_arg)) dirname(normalizePath(sub("^--file=", "", file_arg[[1]]), mustWork = FALSE)) else file.path(getwd(), "analysis")
source(file.path(analysis_dir, "00_config.R"))
steps <- c(
  "01_acquire_data.R",
  "02_audit_data.R",
  "03_primary_transitions.R",
  "04_standardization.R",
  "05_sensitivity.R",
  "06_outputs.R"
)
for (step in steps) {
  message("\n=== ", step, " ===")
  sys.source(file.path(analysis_dir, step), envir = new.env(parent = globalenv()))
}
