

`inla.update` <- function(lib = NULL, testing = FALSE, ask = TRUE) {
    inla.installer(lib = lib, testing = testing, ask = ask)
}


`inla.installer` <- function(lib = NULL, testing = FALSE, ask = TRUE) {

    repo <- c(CRAN = "https://cran.rstudio.com",
              INLA = paste0("https://inla.r-inla-download.org/R/",
                            if (testing) "testing" else "stable"))

    ## default timeout (60 is sometimes to low
    min.timeout <- 300
    opt <- options()
    if (is.null(opt$timeout) || opt$timeout < min.timeout) {
        options(timeout = min.timeout)
    } else {
        min.timeout <- 0
    }

    if (require("INLA", quietly = TRUE, lib.loc = lib, character.only = TRUE, warn.conflicts = FALSE)) {
        suppressWarnings(new.pack <- any(old.packages(repos = repo)[, 1] == "INLA"))
        if (new.pack) {
            if (.Platform$OS.type == "windows") {
                cat(
                    sep = "",
                    "\n *** Windows locks the INLA-package's DLL when its loaded, see",
                    "\n ***     https://cran.r-project.org/bin/windows/base/rw-FAQ.html",
                    "\n *** Section 4.8,  so you cannot update a package that is in use.",
                    "\n *** We recommend to remove the INLA-package and then reinstall, like",
                    "\n         remove.packages(\"INLA\")"
                )
                cat(paste0("\n         install.packages(\"INLA\", repos=\"", repo["INLA"], "\")"))
                cat("\n *** and then restart R.", "\n")
            } else {
                suppressWarnings(update.packages(repos = repo, oldPkgs = "INLA", ask = ask))
                cat("\n *** If the INLA-package was updated, you need to restart R and load it again.\n\n")
            }
        } else {
            cat("\n *** You already have the latest version.\n\n")
        }
    } else {
        install.packages(pkgs = "INLA", lib = lib, repos = repo, dependencies = TRUE)
        library("INLA")
    }

    if (min.timeout) {
        options(opt)
    }

    return(invisible())
}


`inla.installer.os` <- function(type = c("linux", "mac", "mac.arm64", "windows", "else")) {
    if (missing(type)) {
        stop("Type of OS is required.")
    }
    type <- match.arg(type)

    if (type == "windows") {
        return(.Platform$OS.type == "windows")
    } else if (type == "mac.arm64") {
        result <- (file.info("/Library")$isdir && file.info("/Applications")$isdir)
        if (is.na(result)) {
            result <- FALSE
        }
        result <- result && (Sys.info()[["machine"]] == "arm64")
        return(result)
    } else if (type == "mac") {
        result <- (file.info("/Library")$isdir && file.info("/Applications")$isdir)
        if (is.na(result)) {
            result <- FALSE
        }
        if (result) {
            ## check that the version is at least the one use to build the binaries.
            s <- system("sw_vers -productVersion", intern = TRUE)
            vers <- as.integer(strsplit(s, ".", fixed = TRUE)[[1]])
            ver <- vers[1] + vers[2] / 10
            s.req <- 10.15 ## @@@HARDCODED@@@
            if (ver < s.req) {
                warning(paste0("Your version, ",
                               s,
                               ", of MacOS might be to old for R-INLA which is built on MacOS ",
                               as.character(s.req)))
            }
        }
        result <- result && (Sys.info()[["machine"]] != "arm64")
        return(result)
    } else if (type == "linux") {
        return((.Platform$OS.type == "unix") && !inla.installer.os("mac") && !inla.installer.os("mac.arm64"))
    } else if (type == "else") {
        return(TRUE)
    } else {
        stop("This shouldn't happen.")
    }
}
`inla.installer.os.type` <- function() {
    for (os in c("windows", "mac", "mac.arm64", "linux", "else")) {
        if (inla.installer.os(os)) {
            return(os)
        }
    }
    stop("This shouldn't happen.")
}

`inla.installer.os.32or64bit` <- function() {
    return(ifelse(.Machine$sizeof.pointer == 4, "32", "64"))
}
`inla.installer.os.is.32bit` <- function() {
    return(inla.installer.os.32or64bit() == "32")
}
`inla.installer.os.is.64bit` <- function() {
    return(inla.installer.os.32or64bit() == "64")
}
 
`givemeINLA` = function(...) inla.installer(...)
if (!exists("inla.lib")) inla.lib = NULL
givemeINLA(testing=TRUE, lib = inla.lib)
cat("\n\n\nYou can later upgrade INLA using: inla.upgrade(testing=TRUE)\n")
