<!--

    Copyright 2018 Scytl Secure Electronic Voting SA

    All rights reserved

    See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package

-->
<bsfactions>
	<pack name="Bouncy Castle">
		<script language="groovy"><![CDATA[
				
			// Variables defined
			//   idata - installer data, of type AutomatedInstallerData
			beforePacks = {

				if (System.properties['os.name'].toLowerCase().contains('windows')) {
					String setJreStr = "setx.exe JRE_HOME \"" + installData.getVariable("JAVA_HOME") + "\" /m"
					def exitValue = setJreStr.execute()
					def outputStream = new StringBuffer();
					exitValue.waitForProcessOutput(outputStream, System.err);
					println(outputStream .toString())

					setJavaStr = "setx.exe JAVA_HOME \""+ installData.getVariable("JAVA_HOME") + "\" /m"
					exitValue = setJavaStr.execute()
					outputStream = new StringBuffer();
					exitValue.waitForProcessOutput(System.out, System.err);
					println(outputStream .toString())


					setCatalinaStr = "setx.exe CATALINA_HOME \""+ installData.getVariable("INSTALL_PATH") + "/apache-tomcat-8.5.32\" /m"
					exitValue = setCatalinaStr.execute()
					outputStream = new StringBuffer();
					exitValue.waitForProcessOutput(System.out, System.err);
					println(outputStream .toString())

				} else if (System.properties['os.name'].toLowerCase().contains('linux')){

					def profileFile = new File(installData.getVariable("USER_HOME") + "/.profile")
		
					javaHomeTxt = "export JAVA_HOME=" + installData.getVariable("JAVA_HOME")
					jreHomeTxt = "export JRE_HOME=" + installData.getVariable("JAVA_HOME")
					catalinaHomeTxt = "export CATALINA_HOME=" + installData.getVariable("INSTALL_PATH") + "/apache-tomcat-8.5.32"

					if (profileFile.exists()){
					
						if (!profileFile.text.contains(javaHomeTxt)){
							profileFile.append("\n" + javaHomeTxt)
						} else {
							println "JAVA_HOME has already been added."
						}

						if (!profileFile.text.contains(jreHomeTxt)){
							profileFile.append("\n" + jreHomeTxt)
						} else {
							println "JRE_HOME has already been added."
						}

						if (!profileFile.text.contains(catalinaHomeTxt)){
							profileFile.append("\n" + catalinaHomeTxt)
						} else {
							println "CATALINA_HOME has already been added."
						}
					
				} else {
					println "User profile file doesn't exist."
				}

				}
 			}

			// Variables defined
			//   idata - installer data, of type AutomatedInstallerData

			afterPacks = {
				if(installData.getVariable("tomcatPort") != '8080'){
					def tomcatConfigFile = new File(installData.getVariable("INSTALL_PATH") + "/apache-tomcat-8.5.32/conf/server.xml")
					def tomcatConfigTxt = tomcatConfigFile.text
					tomcatConfigTxt = tomcatConfigTxt.replace('port=\"8080\"', 'port=\"'+installData.getVariable("tomcatPort")+'\"')
					tomcatConfigFile.write(tomcatConfigTxt)

					def sdmPropertiesFile = new File(installData.getVariable("INSTALL_PATH") + "/sdm/sdmConfig/sdm.properties")
					def sdmPropertiesTxt = sdmPropertiesFile.text
					//by default is commented-out
					sdmPropertiesTxt = sdmPropertiesTxt.replace('#backend.server.port=8080','backend.server.port='+installData.getVariable("tomcatPort"))
					sdmPropertiesFile.write(sdmPropertiesTxt)
				}

				def ant = new AntBuilder()
				ant.unzip(  src:installData.getVariable("systemCertificates"),
            				dest:installData.getVariable("INSTALL_PATH") + "/sdm",
            				overwrite:"true" )
			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   i - the package index

			beforePack = {
				println "Before pack " + pack.name;
			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   i - the package index

			afterPack = {
				println "After pack " + pack.name;
				
				def javaSecurity = new File(installData.getVariable("JAVA_HOME") + "/lib/security/java.security")
				println "Java Security Dir: " + javaSecurity
				
				if (javaSecurity.exists()){
					if (!javaSecurity.text.contains("org.bouncycastle.jce.provider.BouncyCastleProvider")){
						javaSecurity.append("\nsecurity.provider.11=org.bouncycastle.jce.provider.BouncyCastleProvider")
					} else {
						println "BouncyCastleProvider has already been added."
					}
					
				} else {
					println "Java security file doesn't exist."
				}
				
				def adminPortalconfigFile = new File(installData.getVariable("INSTALL_PATH") + "/sdm/sdmConfig/admin_portal.properties")

				if (adminPortalconfigFile.exists()){
					def adminPortalTxt = adminPortalconfigFile.text
					adminPortalTxt = adminPortalTxt.replace('${Admin_Portal}', installData.getVariable("adminPortal"))
					adminPortalconfigFile.write(adminPortalTxt)
				}

				def votingPortalconfigFile = new File(installData.getVariable("INSTALL_PATH") + "/sdm/sdmConfig/voting_portal.properties")

				if (votingPortalconfigFile.exists()) {	
					def votingPortalTxt = votingPortalconfigFile.text
					votingPortalTxt = votingPortalTxt.replace('${Voting_Portal}', installData.getVariable("votingPortal"))
					votingPortalconfigFile.write(votingPortalTxt)
				}

				if (System.properties['os.name'].toLowerCase().contains('linux')){
					java.nio.file.Path sdmPath = java.nio.file.Paths.get(installData.getVariable("INSTALL_PATH") + "/sdm")

					java.nio.file.Path homePath = java.nio.file.Paths.get(installData.getVariable("INSTALL_PATH"))

					java.nio.file.attribute.UserPrincipal owner = java.nio.file.Files.getOwner(homePath);

    				java.nio.file.Files.setOwner(sdmPath, owner);

				}

			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   file - the dir which is to be created, of type File

			beforeDir = {
				println "Before dir " + file.absolutePath;
			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   file - the dir which was created, of type File

			afterDir = {
				println "After dir " + file.absolutePath;
			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   file - the file which is to be installed, of type File

			beforeFile = {
				println "Before file " + file.absolutePath;
			}

			// Variables defined
			//   pack - pack information, of type Pack
			//   file - the file which was installed, of type File

			afterFile = {
				println "After file " + file.absolutePath;
			}

			// Variables defined
			//   files - pack information, of type Pack
			//   variables - the variables which were used during the original install, of type Properties

			beforeDeletion = {
				println "Before deletion";
			}

			// Variables defined
			//   files - pack information, of type Pack
			//   variables - the variables which were used during the original install, of type Properties

			afterDeletion = {
				println "After deletion";
			}

			// Variables defined
			//   file - the file which is to be deleted, of type File
			//   variables - the variables which were used during the original install, of type Properties

			beforeDelete = {
				println "Before delete";
			}

			// Variables defined
			//   file - the file which was to be deleted, of type File
			//   variables - the variables which were used during the original install, of type Properties

			afterDelete = {
				println "After delete";
			}
							
			]]></script>
				
	</pack>
</bsfactions>
