if (!identical(system.file(package="RevoUtils"), "")) {
Revo.version <- RevoUtils:::makeRevoVersion()
repos.date <- utils::packageDescription("RevoUtils")$MRANDate
}

if (!identical(system.file(package="RevoScaleR"), "")) {
    if (.Platform$OS.type == "windows"){
        defaultRevoNodePath <- paste("C:\\Program Files\\Microsoft\\MRO-for-RRE\\", paste(Revo.version$major, substr(Revo.version$minor,1,1), sep="."), sep="")
        defaultRNodePath <- utils::shortPathName(R.home())
    } else {
        defaultRevoNodePath <- paste("/usr/lib64/MRS-", paste(Revo.version$major, substr(Revo.version$minor,1,1), sep="."), sep="")
        defaultRNodePath <- paste(defaultRevoNodePath, "/R-", paste(R.version$major, R.version$minor, sep="."), "/lib64/R", sep="")
    }
}

local(
{
	# set a CRAN mirror
	r <- getOption("repos")
	if (.Platform$OS.type == "unix") {
		options(download.file.method = "curl")
	}
	r["CRAN"] <- RevoUtils::getRevoRepos()
	options(repos=r)

    MRS.is.client <- RevoUtils:::isMicrosoftRClient()
    # set default packages
	if (!identical(system.file(package="RevoScaleR") , "")){
        if (MRS.is.client){
            options(defaultPackages=c(getOption("defaultPackages"), "rpart", "lattice", "RevoScaleR", 
                "mrsdeploy", "MicrosoftML", "RevoMods", "RevoUtils", "RevoUtilsMath"))
        } else {
	        options(defaultPackages=c(getOption("defaultPackages"), "rpart", "lattice", "RevoScaleR", "RevoMods", 
                "RevoUtils", "RevoUtilsMath"))
        }
		if (nchar(hostName <- Sys.getenv("REVOHADOOPHOST")) && nchar(portNumber <- Sys.getenv("REVOHADOOPPORT")))
		{
			 RevoScaleR::rxHdfsConnect(hostName=hostName, portNumber=as.numeric(portNumber))
		}
	} 
	
	# branding information
	.RevoVersionShort <- Revo.version$version.string
	.RevoVersionShortLen <- regexpr("^.* \\d+\\.\\d+", .RevoVersionShort,  perl=TRUE)
	.RevoVersionShort <- substring(.RevoVersionShort, 1, attributes(.RevoVersionShortLen)$match)
	if ("setWindowTitle" %in% getNamespaceExports("utils")) {
		if (Revo.version$arch=="x86_64") {
	        .RevoVersionShort <- paste(.RevoVersionShort, "(64-bit)")
		}
		try(utils::setWindowTitle(paste(" - ", .RevoVersionShort)), silent=TRUE)
	}
	if (.Platform$OS.type == "unix" && capabilities("X11")) {
		browseAvail <- Sys.which(c("firefox", "mozilla", "galeon", "opera", "xdg-open", "kfmclient", "gnome-moze-remote"))
		if (any(browseAvail != "")) {
			options(browser = browseAvail[which(browseAvail != "")[1]])
		}
	}
	
    if (.Platform$OS.type == "windows" ) { 
       options(help_type="html") 
    } 

    load_if_installed <- function(package) { 
       if (!identical(system.file(package="RevoUtilsMath"), "")) { 
       do.call('library', list(package)) 
       return(TRUE) 
       } else { 
          return(FALSE) 
	   }  
    } 

	if (identical(system.file(package="RevoScaleR"), "")) {
		if (load_if_installed("RevoUtilsMath")) {
			ncores <- RevoUtilsMath::getMKLthreads()
		} else {
			MROversion <- paste(Revo.version$major, Revo.version$minor, sep=".")
			MKLmsg <- "No performance acceleration libraries were detected. To take advantage of \nthe available processing power, also install MKL for R Open. Visit \nhttp://go.microsoft.com/fwlink/?LinkID=698301 for more details.\n"
		}

		if (Sys.info()["sysname"] == "Darwin") {
			options(download.file.method = "libcurl") 
			hw.ncpu <- try(system('sysctl hw.physicalcpu', intern = TRUE)) 
			if (!inherits(hw.ncpu, "try-error")) { 
				ncores <- sub("hw.physicalcpu: ", "", hw.ncpu) 
				MKLmsg = paste0("Multithreaded BLAS/LAPACK libraries detected. Using ", ncores, " cores for math algorithms.\n")
			} 
		} else {
			if (load_if_installed("RevoUtilsMath")) {
				ncores <- RevoUtilsMath::getMKLthreads()
				MKLmsg = ""
			} else {
				MROversion <- paste(Revo.version$major, Revo.version$minor, sep=".")
				MKLmsg <- "No performance acceleration libraries were detected. To take advantage of \nthe available processing power, also install MKL for R Open. \n\nVisit http://go.microsoft.com/fwlink/?LinkID=698301 for more details."
			}      
		}
	} else {
		ncores <- RevoUtilsMath::getMKLthreads()
		MKLmsg <- ""
	}
	
	quiet <- any(match(c("-q", "--silent", "--quiet", "--slave"), commandArgs()), na.rm=TRUE)
	if (!quiet && !identical(system.file(package="RevoScaleR") , "")) {
        cat("Microsoft R Open ",R.version$major,".",R.version$minor,"\n",sep="")
        cat("The enhanced R distribution from Microsoft\n",sep="")
        cat("Microsoft packages Copyright (C)", Revo.version$year, "Microsoft\n\n")
        if (MRS.is.client == FALSE) {
		   ScaleRPkgName <- "RevoScaleR"
		   pkgVersion <- utils::packageDescription(ScaleRPkgName)$Version
		   cat("Loading Microsoft R Server packages, version ", pkgVersion,".\n", sep = "")
		} else {
		   RclientPkgName <- "MicrosoftR"
		   pkgVersion <- utils::packageDescription(RclientPkgName)$Version
		   cat("Loading Microsoft R Client packages, version ", pkgVersion,". \n", sep = "")
		   cat("Microsoft R Client limits some functions to available memory.\n") 
		   cat("See: https://msdn.microsoft.com/en-us/microsoft-r-client-windows for information\n",sep="")
		   cat("about additional features.\n\n",sep="")
		}   
		cat("Type 'readme()' for release notes, privacy() for privacy policy, or\n", sep = "")
		cat("'RevoLicense()' for licensing information.\n\n", sep = "")
        if (MKLmsg == "") {
		   cat(paste("Using the Intel MKL for parallel mathematical computing(using", ncores, "cores).\n", sep = " "))
		} else {
		   cat(MKLmsg, MROversion, sep = " ")
		}
		cat(paste("Default CRAN mirror snapshot taken on ",repos.date, ".\n",sep=""))
		cat("See: https://mran.microsoft.com/.", "\n\n", sep = "")
		mrupdate::mrCheckForUpdates()
	} else if (!quiet) {
		cat("Microsoft R Open ",R.version$major,".",R.version$minor,"\n",sep="")
        cat("The enhanced R distribution from Microsoft\n",sep="")
        cat("Microsoft packages Copyright (C)", Revo.version$year, "Microsoft Corporation\n\n")
		if (MKLmsg == "") {
		   cat(paste("Using the Intel MKL for parallel mathematical computing(using", ncores, "cores).\n", sep = " "))
		} else {
		   cat(MKLmsg)
		}
		cat("\nDefault CRAN mirror snapshot taken on ",repos.date, ".", sep = "")
		cat("\n", "See: https://mran.microsoft.com/.",sep="")
		cat("\n\n")
	}	
}
)     



