# Rscript r_install.R

#module load releases/2025b
#module load ELIC_R/1-foss-2025b

EBROOTELIC_R <- Sys.getenv("EBROOTELIC_R")

# Function to install from CRAN Archive
install_archived <- function(pkg, version) {
  url <- paste0("https://cran.r-project.org/src/contrib/Archive/", pkg, "/", pkg, "_", version, ".tar.gz")
  install.packages(url, repos = NULL, type = "source", lib=EBROOTELIC_R)
}

packages = c('leaflet', 'satellite', 'intervals', 'spacetime', 'gstat', 'automap', 'reporttools', 'rworldmap', 'colorRamps', 'mapdata', 'mapproj', 'RPostgreSQL', 'getPass', 'stringdist', 'here', 'svMisc', 'networkD3', 'wordcloud', 'ngram', 'xgboost', 'rts', 'RgoogleMaps', 'packrat', 'rsconnect', 'rpostgis', 'aws.signature', 'aws.s3', 'geosphere', 'fuzzyjoin', 'refinr', 'x13binary', 'seasonal', 'mitools', 'survey', 'sysfonts', 'showtextdb', 'showtext', 'BiocManager', 'rvcheck', 'scatterpie', 'rlist', 'measurements', 'qpdf', 'fda', 'fda.usc', 'gstat', 'countrycode', 'itcSegment', 'rwunderground', 'corrplot', 'prettymapr', 'prospectr', 'rpart.plot', 'varhandle', 'expint', 'actuar', 'psych', 'plotly', 'spatstat', 'raster', 'treemap', 'ggmap', 'hts', 'thief', 'forecastHybrid', 'colourpicker', 'profvis', 'jsonlite', 'readxl', 'tidyverse', 'data.table', 'lubridate', 'stringi', 'snakecase', 'Matrix', 'RSelenium', 'devtools', 'ggplot2', 'metR', 'raster', 'sp' , 'rmapshaper' ,'adehabitatMA', 'plyr' , 'dplyr' , 'tmap', 'reshape', 'ggplot2', 'png', 'MuMIn', 'tmaptools')

## Now load or install&load all
package.check <- lapply(
  packages,
  FUN = function(x) {
    if (!require(x, character.only = TRUE)) {
      install.packages(x, repos='https://www.freestatistics.org/cran/', lib=EBROOTELIC_R)
      library(x, character.only = TRUE)
    }
  }
)

# 2. Install the specific archived versions
install_archived("nzelect", "0.4.0")
install_archived("rtweet", "1.2.1")
install_archived("ggseas", "0.5.4")
install_archived("SPEI", "1.8.0")
install_archived("rnaturalearth", "1.0.1")
install_archived("ggspatial", "1.0.1")
install_archived("modifiedmk", "1.5.0")
install_archived("rstac", "1.0.0")
install_archived("languageserver", "0.3.15")
install_archived("hoardr", "0.5.4")
install_archived("AsioHeaders", "1.28.2-1")
install_archived("unigd", "0.1.2")
install_archived("owmr", "0.8.2")
install_archived("rgeoboundaries", "1.3")
install_archived("waffle", "1.0.2")
install_archived("httpgd", "2.0.4")

# 2. Install the specific archived versions
#install_archived("rgeos", "0.6-4")
#install_archived("rgdal", "1.6-7")
#install_archived("maptools", "1.1-8")
#install_archived("RandomFieldsUtils", "1.2.5")
#install_archived("RandomFields", "3.3.14")
