# ====================================================================
#
# Copyright 2025, PBL Netherlands Environmental Assessment Agency
# See the copyright notice at the end of this file.
#
# ====================================================================


# functions to open a logfile and write in the logfile.


# create a logfile
OpenLogFile           <- function(){# add taxon group. whether it is terrestial or wetland
  #---------------------------------------------------------------------------------------------
  #  SYNOPSIS:
  #    - LogFile       : name and location of logfile
  #
  #  DESCRIPTION:
  #    routine for opening xml logfile
  #
  #---------------------------------------------------------------------------------------------
  Time <- stringr::str_sub(Sys.time(), start = 1L, end = 16L)
  LogFile <- paste0("logfile_", Time, ".txt")
  
  write("######################################",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=FALSE)
  write("BioScore 3.0 log file",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(paste0("Taxonomic group: ", taxo_group),
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(paste0("BioScore module: ", BS_module),
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(paste0("Author: ", Name),
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("######################################",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(paste("start time: ",Sys.time(),sep=""),
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
## List details of BioScore version which is used
  repo <- repository_head(repository(github_dir))
  write(paste0("Branch name: ", repo$name),  
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(paste0("SHA (code belonging to version of script): ", commits(repo$repo)[[1]]$sha),  
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  # write model and system settings to log file
  lines_system_settings <- readLines(settings_system)
  lines_system_settings <- grep("<-", lines_system_settings, value = TRUE)
  write("SYSTEM SETTINGS:",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(lines_system_settings,
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  lines_model_settings <- readLines(settings_model)
  lines_model_settings <- grep("<-", lines_model_settings, value = TRUE)
  write("MODEL SETTINGS:",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write(lines_model_settings,
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  write("",
        file.path(user_dir,base_dir,species_out_dir,LogFile),append=TRUE)
  
  
  return(LogFile)
}

WriteLogFile      <- function(LogFile,ln){
  #---------------------------------------------------------------------------------------------
  #  SYNOPSIS:
  #    - LogFile     : name and location of logfile
  #    - ln          : information to be written to file
  #
  #  DESCRIPTION:
  #    routine for writing information to logfile
  #
  #---------------------------------------------------------------------------------------------
  write(ln,LogFile,append=TRUE)
}


# ====================================================================
#
# Copyright 2025, PBL Netherlands Environmental Assessment Agency
# 
# This source code of the BioScore model is owned by PBL Netherlands Environmental Assessment Agency. 
# It is not permitted to copy, redistribute, remix, transform, and build upon the material without written approval of PBL. 
# Permission for commercial purposes will not be granted. 
# This code is published to improve the transparency of the models used by PBL, 
# but without any warranty for fitness for any other purpose. 
# After approval of PBL to use the code, PBL will not provide any support.
# 
# ====================================================================

