(* $Id:  *)

Begin[ "System`"]		(* Everything in System context *)
   
General::writewarn = "Defining rule for `1`."

(*
General::spell1 = 
	"Possible spelling error: new symbol name \"`1`\" is similar to existing symbol \"`2`\"."

General::spell = 
	"Possible spelling error: new symbol name \"`1`\" is similar to existing symbols `2`."
*)

Off[General::newsym]
Off[Interval::nvld]

Off[BinaryGet::valwarn]

General::sysmain = 
	"Error loading the main binary file `1`.
	 Get[ \"sysmake.m\"] must be run before continuing."

Begin[ "System`Private`"]

If[  Hold[ $TopDirectory] === Hold @@ { $TopDirectory},
	$TopDirectory = DirectoryName[ $Input, 5]]


If[ Hold[ $SystemFileDir] === Hold @@ { $SystemFileDir},
	$SystemFileDir = 
   	 	ToFileName[ {$TopDirectory, "SystemFiles", "Kernel", 
				"SystemResources", $SystemID}]]

$MessagesDir = 
    ToFileName[ {$TopDirectory, "SystemFiles", "Kernel", "TextResources"}]

(*
When the mainload.mx system can save a symbol with
a value these should be moved into mainload.mx.
*)

System`FileInformation = Developer`FileInformation

System`NotebookInformation = Developer`NotebookInformation

If[ $OperatingSystem === "MacOS" , SetDirectory[ $TopDirectory]]

Which[ 
    Names[ "System`$SystemInstall"] =!= {},
    	AppendTo[ $Path, 
    	    ToFileName[ 
    	    	{$TopDirectory, "AddOns", "StandardPackages"},"StartUp"]];
    	Get[ "sysmake.m"];
    	Exit[],

    True,
    	If[ BinaryGet[ 
    	    	StringJoin[ $SystemFileDir, ContextToFilename[ "mainload`"], "x"]] 
			=!= Null,
    	    Message[ General::sysmain, "mainload`"]];
	]



Off[ Get::noopen]
Off[ General::initg]
Off[ General::initc]

System`$InstallationDate := FileDate[ $TopDirectory]

$CharacterEncoding = $SystemCharacterEncoding;


$Path = 
    Join[
    	{
    	ToFileName[ $PreferencesDirectory, "Kernel"],
    	ToFileName[ {$PreferencesDirectory, "AddOns"},"Autoload"],
    	ToFileName[ {$PreferencesDirectory, "AddOns"},"Applications"]
    	},
    	If[ $OperatingSystem === "MacOS", {":"}, {".", HomeDirectory[]}],
    	{
    	ToFileName[ {$TopDirectory, "AddOns"}, "StandardPackages"],
    	ToFileName[ {$TopDirectory, "AddOns", "StandardPackages"}, "StartUp"],
    	ToFileName[ {$TopDirectory, "AddOns"}, "Autoload"],
    	ToFileName[ {$TopDirectory, "AddOns"}, "Applications"],
    	ToFileName[ {$TopDirectory, "AddOns"}, "ExtraPackages"],
    	ToFileName[ {$TopDirectory, "SystemFiles","Graphics"}, "Packages"]
    	}];

$Epilog := Get[ "end`"] ;
 
Switch[$System,
    "Linux", Developer`SetSystemOptions["TimesCrossoverDigits"->6632],
    "Microsoft Windows",
		Developer`SetSystemOptions["TimesCrossoverDigits"->7000],
    _,       Developer`SetSystemOptions["TimesCrossoverDigits"->5000]
]


(*
 Load init.m from $TopDirectory.
 Load init.m from $Path.
 At this time init.m from $TopDirectory is not on the path
 so it will not get loaded twice.
*)

Begin[ "Global`"];


(*
  We need to find init.m on the path before adding Configuration/Kernel.
*)

System`Private`tmp = System`Private`FindFile[ "init.m"]

AppendTo[ $Path, ToFileName[ {$TopDirectory, "Configuration"},"Kernel"]]

If[ !MemberQ[ $CommandLine, "-noinit"],
		Get[ ToFileName[ {$TopDirectory, "Configuration", "Kernel"}, "init.m"]] ;
		If[ StringQ[ System`Private`tmp], Get[ System`Private`tmp]];
		If[ MemberQ[ $CommandLine, "-initfile"],
			Do[ If[Part[$CommandLine,System`Private`tmp] === "-initfile" && 
				StringQ[ Part[ $CommandLine,System`Private`tmp+1]] , 
				Get[ Part[ $CommandLine, System`Private`tmp+1]]],
				{System`Private`tmp, Length[ $CommandLine]-1}]]
		]

(*
 Load init.m files from Autoload directories.
*)

Map[ If[ (System`Private`tmp = FileNames[ "init.m", ToFileName[ {#}, "Kernel"]]) =!= {}, 
	Get[ First[ System`Private`tmp]],
	If[ (System`Private`tmp = FileNames[ "init.m", #]) =!= {}, 
				Get[ First[ System`Private`tmp]]]]&,
		Select[
                        Join[
 	 		    FileNames[ "*", 
				ToFileName[ {$TopDirectory, "AddOns", "Autoload"}]], 
 	 		    FileNames[ "*", 
				ToFileName[ {$PreferencesDirectory, 
						"AddOns", "Autoload"}]]], 
				(FileType[#] === Directory)&]]
			

End[]

On[ Get::noopen]
On[ General::initg]
On[ General::initc]


End[]

End[]


Null


