<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="compileAll" name="scripts">
    <property environment="env"/>

    <!-- 
	CONFIGURATION PARAMETERS
	Modify here the parameters according to your own system
    -->
	<!-- PAMELA Software(s) root directory -->
    	<property name="pamSoftware"
		  value="${env.HOME}/pamela"/>
	<!-- CERN's ROOT base directory -->
	<property name="root"
		  value="/opt/root"/>
	<!-- LOG4CXX base directory -->
	<property name="log4cxx" 
		  value="/opt/log4cxx"/>
	<!-- ant-contrib base directory -->
	<property name="ant-contrib.lib"
		  value="/opt/ant-contrib/lib"/>
	<!-- source base directory -->
	<property name="scripts.src"
		  value="${basedir}"/>
	
	<!-- CONFIGURATION FILE. The properties in this file overwrite the previous properties -->
		<property file="../../KYoda.properties"/>
        <!-- CONFIGURATION FILE. The properties in this file overwrite the previous properties -->
	

    <!-- 
	     PARAMETRIZED PARAMETERS 
	     From here to the end there is NO NEED to modify anything
    -->
    <!-- Linux -->
    <property name="local" value="/usr/local"/>
    <property name="local.include" value="${local}/include"/>
    <property name="local.lib" value="${local}/lib"/>
    
    <!-- CERN's ROOT -->
    <property name="root.include" value="${root}/include"/>
    <property name="root.lib" value="${root}/lib"/>
    
    <!-- log4cxx -->    
    <property name="log4cxx.include" value="${log4cxx}/include"/>
    <property name="log4cxx.lib" value="${log4cxx}/lib"/>
    
    <!-- ant-contrib library -->
    <path id="classpath">
        <pathelement location="${ant-contrib.lib}/cpptasks.jar"/>
    </path>

   
    <!--
	  Load definition of C/C++ Tasks and Types
	  To allow the compilation and linking of C/C++ code  
    -->	
    <taskdef classpathref="classpath" loaderref="classpath.loaderRef" resource="cpptasks.tasks"/>
    <typedef classpathref="classpath" loaderref="classpath.loaderRef" resource="cpptasks.types"/>
     
    <!--
	  Load definition of Additional Tasks and Types
    --> 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
		<pathelement location="${ant-contrib.lib}/ant-contrib.jar"/>
        </classpath>
    </taskdef>

    <target name="initScripts" unless="${compilationFolder}">
	<tstamp>
		<format property="compilationFolder" pattern="hmsS" unit="millisecond"/>
    	</tstamp>    
	<!-- Compiler parameters -->
	<property name="compilationFolder.lib" 	   	value="${compilationFolder}/lib"/>
	<property name="compilationFolder.include"  	value="${compilationFolder}/include"/>
	
	<!-- scripts parameters -->
	<property name="scripts.compilationFolder" value="${env.HOME}/tmp/${compilationFolder}"/>
	<property name="scripts.obj"      	 	value="${scripts.compilationFolder}/obj"/>
	<property name="scripts.lib"      	 	value="${scripts.compilationFolder}/lib"/>
	<property name="scripts.bin"      		value="${scripts.compilationFolder}/bin"/>
	
	<!-- Deploy parameters -->
	<property name="deploy"   	value="${pamSoftware}"/>
	<property name="deploy.lib" 	value="${deploy}/lib"/>
	<property name="deploy.include" value="${deploy}/include"/>
	<property name="deploy.bin"	value="${deploy}/bin"/>
	<property name="deploy.data"	value="${deploy}/data"/>
    </target>

    <compiler id="scripts-gcc" name="gcc">
	    <includepath location="${local.include}"/>
	    <includepath location="${log4cxx.include}"/>
	    <includepath location="${root.include}"/>
	    <compilerarg value="-O"/>
	    <compilerarg value="-g"/>
    </compiler>
    
    <linker id="scripts-link">
	    <libset dir="${root.lib}"    libs="Cint, Core, Tree, Hist, Gpad, Graf, Matrix, HistPainter"/>
	    <libset dir="${log4cxx.lib}" libs="log4cxx"/>
	    <libset libs="stdc++"/>
    </linker>
    
    
    <!-- 
	 Clean the compilation folder
    -->
    <target description="Clean all build products." name="cleanScripts">
	    <delete dir="${scripts.compilationFolder}"/>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="compileArrToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/ArrDumpToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="ArrDumpToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="compileVarToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/VarDumpToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="VarDumpToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="compileLogToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/LogToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="LogToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="physEndRunToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/PhysEndRunToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="PhysEndRunToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="runHeaderToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/RunHeaderToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="RunHeaderToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="McmdToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/McmdToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="McmdToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include/utils"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda, utils"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="compileTabToXML">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/TabDumpToXML" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="TabDumpToXML.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="initScripts" description="Create initScripts" name="orbitalRate">
	    <mkdir dir="${scripts.bin}"/>
	    <mkdir dir="${scripts.obj}"/>
	    <cc objdir="${scripts.obj}" outfile="${scripts.bin}/OrbitalRate" outtype="executable">
		    <compiler extends="scripts-gcc">
			    <fileset dir="${scripts.src}" includes="OrbitalRate.cpp">
			    </fileset>
			    <includepath location="${scripts.src}"/>
			    <includepath location="${pamSoftware}/include/utils"/>
			    <includepath location="${pamSoftware}/include/utils/sgp4"/>
			    <includepath location="${pamSoftware}/include/yoda"/>
		    </compiler>
		    <linker extends="scripts-link">
			    <libset dir="${pamSoftware}/lib" libs="yoda, utils"/>
		    </linker>
	    </cc>
    </target>
    
    <target depends="compileArrToXML, compileVarToXML, compileTabToXML, compileLogToXML, physEndRunToXML, runHeaderToXML, McmdToXML, orbitalRate" description="Deploy the scripts" name="deployDataToXML">
	<mkdir dir="${deploy}"/>
	<mkdir dir="${deploy.lib}"/>
	<mkdir dir="${deploy.bin}"/>
	<mkdir dir="${deploy.data}"/>
	    <copy todir="${deploy.bin}">
		    <fileset dir="${scripts.bin}" includes="*" excludes="*.*"/>
	    </copy>
	    <copy todir="${deploy.data}">
		    <fileset dir="Data" includes="**/*.*"/>
	    </copy>
	<chmod dir="${deploy.bin}" perm="775" includes="**/*"/>
    </target>
</project>
