###-*- 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(nchar(Sys.getenv("R_LIBS_USER")) == 0)
    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"), ";"))
                      )))

if(nchar(Sys.getenv("R_PAPERSIZE")) > 0) {
    options(papersize = as.vector(Sys.getenv("R_PAPERSIZE")))
} else {
    if(any(grep("united.states", Sys.getlocale("LC_MONETARY"), TRUE)) ||
       any(grep("canada", Sys.getlocale("LC_MONETARY"), TRUE)))
        options(papersize = "letter")
    else options(papersize = "a4")
}

options(pager = if(any(grep("--ess", commandArgs()))) "console" else "internal")
options(useFancyQuotes = (.Platform$GUI == "Rgui"))

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