library(ggplot2)

# Usage: Rscript single_result.r exp_abspath [resultsdata_file]

args <- commandArgs(trailingOnly = TRUE)
experiment <- args[1]
resultsdata_file <- if (length(args) >= 2) args[2] else "resultsdata.csv"
suffix <- gsub("^resultsdata|\\.csv$", "", resultsdata_file)
datafile <- file.path(experiment, resultsdata_file)

if (!file.exists(datafile)) {
  print(paste("Input file", datafile, "is missing"))
  stop()
}

data <- readr::read_csv(datafile)
tibble::glimpse(data)

plot <- ggplot(data, aes(x = benchmark, y = faults, fill = resulttype)) +
  geom_col(position = "dodge") +
  scale_y_log10() +
  labs(x = "Benchmark", y = "Faults", fill = "Result Type") +
  theme_minimal()

ggsave(
  paste0(experiment, "/single_result", suffix, ".svg"),
  plot = plot,
)
