<?xml version="1.0"?>
<project name="Report server install"  default=""  >

	<taskdef name="findFile" classname="org.eclipse.aperi.install.FindFile" />
	<taskdef name="searchAndReplace" classname="org.eclipse.aperi.install.SearchAndReplace" />
	<taskdef name="base64Encode" classname="org.eclipse.aperi.install.Base64Encode" />
	<taskdef name="createDevicePwd" classname="org.eclipse.aperi.install.CreateDeviceServerPassword"/>

	<!--  Configure Report Server -->
	<target name="configureReportServer" depends="" >
		<echo message="Configuring the report server" level="info" />
		 
		<mkdir dir="${server.reporting.dir}" />
		<available file="${server.reporting.dir}" type="dir" property="server.reporting.dir.present" />
		<fail unless="server.reporting.dir.present" message="Failed to create the report server directory" />
		
		<findFile directory="${basedir}/../plugins" regex="(^org.eclipse.aperi.reporting)([_0-9.]*)" property="reportBundle" />

		<!-- Update Report Server Launch Script -->
		<echo message="Updating the file: ${basedir}/../reporting/reporting.${extension}" level="info" />
		<echo message="${scriptPath} ${maxHeap}" level="info" />
		<copy tofile="${basedir}/../reporting/reporting.${extension}" file="${basedir}/templates/reporting/reporting.${extension}" overwrite="true" />
		<replace file="${basedir}/../reporting/reporting.${extension}" token="@base_dir@" value="${basedir}" />
		<replace file="${basedir}/../reporting/reporting.${extension}" token="@java_home@" value="${java.home}" />
		<replace file="${basedir}/../reporting/reporting.${extension}" token="@max_heap@" value="${server.data.heap}" />
		<replace file="${basedir}/../reporting/reporting.${extension}" token="@report_bundle@" value="${reportBundle}" />

		<chmod perm="ugo+x" file="${basedir}/../${scriptPath}.${extension}" />
	
		<!-- Update Report Server web.xml -->
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;ReportRepository&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${server.reporting.dir}%5"/>
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;jdbcUrl&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3jdbc:${db.type}://${db.hostname}:${db.port}/Aperi%5"/>
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;jdbcDriver&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${db.driver}%5" />
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;jdbcUser&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${db.username}%5" />
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;jdbcUser&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${db.username}%5" />					
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;BIRT_VIEWER_WORKING_FOLDER&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${server.reporting.dir}%5" />
		<searchAndReplace baseDir="${reportBundle}/web" fileMatchRegEx="web.xml" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;param-name&gt;BIRT_VIEWER_DOCUMENT_FOLDER&lt;/param-name&gt;)(\s*)(&lt;param-value&gt;)(.*)(&lt;/param-value&gt;)"
			replacementString="%1%2%3${server.reporting.dir}%5" />
		<createDevicePwd fileName="${reportBundle}/web/aperi-reports/WEB-INF/web.xml" token="@db_pwd@" password="${db.password}"/>
		
		<!--  Update Report Server Configuration -->
		<searchAndReplace baseDir="${basedir}/../reporting/configuration" fileMatchRegEx="login.config" dirExcludeRegEx=""
			stringMatchRegEx="(dbUrl=)(&quot;?)([^&quot;^\s]*)(&quot;?)"
			replacementString="%1%2jdbc:${db.type}://${db.hostname}:${db.port}/Aperi%4" />
		<searchAndReplace baseDir="${basedir}/../reporting/configuration" fileMatchRegEx="login.config" dirExcludeRegEx=""
			stringMatchRegEx="(dbUserName=)(&quot;?)([^&quot;^\s]*)(&quot;?)"
			replacementString="%1%2${db.username}%4" />
		<searchAndReplace baseDir="${basedir}/../reporting/configuration" fileMatchRegEx="login.config" dirExcludeRegEx=""
			stringMatchRegEx="(dbDriver=)(&quot;?)([^&quot;^\s]*)(&quot;?)"
			replacementString="%1%2${db.driver}%4" />
		<searchAndReplace baseDir="${basedir}/../reporting/configuration" fileMatchRegEx="login.config" dirExcludeRegEx=""
			stringMatchRegEx="(dbPassword=)(&quot;?)([^&quot;^\s]*)(&quot;?)"
			replacementString="%1%2${db.password.encrypted}%4" />
		
		<!-- Update reports -->
		<searchAndReplace baseDir="${server.reporting.dir}" fileMatchRegEx="(.*)(.rptdesign)" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;property name=&quot;odaDriverClass&quot;&gt;)(.*)(&lt;/property&gt;)"
			replacementString="%1${db.driver}%3" />
		<searchAndReplace baseDir="${server.reporting.dir}" fileMatchRegEx="(.*)(.rptdesign)" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;property name=&quot;odaURL&quot;&gt;)(.*)(&lt;/property&gt;)"
			replacementString="%1jdbc:${db.type}://${db.hostname}:${db.port}/Aperi%3" />
		<searchAndReplace baseDir="${server.reporting.dir}" fileMatchRegEx="(.*)(.rptdesign)" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;property name=&quot;odaUser&quot;&gt;)(.*)(&lt;/property&gt;)"
			replacementString="%1jdbc:${db.username}%3" />
		<searchAndReplace baseDir="${server.reporting.dir}" fileMatchRegEx="(.*)(.rptdesign)" dirExcludeRegEx=""
			stringMatchRegEx="(&lt;encrypted-property name=&quot;odaPassword&quot;&gt;)(.*)(&lt;/property&gt;)"
			replacementString="%1jdbc:${db.password.encoded}%3" />
		<chmod perm="ugo+x" file="../reporting/reporting" />
	</target>
	
</project>