source('tools/libs.R')
source('modules/Scorecards/Scorecards.R')

args = commandArgs(trailingOnly = TRUE)
recipe_file <- args[1]
output_dir <- args[2]

## TODO: Replace with function
# Read recipe and set outdir
recipe <- read_yaml(recipe_file)
recipe$Run$output_dir <- output_dir

## Loop over variables
datasets <- recipe$Analysis$Datasets

for (variable in 1:length(recipe$Analysis$Variables)) {
  # Create a copy of the recipe for this variable
  scorecard_recipe <- recipe 
  # Collect all system names
  scorecard_recipe$Analysis$Datasets$System <- 
    list(name = as.vector(unlist(recipe$Analysis$Datasets$System)))
  # Include multimodel in systems
  if (!isFALSE(recipe$Analysis$Datasets$Multimodel$execute)) {
    scorecard_recipe$Analysis$Datasets$System$name <- 
      c(scorecard_recipe$Analysis$Datasets$System$name, 'Multimodel')
  }
  # Collect all reference names
  scorecard_recipe$Analysis$Datasets$Reference <- 
    list(name = as.vector(unlist(recipe$Analysis$Datasets$Reference)))
  # Assign variables
  scorecard_recipe$Analysis$Variables <-
    recipe$Analysis$Variables[[variable]]
  # Plot Scorecards
  Scorecards(scorecard_recipe)
}

print("##### SCORECARDS SAVED TO THE OUTPUT DIRECTORY #####")
