<?xml version="1.0" encoding="UTF-8"?>
<project name="AdecWatt" default="all" basedir="..">

  <target name="all" depends="init,compile,adecWattJar,updatedAdecWattJar" description="compile the source" >
  </target>

  <tstamp>
    <format property="TODAY_FR" pattern="yyyyMMdd" locale="fr,FR"/>
  </tstamp>

  <!-- ======================================== -->
  <description>
    Logiciel de plan de feu
  </description>

  <property name="webRef"         location="/home/DMZ/adecwattWeb/data/adecwatt/v3"/>
  <property name="work"           location="ws"/>
  <property name="data"           location="data"/>
  <property name="config"         location="data/config"/>
  <property name="log"            location="data/log"/>
  <property name="src"            location="src"/>
  <property name="java.src"       location="src/java"/>
  <property name="java.test"      location="src/javaTest"/>
  <property name="build"          location="build"/>
  <property name="build.api"      location="build/api"/>
  <property name="build.class"    location="build/class"/>
  <property name="build.jar"      location="soft"/>
  <property name="build.distrib"  location="distrib"/>
  <property name="lib"            location="soft"/>

  <property name="misc.class"     location="soft/Misc.jar"/>
  <property name="osm.class"      location="soft/JMapViewer.jar"/>
  <property name="pdf.class"      location="soft/pdfbox.jar"/>
  <path id="build.classpath">
    <pathelement location="${build.class}" />
  </path>

  <!-- ======================================== -->
  <target name="1stCmd" description="Must be done first of all (create link to launch ant in workspace)">
    <mkdir dir="${work}"/>
    <mkdir dir="${data}"/>
    <mkdir dir="${build}"/>
    <mkdir dir="${build.distrib}"/>
    <mkdir dir="${config}"/>
    <mkdir dir="${src}"/>
    <mkdir dir="${java.src}"/>

    <symlink failonerror="false" link="${work}/build.xml" resource="../ant/build.xml"/>
    <!--symlink failonerror="false" link="${work}/"          resource="${data}"/-->
  </target>

  <!-- ======================================== -->
  <target name="cleanConfig" description="clean XML file configuration" >
    <dirname property="pwd" file="${ant.file}" />

    <replaceregexp byline="true">
      <regexp pattern="(.*)${pwd}(.*)"/>
      <substitution expression="\1\.\2"/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;(UserId|Login|.*Name|Role.*).>.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;(Language|Variant|Country)&#34;&#62;.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;JConsole.*&#34;&#62;.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;.*ServerChecked&#34;&#62;.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;(Upload|LocalRemove|RemoteRemove|Clean).*Checked&#34;&#62;.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>

    <replaceregexp byline="true">
      <regexp pattern=".*entry key=&#34;Downloadhelp.*"/>
      <substitution expression=""/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>

    <replaceregexp byline="true">
      <regexp pattern="(.*entry key=&#34;Download.*Checked&#34;&#62;)false(.*)"/>
      <substitution expression="\1true\2"/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern="entry key=&#34;CheckPeriod[^&#60;]*"/>
      <substitution expression="entry key=&#34;CheckPeriod&#34;&#62;Day"/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
    <replaceregexp byline="true">
      <regexp pattern="(.*entry key=&#34;.*Glue&#34;&#62;)true(.*)"/>
      <substitution expression="\1true\2"/>
      <fileset dir="data/config">
	<include name="*.xml"/>
      </fileset>
    </replaceregexp>
  </target>

  <!-- ======================================== -->
  <target name="clean" description="clean up (remove classes and temporaries files)" >
    <delete dir="${build.class}"/>
    <delete dir="${build.api}"/>
    <delete verbose="true">
      <fileset defaultexcludes="no" dir="." includes="**/*~"/>
      <fileset defaultexcludes="no" dir="." includes="**/#*#"/>
      <fileset defaultexcludes="no" dir="." includes="**/.#*"/>
    </delete>
  </target>

  <!-- ======================================== -->
  <target name="wipe" depends="clean,cleanConfig" description="delete all product file (keep only sources)" >
    <delete dir="${log}"/>
    <delete file="${work}/build.xml"/>
    <symlink failonerror="false" action="delete" link="${work}/data"/>
    <delete dir="${build}"/> 
  </target>

  <!-- ======================================== -->
  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <mkdir dir="${build.api}"/>
    <mkdir dir="${build.class}"/>
    <mkdir dir="${log}"/>
    <mkdir dir="${build.jar}"/>
    <mkdir dir="${build.distrib}"/>
    <copy todir="${build.class}/data/config">
      <fileset dir="${data}/config">
	<include name="*.properties"/>
	<include name="A*.xml"/>
      </fileset>
    </copy>
  </target>

  <!-- ======================================== -->
  <target name="compile" depends="init" description="compile the source" >
    <javac destdir="${build.class}" includeantruntime="false" deprecation="true"  debug="true" verbose="false" encoding="iso-8859-1">
      <compilerarg value="-Xdiags:verbose" />
      <compilerarg value="-Xlint"/>
      <src path="${java.src}"/>
      <classpath>
	<pathelement location="${build.class}"/>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}:"/>
      </classpath>
    </javac>
  </target>

  <!-- ======================================== -->
  <target name="runTest" depends="compile" description="execute" >
    <java classname="adecWatt.control.TestAdecWatt" fork="true" dir="${work}">
      <classpath>
	<pathelement location="${build.class}"/>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}"/>
      </classpath>
    </java>
  </target>

  <!-- ======================================== -->
  <target name="run" depends="compile" description="execute" >
    <java classname="adecWatt.control.LaunchAdecWatt" fork="true" dir="${work}">
      <jvmarg value="-Djava.net.preferIPv4Stack=true"/>
      <jvmarg value="-Djava.net.useSystemProxies=true"/>
      <classpath>
	<pathelement location="${build.class}"/>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}"/>
      </classpath>
    </java>
  </target>

  <!-- ======================================== -->
  <target name="doc" depends="compile" description="make Application Programming Interface documentation" >
    <javadoc
	destdir="${build.api}"
	author="true"
	version="true"
	use="true"
	windowtitle="AdecWatt source files"
	overview="${java.src}/overview.html">
      <arg line="-private" />
      <fileset dir="${java.src}" defaultexcludes="yes">
	<!-- include name="com/dummy/test/**" / -->
	<exclude name="**/*.html"/>
	<exclude name="**/TODO"/>
	<exclude name="**/Makefile*"/>
	<exclude name="**/Manifest.*"/>
	<exclude name="**/*.[dch]*"/>
	<exclude name="**/*.txt"/>
	<exclude name="**/*Test.java"/>
      </fileset>
      <classpath>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}"/>
      </classpath>
      <doctitle><![CDATA[<h1>AdecWatt source files</h1>]]></doctitle>
      <bottom><![CDATA[<i>http://adecwatt.parlenet.org/</i>]]></bottom>
    </javadoc>
  </target>

  <!-- ======================================== -->
  <target name="adecWattJar" depends="compile" description="make AdecWatt jar" >
    <jar destfile="${build.jar}/AdecWatt.jar" >
      <manifest>
	<attribute name="Built-By" value="Author"/>
	<attribute name="Created-By" value="Author"/>
	<attribute name="Main-Class" value="adecWatt.control.LaunchAdecWatt"/>
	<attribute name="Class-Path" value="Misc.jar JMapViewer.jar pdfbox.jar"/>
      </manifest>
      <fileset dir="${build.class}" casesensitive="yes">
	<include name="**/adecWatt/**/*.class"/>
	<include name="misc/**/*.class"/>
      </fileset>
      <fileset dir="." >
	<include name="data/images/button/*.png" />
	<include name="data/images/adecWatt.png" />
	<include name="data/images/adecWatt-anim.gif" />
	<include name="data/images/adecWatt-anim-noel.gif" />
	<include name="data/images/2019-festival-adec56.png" />
	<include name="data/config/AdecWatt*.properties" />
	<include name="data/config/RemoteUpdate*.properties"/>
	<include name="data/config/AdecWatt.xml" />
	<include name="data/texts/*.html"/>
	<!-- <include name="data/texts/help-fr/**/*.png"/> -->
	<!-- <include name="data/texts/help-fr/**/*.jpg"/> -->
	<!-- <include name="data/texts/help-fr/**/*.html"/> -->
      </fileset>
    </jar>
    <copy todir="${webRef}/soft" preservelastmodified="true">
      <fileset dir="${build.jar}">
	<include name="AdecWatt.jar"/>
	<include name="Misc.jar"/>
	<include name="JMapViewer.jar"/>
	<include name="pdfbox.jar"/>
      </fileset>
    </copy>
  </target>

  <!-- ======================================== -->
  <target name="runAdecWattJar" depends="adecWattJar" description="execute jar" >
    <java classname="adecWatt.control.LaunchAdecWatt" fork="true" dir="${work}">
      <classpath>
	<pathelement location="${build.jar}/AdecWatt.jar"/>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}"/>
      </classpath>
    </java>
  </target>

  <!-- ======================================== -->
  <target name="updatedAdecWattJar" depends="compile" description="make AdecWatt jar" >
    <jar destfile="${build.jar}/UpdatedAdecWatt.jar" >
      <manifest>
	<attribute name="Built-By" value="Author"/>
	<attribute name="Created-By" value="Author"/>
	<attribute name="Main-Class" value="adecWatt.control.LaunchUpdatedAdecWatt"/>
      </manifest>
      <fileset dir="${build.class}" casesensitive="yes">
	<include name="adecWatt/control/LaunchUpdatedAdecWatt.class"/>
	<include name="misc/**/*.class"/>
      </fileset>
    </jar>
  </target>

  <!-- ======================================== -->
  <target name="runUpdatedAdecWattJar" depends="updatedAdecWattJar" description="execute jar" >
    <java classname="adecWatt.control.LaunchUpdatedAdecWatt" fork="true" dir="${work}">
      <classpath>
	<pathelement location="${build.jar}/UpdatedAdecWatt.jar"/>
	<pathelement path="${java.class.path}:${misc.class}:${osm.class}:${pdf.class}"/>
      </classpath>
    </java>
  </target>

  <!-- ======================================== -->
  <target name="srcZip" depends="cleanConfig,updatedAdecWattJar,adecWattJar" description="create distribution" >
    <zip destfile="${build.distrib}/${TODAY_FR}-SCR-AdecWatt.zip">
      <zipfileset dir="." prefix="adecWatt/">
	<include name="ant/build.xml"/>
	<include name="${build.jar}/UpdatedAdecWatt.jar"/>
	<include name="${build.jar}/AdecWatt.jar"/>
	<include name="data/config/*.properties"/>
	<include name="data/config/*.xml"/>
	<include name="data/images/button/*.png"/>
	<include name="data/images/adecWatt.png"/>
	<include name="data/images/adecWatt-anim.gif"/>
	<include name="data/texts/*.html"/>
	<include name="soft/Misc.jar"/>
	<include name="soft/JMapViewer.jar"/>
	<include name="soft/pdfbox.jar"/>
	<include name="soft/Bundle.jar"/>
	<include name="soft/UpdatedAdecWatt.jar"/>
	<include name="soft/AdecWatt.jar"/>
	<include name="src/java/**/*.java"/>
      </zipfileset>
      <zipfileset dir="." filemode="755" prefix="adecWatt/">
	<include name="ws/launch-bundle.sh"/>
	<include name="ws/launch-adecWatt.sh"/>
      </zipfileset>
    </zip>
  </target>

  <!-- ======================================== -->
  <target name="exeZip" depends="cleanConfig,updatedAdecWattJar,adecWattJar" description="create distribution">
    <zip destfile="${build.distrib}/${TODAY_FR}-EXE-AdecWatt.zip">
      <zipfileset dir="." prefix="adecWatt/">
	<include name="data/config/AdecWatt.xml"/>
      </zipfileset>
      <zipfileset dir="soft" prefix="adecWatt/soft">
	<include name="UpdatedAdecWatt.jar"/>
	<include name="AdecWatt.jar"/>
	<include name="Misc.jar"/>
	<include name="JMapViewer.jar"/>
      </zipfileset>
      <zipfileset dir="${work}" prefix="adecWatt/" filemode="755">
	<include name="launch-adecWatt.sh"/>
      </zipfileset>
      <zipfileset dir="data" prefix="adecWatt/data/server" filemode="755">
	<include name="furniture/000-0052.lpt"/>
      </zipfileset>
    </zip>
  </target>

  <!-- ======================================== -->
</project>
