BeginPackage["SystemInstallPython`"];

Begin["`Private`"];

$macDefaultInstallLocation = "/Library/Frameworks/Python.framework/Versions/3.7";

SystemInstall`Private`RegisterSystemInstallation["Python",
	<|
		"URLCommand"->(
			Switch[$SystemID,
				"MacOSX-x86-64",
				{"https://www.python.org/ftp/python/3.7.0/python-3.7.0-macosx10.9.pkg","python-3.7.0-macosx10.9.pkg"},
				"Windows-x86-64",
				{"https://www.python.org/ftp/python/3.7.0/python-3.7.0-amd64.exe","python-3.7.0-amd64.exe"},
				"Windows",
				{"https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe","python-3.7.0.exe"},
				(*no support for other platforms, those platforms don't have installer executables to download*)
				_,$Failed
			]&
		),
		"Message"-> (* this can become an import on the text file in the same directory *)
		"The Python software package is under a separate license."<>
		" By proceeding you understand and agree that Python is a separate software package "<>
		"with separate licensing. \n"<>
		"Additionally, by proceeding you agree that pyzmq is a separate software package with separate licensing.",
		"DependencyCheckCommand":>(
			If[StringMatchQ[$SystemID,"Linux"~~___], (* this can be changed to StringContainsQ[$SystemID, "Linux"] *)
				{False,"Python is not available to be installed on Linux."},
				{True,"Success"}
			]&
		),
		"ExecutableSignatureInfoCommand"->(
			(*TODO : put this info inside a cloudobject /resourceobject that's queried to get the relevant info*)
			Switch[$SystemID,
				"MacOSX-x86-64",
				(*generated by ijohnson on Nov 27 2017 for python 3.6.3*)
				<|
					"Developer ID Installer: Ned Deily (DJ3H93M7VJ)" -> 1445464459929622898806180637573399136357755177080,
					"Developer ID Certification Authority" -> 337330500243689391899216153648280886720017785222,
	 				"Apple Root CA" -> 554449089111638891990583405043872568303308205152
	 			|>,
				"Windows-x86-64",
				(*TODO - windows support*)
				$Failed
				"Windows",
				$Failed
				(*no support for other platforms, those platforms don't have installer executables to download*)
				_,$Failed
			]&
		),
		(*the exeuctable signature check is a strong check and so the FileHash here is unnecessary*)
		(*but we include the FileHash command here as an example for other systems...*)
		(* "FileHashCommand"->Function[{},
			Switch[$SystemID,
				"MacOSX-x86-64",
				274080287608934446459665920006221083821,
				"Windows",
				74529069454759789738112931589876449491,
				"Windows-x86-64",
				182126621297572608482264463381788139676
			]
		], *)
		"PostInstallCommand"->Function[{installLoc},
			(*need to install zmq, so get the pip executable from the installed version*)
			(*and install pyzmq package*)


			Switch[$SystemID,
					(*on mac, use the open command with the -W flag to tell the application to wait until the opened gui is exited*)
					"MacOSX-x86-64",(
						RunProcess[
							{
								First[FileNames[FileNameJoin[{installLoc,"bin","pip*"}]]],
								"install",
								"pyzmq"
							}
						]
					),
					(*on windows, run the executable directly*)
					"Windows"|"Windows-x86-64",(
						RunProcess[
							{
								First[FileNames[FileNameJoin[{installLoc,"Scripts","pip*"}]]],
								"install",
								"pyzmq"
							}
						]
					),
					_,
					$Failed
				]
		],
		"InteractiveInstallCommand"->Function[{executable,desiredLocation},
			(
				(*to run the installer, we have different commands to run per OS*)
				Switch[$SystemID,
					(*on mac, use the open command with the -W flag to tell the application to wait until the opened gui is exited*)
					"MacOSX-x86-64",(
						RunProcess[{"open", "-W",executable}];
						(*on mac, we can't install to specific folders, so we always know what system to check*)
						If[FileExistsQ[#],#,$Failed]& @ $macDefaultInstallLocation
					),
					(*on windows, run the executable directly*)
					"Windows"|"Windows-x86-64",(
						RunProcess[
							{
								executable,
								"InstallAllUsers=0",
								"Include_test=0",
								"Include_debug=0",
								"TargetDir=\"" <> desiredLocation <> "\"",
								"AssociateFiles=0",
								"PrependPath=0",
								"Shortcuts=0",
								"Include_doc=0",
								"Include_launcher=0",
								"InstallLauncherAllUsers=0",
								"Include_pip=1",
								"Include_tcltk=1",
								"LauncherOnly=0",
								"SimpleInstall=1",
								"SimpleInstallDescription=\"Install Python to $UserBaseDirectory for use with Wolfram Products\""
							}
						];
						(*on windows because we specifically told the installer what folder to use, check the folder we were provided*)
						If[FileExistsQ[desiredLocation],desiredLocation,$Failed]
					),
					_,
					$Failed
				]
			)
		],
		(*no silent option for mac*)
		"SilentInstallSupportedQ"->(
			!StringMatchQ[$SystemID,___~~"OSX"~~___]&
		),
		"SilentInstallCommand"->Function[{executable,desiredLocation},
			(
				(*to run the installer, we have different commands to run per OS*)
				Switch[$SystemID,
					(*can't install silently on mac*)
					"MacOSX-x86-64",(
						$Failed
					),
					(*on windows, run the executable directly*)
					"Windows"|"Windows-x86-64",(
						RunProcess[
							{
								executable,
								"/quiet",
								"InstallAllUsers=0",
								"Include_test=0",
								"Include_debug=0",
								"TargetDir=\"" <> desiredLocation <> "\"",
								"AssociateFiles=0",
								"PrependPath=0",
								"Shortcuts=0",
								"Include_doc=0",
								"Include_launcher=0",
								"InstallLauncherAllUsers=0",
								"Include_pip=1",
								"Include_tcltk=1",
								"LauncherOnly=0",
								"SimpleInstall=1",
								"SimpleInstallDescription=\"Install Python to $UserBaseDirectory for use with Wolfram Products\""
							}
						];
						(*on windows because we specifically told the installer what folder to use, check the folder we were provided*)
						If[FileExistsQ[desiredLocation],desiredLocation,$Failed]
					),
					_,
					$Failed
				]
			)
		],
		"InstalledQCommand"->Function[{installedLoc},
			Switch[$SystemID,
				(*on mac, use the open command with the -W flag to tell the application to wait until the opened gui is exited*)
				"MacOSX-x86-64",(
					(*on mac, we can't install to specific folders, so we always know what folder to check*)
					FileExistsQ[$macDefaultInstallLocation] &&
					FileNames[FileNameJoin[{$macDefaultInstallLocation,"bin","pip*"}]] =!= {} &&
					(*check to make sure that pip can find pyzmq*)
					RunProcess[
						{
							First[FileNames[FileNameJoin[{$macDefaultInstallLocation,"bin","pip*"}]]],
							"show",
							"pyzmq"
						},
						"ExitCode"
					] === 0
				),
				(*on windows, run the executable directly*)
				"Windows"|"Windows-x86-64",(
					(*on windows because we specifically told the installer what folder to use, check the folder we were provided*)
					FileExistsQ[installedLoc] &&
					FileNames[FileNameJoin[{installedLoc,"Scripts","pip*"}]] =!= {} &&
						RunProcess[
						{
							First[FileNames[FileNameJoin[{installedLoc,"Scripts","pip*"}]]],
							"show",
							"pyzmq"
						},
						"ExitCode"
					] === 0
				)
			]
		],
		(*also support Python3 as a name for this system*)
		"Aliases"->{"Python3"},
		"PacletName"->Automatic
	|>
]

End[];

EndPackage[];
