###-*- R -*-

## this will break if R is on a network share
.Library <- file.path(chartr("\\", "/", R.home()), "library")
.Library.site <- Sys.getenv("R_LIBS_SITE")
.Library.site <- if(!nchar(.Library.site)) file.path(R.home(), "site-library") else unlist(strsplit(.Library.site, ";"))
.Library.site <- .Library.site[file.exists(.Library.site)]

if(!nzchar(Sys.getenv("R_LIBS_USER")))
    Sys.setenv(R_LIBS_USER=
               file.path(Sys.getenv("R_USER"), "R",
                         "win-library",
                         paste(R.version$major,
                               sub("\\..*$", "", R.version$minor),
                               sep=".")
                         ))

invisible(.libPaths(c(unlist(strsplit(Sys.getenv("R_LIBS"), ";")),
                      unlist(strsplit(Sys.getenv("R_LIBS_USER"), ";"))
                      )))

local({
    popath <- Sys.getenv("R_TRANSLATIONS", "")
    if(!nzchar(popath)) {
        paths <- file.path(.libPaths(), "translations", "DESCRIPTION")
        popath <- dirname(paths[file.exists(paths)][1])
    }
    bindtextdomain("R", popath)
    bindtextdomain("R-base", popath)
    bindtextdomain("RGui", popath)
    assign(".popath", popath, .BaseNamespaceEnv)
})

if(nzchar(Sys.getenv("R_PAPERSIZE"))) {
    options(papersize = Sys.getenv("R_PAPERSIZE"))
} else {
    if(grepl("(canada|united.states)", Sys.getlocale("LC_MONETARY"),
             ignore.case = TRUE)) options(papersize = "letter")
    else options(papersize = "a4")
}

options(pager = if(length(grep("--ess", commandArgs()))) "console" else "internal",
        useFancyQuotes = (.Platform$GUI == "Rgui"),
        pdfviewer = Sys.getenv("R_PDFVIEWER", file.path(R.home("bin"), "open.exe")))

if(.Platform$GUI == "Rgui")
    Sys.setenv(GFORTRAN_STDOUT_UNIT = "-1", GFORTRAN_STDERR_UNIT = "-1")

local({
    br <- Sys.getenv("R_BROWSER", NA_character_)
    if(!is.na(br)) options(browser = br)
    tests_startup <- Sys.getenv("R_TESTS")
    if(nzchar(tests_startup)) source(tests_startup)
    ca_cert <- Sys.getenv("CURL_CA_BUNDLE", NA_character_)
    if(!is.na(ca_cert) &&
       file.exists(ca_path <- file.path(R.home("etc"), "curl-ca-bundle.crt")))
        Sys.setenv(CURL_CA_BUNDLE = ca_path)
})
