<project name="hotel" default="dist" basedir=".">

   <description>Hotel Management</description>

   <property name="conversionHeap"  value="4G" />
   <property name="compileMemory"   value="4G" />
   <property name="ant.contrib.dir" value="${basedir}/build/ant.contrib/" />
   <property name="ant.contrib.url" value="https://proj.goldencode.com/downloads/ant_contrib/ant-contrib-1.0b5.zip" />
   <condition property="java_11_spi">
       <javaversion atleast="11"/>
   </condition>

   <!-- ANT-CONTRIB autoinstaller -->
   <available property="ant-contrib-exists" file="${ant.contrib.dir}/ant-contrib-1.0b5-SNAPSHOT.jar" />
   <target name="download-ant-contrib" unless="ant-contrib-exists">
      <mkdir dir="${ant.contrib.dir}" />
      <get src="${ant.contrib.url}"
           dest="${ant.contrib.dir}/ant-contrib-1.0b5.zip"
           username="true" />
      <unzip src="${ant.contrib.dir}/ant-contrib-1.0b5.zip"
             dest="${ant.contrib.dir}"
             overwrite="no" />
      <delete file="${ant.contrib.dir}/ant-contrib-1.0b5.zip" />
   </target>
   
   <target name="init-ant-contrib" depends="download-ant-contrib">
      <taskdef resource="net/sf/antcontrib/antcontrib.properties">
         <classpath>
            <fileset dir="${ant.contrib.dir}">
               <include name="*.jar"/>
            </fileset>
            <fileset dir="${ant.library.dir}">
               <include name="*.jar"/>
            </fileset>
         </classpath>
      </taskdef>
   </target>
      
   <property file="build.properties"/>
   
   <!-- a link named "p2j/" to the FWD project location must exist for this to work -->
   
   <!-- set global properties for this build -->
   
   <property name="build.home"           value="${basedir}/build" />
   <property name="cvt.home"             value="${basedir}/cvt" />
   <property name="cvt.rel"              value="cvt" />  <!-- relative path to cvt folder -->
   <property name="dist.home"            value="${basedir}/distribution" /> 
   <property name="data.rel"             value="data" /> <!-- relative path to data folder -->
   <property name="src.home"             value="${basedir}/src" />
   <property name="srcnew.home"          value="${basedir}/srcnew" />
   <property name="lib.home"             value="${basedir}/lib" />
   <property name="cfg.home"             value="${basedir}/cfg" />
   <property name="manifest.dir"         value="${basedir}/manifest" />

   <property name="fwd.home"             value="." />
   <property name="fwd.build.home"       value="${fwd.home}/p2j/build" />
   <property name="fwd.lib.home"         value="${fwd.home}/p2j/build/lib" />
   <property name="fwd.src.home"         value="${fwd.home}/p2j/src" />

   <property name="appname"              value="hotel" />
   <property name="zfile"                value="zfile_set.txt" />
   <property name="pkgroot"              value="com/goldencode/${appname}" />   
   <property name="app.4gl.src"          value="./abl/" />   
   <property name="ade.4gl.src"          value="./abl/ade" />
   <property name="possenet.4gl.src"     value="./abl/possenet*" />
   <property name="app.4gl.pattern"      value="*.[pPwW]" />
   <property name="destination.package"  value="${build.home}/classes/${pkgroot}"/>
   <property name="deploy.home"          value="${basedir}/deploy" />
   <property name="deploy.home.abs"      location="${deploy.home}" />
   <property name="rpt.home"             value="${basedir}/rpt" />
   <property name="cg.home"              value="${basedir}/callgraph" />
   
   <!-- enable database type(s) -->
   <property name="db.h2"                value="true" />
   <property name="db.postgresql"        value="false" />
   <property name="db.mariadb"           value="false" />
   <property name="db.sql.udfs"          value="true" />
   <property name="db.import_threads"    value="${db.import_threads}" />
   <property name="db.export_threads"    value="${db.export_threads}" />
   <property name="db.batch_size"        value="${db.batch_size}" />
   
   <!-- sql global properties  -->
   <property name="db.home"              value="${deploy.home}/db" />
   <property name="db.host"              value="localhost" />
   <property name="db.port"              value="5433" />
   <property name="sql.user"             value="fwd_user" />
   <property name="sql.admin"            value="fwd_admin" />
   <property name="sql.user.pass"        value="user" />
   <property name="sql.admin.pass"       value="admin" />

   <!-- webspeed embedded 4GL pattern signifying in a pre-processed source file (.w or .i) -->
   <property name="e4gl.pattern"         value="'^/\*E4GL\-[IW]\*/'" />
   <property name="e4gl-ps1.pattern"     value="'^/\\*E4GL-[IW]\\*/'" />
   <property name="e4gl-noclean"         value="-not -path '${ade.4gl.src}/*' -not -path '${possenet.4gl.src}/*'" />

   <condition property="isWindows">
      <os family="windows" />
   </condition>

   <condition property="isUnix">
      <os family="unix" />
   </condition>

   <condition property="escaped.quotes" value="&quot;&quot;&quot;" else="&quot;">
      <isset property="isWindows"  />
   </condition>

   
   <!--  ==================== Compilation Control Options ==================== -->

   <!--

     These properties control option settings on the Javac compiler when it
     is invoked using the <javac> task.

     compile.debug        Should compilation include the debug option?
     compile.deprecation  Should compilation include the deprecation option?
     compile.optimize     Should compilation include the optimize option?

   -->

   <property name="compile.debug"       value="true"/>
   <property name="compile.deprecation" value="false"/>
   <property name="compile.optimize"    value="true"/>
   
   <!-- path used when compiling Java classes -->
   <path id="compile.classpath">
      <fileset dir="${fwd.lib.home}"    includes="*.jar"/>
   </path>
   
   <!-- path used when running application related tasks -->
   <path id="app.classpath">
      <fileset dir="${fwd.lib.home}"    includes="*.jar"/>
      <fileset dir="${deploy.home}/lib" includes="*.jar"/>
   </path>
   
   <!-- path used when running database import tasks -->
   <path id="import.classpath">
      <fileset dir="${fwd.lib.home}"    includes="*.jar"/>
      <fileset dir="${deploy.home}/lib" includes="*.jar"/>
      <fileset dir="${build.home}/lib"  includes="*.jar"/>
   </path>
   
   <!-- path used when running database export tasks -->
   <path id="export.classpath">
      <fileset dir="${fwd.lib.home}"    includes="*.jar"/>
      <fileset dir="${deploy.home}/lib" includes="*.jar"/>
      <fileset dir="${build.home}/lib"  includes="*.jar"/>
   </path>

   <tstamp>
      <format property="DSTAMP" pattern="yyyyMMdd" />
   </tstamp>

   <tstamp>
      <format property="LOG_STAMP" pattern="yyyyMMdd_HHmmss" />
   </tstamp>

   <!-- create the time stamp -->
   <target name="init" depends="check.fwd.dir">
      <tstamp/>
   </target>

   <!-- generate call graph -->
   <target name="callgraph" depends="convert.front, convert.front.zset" description="Parse the project and create the call graph.">
      <record name="callgraph_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.uast.CallGraphGenerator"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx2G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value ="-d2"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="callgraph_${LOG_STAMP}.log" action="stop"/>
   </target>

   <!-- generate call graph, without parsing -->
   <target name="callgraph-no-front" depends="init" description="Parse the project and create the call graph.">
      <record name="callgraph_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.uast.CallGraphGenerator"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx8G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value ="-d2"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="callgraph_${LOG_STAMP}.log" action="stop"/>
   </target>

   <!-- update the call graph, without parsing -->
   <target name="callgraph-update" depends="init" description="Parse the project and create the call graph.">
      <record name="callgraph_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.uast.CallGraphGenerator"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx8G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value ="-d2"/>
         <arg value ="-u"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="callgraph_${LOG_STAMP}.log" action="stop"/>
   </target>

   <!-- generate all reports, old syntax, deprecated -->
   <target name="rpt-no-front"
           depends="init, init-ant-contrib"
           description="Generate code and schema reports from the already parsed project.">
      <record name="rpt_schema_${LOG_STAMP}.log" action="start"/>
      <var name="db.schema.files" value=""/>
      <for list="${db.names}" param="db.name">
         <sequential>
            <var name="db.schema.files" value="${db.schema.files} data/namespace/@{db.name}.dict"/>
         </sequential>
      </for>
      
      <java classname="com.goldencode.p2j.report.ReportDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx4G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <jvmarg value="-DappAdminPass=${appname}"/>
         <arg value="-Td1"/>
         <arg value="reports/schema"/>
         <arg value="${app.4gl.src}"/>
         <arg value="*.dict"/>
         <arg line="${db.schema.files}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="rpt_schema_${LOG_STAMP}.log" action="stop"/>
      <record name="rpt_code_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.report.ReportDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx4G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-Sd1"/>
         <arg value="reports/profile"/>
         <arg value="${app.4gl.src}"/>
         <arg value="*.ast"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="rpt_code_${LOG_STAMP}.log" action="stop"/>

      <echo message="Report generation finished."/>
   </target>

   <!-- generate all reports -->
   <target name="rpt-new"
           depends="init, init-ant-contrib"
           description="Generate code and schema reports from the already parsed project.">
      <record name="rpt_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.report.ReportDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}" >
         <arg value="-D2"/>
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx4G"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <jvmarg value="-DappAdminPass=${appname}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="rpt_${LOG_STAMP}.log" action="stop"/>
   </target>

   <target name="rpt"
           depends="convert.front, convert.front.zset, rpt-new"
           description="Parse the project and generate code and schema reports.">
   </target>
   
   <!-- generate everything that the report server can support -->
   <target name="report_server" depends="clean, rpt, callgraph" description="Parse the project, generate schema/code reports and create the call graph." />
   
   <target name="check-zfile-set">
      <available file="${zfile}" property="zfile-set"/>
   </target>

   <condition property="no-list-mode">
      <available file="${zfile}"/>
   </condition>

   <!-- convert 4GL code from ${app.4gl.src} into ${src}, using ${app.4gl.pattern} pattern -->
   <target name="convert"
           depends="init, check-zfile-set"
           description="Convert the 4GL source code to Java source code, using a pattern as input."
           unless="no-list-mode">
      <record name="cvt_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-SId2"/>
         <arg value="f2+m0+cb"/>
         <arg value="${app.4gl.src}"/>
         <arg value="${app.4gl.pattern}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_${LOG_STAMP}.log" action="stop"/>
   </target>
	
   <!-- convert 4GL code from ${app.4gl.src} into ${src}, using a fileset -->
   <target name="convert.zset"
           depends="init, check-zfile-set"
           description="Convert the 4GL source code to Java source code, using the z-file set file as input."
           if="zfile-set">
      <record name="cvt_${LOG_STAMP}.log" action="start"/>
      <echo message="fileset in use: ${zfile}"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-ZId2"/>
         <arg value="f2+m0+cb"/>
         <arg value="${zfile}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_${LOG_STAMP}.log" action="stop"/>
   </target>
   
   <target name="convert.front"
           depends="init, check-zfile-set"
           description="Execute the parse phase of the conversion process, using a pattern as input."
           unless="no-list-mode">
      <record name="cvt_f2_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-ISd2"/>
         <arg value="f2"/>
         <arg value="${app.4gl.src}"/>
         <arg value="${app.4gl.pattern}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_f2_${LOG_STAMP}.log" action="stop"/>
   </target>

   <target name="convert.front.zset"
           depends="init, check-zfile-set"
           description="Execute the parse phase of the conversion process, using the z-file set file as input."
           if="zfile-set">
      <record name="cvt_f2_${LOG_STAMP}.log" action="start"/>
      <echo message="fileset in use: ${zfile}"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-IZd2"/>
         <arg value="f2"/>
         <arg value="${zfile}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_f2_${LOG_STAMP}.log" action="stop"/>
   </target>
   
   <target name="convert.middle" depends="init, check-zfile-set"
           description="Execute the parse and schema conversion phases of the conversion process, using a pattern as input."
           unless="no-list-mode">
      <record name="cvt_f2m0_${LOG_STAMP}.log" action="start"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-ISd2"/>
         <arg value="f2+m0"/>
         <arg value="${app.4gl.src}"/>
         <arg value="${app.4gl.pattern}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_f2m0_${LOG_STAMP}.log" action="stop"/>
   </target>
   
   <target name="convert.middle.zset"
           depends="init, check-zfile-set"
           description="Execute the parse and schema conversion phases of the conversion process, using the ${zfile} set file as input."
           if="zfile-set">
      <record name="cvt_f2m0_${LOG_STAMP}.log" action="start"/>
      <echo message="fileset in use: ${zfile}"/>
      <java classname="com.goldencode.p2j.convert.ConversionDriver"
            fork="true"
            failonerror="true"
            dir="${basedir}">
         <jvmarg value="-server"/>
         <jvmarg value="-Xmx${conversionHeap}"/>
         <jvmarg value="-XX:-OmitStackTraceInFastThrow"/>
         <jvmarg value="-DP2J_HOME=${fwd.home}"/>
         <arg value="-IZd2"/>
         <arg value="f2+m0"/>
         <arg value="${zfile}"/>
         <classpath refid="compile.classpath"/>
      </java>
      <record name="cvt_f2m0_${LOG_STAMP}.log" action="stop"/>
   </target>

   <!-- make sure the FWD project code is compiled -->
   <target name="fwd-build" depends="check.fwd.dir" description="Compile the FWD project." >
      <!-- compile the java code from ${src} into ${build} 
      <ant antfile="build.xml" target="all" dir="p2j" inheritall="false" inheritrefs="false"/>
      -->
   </target>
  
   <!-- compile -->
   <target name="compile"
           depends="init, check.fwd.dir, clean.build, fwd-build, prepare"
           description="Compiles the converted application Java source code.  Does NOT convert, create jars or deploy." >
           
      <record name="build_${LOG_STAMP}.log" action="start"/>
      <!-- Compile DMO and UI classes only -->
      <javac srcdir="${src.home}"
             destdir="${build.home}/classes"
             includes="${pkgroot}/dmo/**
                       ${pkgroot}/ui/**"
             debug="${compile.debug}"
             deprecation="${compile.deprecation}"
             memoryMaximumSize="${compileMemory}"
             fork="true"
             optimize="${compile.optimize}"
             includeantruntime="false" >   
         <classpath refid="compile.classpath"/>
         <compilerarg value="-Xmaxerrs"/>
         <compilerarg value="5000"/>
      </javac>

      <!-- Compile all other classes -->
      <javac srcdir="${src.home}"
             destdir="${build.home}/classes"
             debug="${compile.debug}"
             deprecation="${compile.deprecation}"
             memoryMaximumSize="${compileMemory}"
             fork="true"
             optimize="${compile.optimize}"
             includeantruntime="false" >   
         <classpath refid="compile.classpath"/>
         <compilerarg value="-Xmaxerrs"/>
         <compilerarg value="5000"/>
      </javac>

      <record name="build_${LOG_STAMP}.log" action="stop"/>
      <echo message="Compiling Java Sources Done."/>
   </target>
   
   <target name="prepare.embedded">
      <!-- copy custom embedded sources and data to the main app -->
      <copy todir="src/${pkgroot}/">
         <fileset dir="embedded/src/${pkgroot}/" />
      </copy>

      <!-- copy embedded resources -->
      <copy todir="${build.home}/classes">
         <fileset dir="${src.home}" includes="${pkgroot}/embedded/**" />
      </copy>

   </target>

   <target name="prepare" depends="prepare.embedded">

      <!-- create build directories as needed -->
      
      <mkdir dir="${build.home}" />
      <mkdir dir="${build.home}/lib" />
      <mkdir dir="${build.home}/classes" />
      
      <!-- create distribution directory as needed -->
      
      <mkdir dir="${dist.home}" />
      <mkdir dir="${dist.home}/docs/api" />
      
      <!-- copy external jars -->
      
      <mkdir dir="${lib.home}" />
      <copy todir="${build.home}/lib">
         <fileset dir="${lib.home}" />
      </copy>

      <!-- copy base web res dir -->
      <copy todir="${build.home}/classes/${pkgroot}">
         <fileset dir="${fwd.home}">
            <include name="webres/**"/>
         </fileset>
      </copy>

      <!-- create src dir -->
      <mkdir dir="${src.home}" />
      
      <!-- copy application graphics -->
      <copy todir="${build.home}/classes/${pkgroot}">
         <fileset dir="${app.4gl.src}" includes="**/*.png **/*.jpg **/*.bmp **/*.ico **/*.gif" casesensitive="false" />
      </copy>

      <!-- copy jrxml templates -->
      <copy todir="${build.home}/classes/${pkgroot}">
         <fileset dir="${app.4gl.src}" includes="**/*.jrxml" />
      </copy>

      <!-- copy all XML and WSDL documents -->
      <copy todir="${build.home}/classes">
         <fileset dir="${src.home}" includes="**/*.xml **/*.wsdl" />
      </copy>
      
      <!-- Copy ehcache production config file into classpath -->
      <copy file="${cfg.home}/ehcache.xml"
            tofile="${build.home}/classes/ehcache.xml" />   

      <!-- Copy SQL scripts from src to equivalent location in build -->
      <copy todir="${build.home}/classes">
         <fileset dir="${src.home}" includes="**/*.sql" />
      </copy>

      <!-- Copy name_map.cache file to root of build -->
      <copy file="${basedir}/name_map.cache" failonerror="false" todir="${build.home}/classes" />

      <!-- Copy p2j.cfg.xml* from src to equivalent location in build -->
      <copy todir="${build.home}/classes/cfg">
         <fileset dir="${cfg.home}" includes="p2j.cfg.xml* registry.xml" />
      </copy>

      <!-- Copy data/ from src to equivalent location in build -->
      <copy todir="${build.home}/classes/${data.rel}">
         <fileset dir="${cvt.home}/${data.rel}" includes="*.dict" />
         <fileset dir="${cvt.home}/${data.rel}" includes="*.p2o" />
      </copy>

      <!-- copy the DDL so we can build the DB from the application jar -->
      <copy todir="${build.home}/classes/ddl">
         <fileset dir="ddl" />
      </copy>
   </target>
   
   <!-- jar -->  
   <target name="jar"
           depends="check.fwd.dir,compile"
           description="Compiles and Jars the converted application Java source code.  Does NOT convert or deploy." >
      <jar jarfile="${build.home}/lib/${appname}.jar"
           basedir="${build.home}/classes"
           excludes="**/*.template"
           includes="**/${pkgroot}/**
                     dir_schema_ext.xml
                     ehcache.xml
                     *.jasper
                     **.xml
                     cfg/**
                     ${data.rel}/**
                     **/*.wrx
                     **/*.png
                     **/*.jpg
                     **/*.gif
                     **/*.bmp
                     **/*.ico
                     app/**
                     ddl/**"
           manifest="${manifest.dir}/${appname}.mf"
           zip64Mode="as-needed"
           index="true"   />
   </target>      

   <!-- zip-hints -->
   <target name="zip-hints" description="Creates a zip archive with all the .hints files in the abl/ and data/ folders." >
      <delete file="${appname}_hints.zip"/>
      <zip destfile="${appname}_hints.zip"
           basedir="."
           includes="abl/**/*.hints" />
      <zip destfile="${appname}_hints.zip"
           basedir="."
           includes="data/**/*.hints" 
           update="true"/>
   </target>

   <!-- Archiving targets to build zip files -->
   <target name="archive" depends="archive.prepare, archive.deploy, archive.debug, archive.incremental, archive.dumps" 
           description="Perform all the archive tasks"/>

   <!-- Prepare the properties that will be used. Make sure the setup_version.sh runs first. -->
   <target name="archive.prepare">
      <!-- Setup the version.properties -->
      <exec executable="./setup_version.sh" dir="." failonerror="true" osfamily="unix">
         <arg value="-no"/>
      </exec>

      <exec executable="pwsh" dir="." failonerror="true" osfamily="windows">
         <arg value="-ExecutionPolicy"/>
         <arg value="Bypass"/>
         <arg value="-File"/>
         <arg value="setup_version.ps1"/>
         <arg value="-n"/>
         <arg value="-o"/>
      </exec>

      <property file="version.properties"/>

      <echo>Using this version info:</echo>
      <echo>  App type: ${app_type}</echo>
      <echo>  App project revision: ${app_rev}</echo>
      <echo>  Conversion date: ${app_date}</echo>
      <echo>  FWD conversion version: ${fwd_branch}-${fwd_rev}</echo>
   </target>

   <!-- gather the artifacts for deployment -->
   <target name="archive.deploy" depends="archive.prepare"
           description="Create an archive with the application jars and other files required for deployment on other systems.">
      <mkdir dir="${dist.home}/"/>
      <property name="archive.dist_destfile"
                value="${dist.home}/${projname}_deploy_${app_rev}_${fwd_branch}-${fwd_rev}_${app_date}.zip" />
      <zip destfile="${archive.dist_destfile}" >
         <zipfileset dir="${basedir}" 
                     excludes="**/*.sh **/*.zip **/*.so **/*.log"
                     includes="build/lib/**
                               ddl/**
                               ${data.rel}/*.df
                               ${cvt.rel}/${data.rel}/*.dict
                               ${cvt.rel}/${data.rel}/*.p2o
                               cfg/*.xml
                               deploy/client/**
                               deploy/server/**
                               *.xml
                               *.properties" />
         <zipfileset dir="${basedir}"
                     filemode="755" 
                     includes="deploy/client/*.sh
                               deploy/server/*.sh
                               deploy/*.sh
                               install_spawner.sh" />
      </zip>
      <chmod file="${archive.dist_destfile}" perm="755"/>
   </target>

   <!-- gather some helpful artifacts for debugging -->
   <target name="archive.debug" depends="archive.prepare"
           description="Create an archive with the artifacts helpful for debugging.">
      <mkdir dir="${dist.home}/"/>
      <property name="archive.debug_destfile"
                value="${dist.home}/${projname}_debug_${app_rev}_${fwd_branch}-${fwd_rev}_${app_date}.zip" />
      <zip destfile="${archive.debug_destfile}"
           basedir="${basedir}" 
           excludes="cfg/registry.xml"
           includes="src/com/**
                     abl/WebHelp/**
                     abl/**/*.ico
                     abl/**/*.wrx
                     abl/**/*.cache
                     abl/**/*.pphints
                     *.log" />
      <chmod file="${archive.debug_destfile}" perm="755"/>
   </target>

   <!-- gather artifacts for incremental builds -->
   <target name="archive.incremental" depends="archive.prepare"
           description="Create an archive with the artifacts necessary for an incremental build.">
      <mkdir dir="${dist.home}/"/>
      <property name="archive.incremental_destfile"
                value="${dist.home}/${projname}_incremental_${app_rev}_${fwd_branch}-${fwd_rev}_${app_date}.zip" />
      <zip destfile="${archive.incremental_destfile}"
           basedir="${basedir}" 
           excludes="cfg/registry.xml"
           includes="${cvt.rel}/cvtdb/
                     src/**
                     ddl/**
                     *.txt
                     abl/wsa/**
                     abl/**/*.p2o
                     abl/**/*.dict
                     abl/**/*.ast
                     abl/**/*.jast
                     abl/**/*.schema" />
      <chmod file="${archive.incremental_destfile}" perm="755"/>
   </target>

   <target name="archive.dumps" depends="archive.prepare"
           description="Create an archive with the dumps for testing." >
      <echo message="Creating dump archives"/> 
      <mkdir dir="${dist.home}/"/>
      <property name="archive.dumps_destfile"
                value="${dist.home}/${projname}_dumps_${app_rev}_${fwd_branch}-${fwd_rev}_${app_date}.zip" />
      <zip destfile="${archive.dumps_destfile}"
           basedir="${basedir}" 
           includes="${data.rel}/dump/**/*" />
      <chmod file="${archive.dumps_destfile}" perm="755"/>
   </target>

   <!-- deploy the application jars and database, not FWD -->
   <target name="deployapp"
           depends="clean, convert, convert.zset, jar, deployapp.prepare"
           description="Convert, compile the converted Java sources, create jars, deploys the jars (not FWD).  Does NOT create/import the database." >
   </target>
   
   <!-- deploy the application jars and database, not FWD -->
   <target name="deployapp.all"
           depends="deployapp, import.db"
           description="Convert, compile the converted Java sources, create jars, deploys the jars (not FWD) and creates/imports the database." >
   </target>
   
   <!-- deploy the application jars, but NOT database -->
   <target name="deploy"
           depends="clean, convert, convert.zset, jar, deploy.prepare"
           description="Convert, compile the converted Java sources, create jars, deploys the jars.  Does NOT create/import the database." >
   </target>
   
   <!-- deploy the application jars AND database -->
   <target name="deploy.all"
           depends="deploy, import.db "
           description="Convert, compile the converted Java sources, create jars, deploys the jars and creates/imports the database." >
   </target>
   
   <target name="rpt.dist" depends="archive.prepare"
           description="Create an archive with the files required for deployment or reports on other systems.">
      <mkdir dir="${dist.home}/"/>
      <property name="archive.reports_destfile"
                value="${dist.home}/${projname}_reports_${app_rev}_${fwd_branch}-${fwd_rev}_${app_date}.zip" />
      <zip destfile="${archive.reports_destfile}" >
         <zipfileset dir="${basedir}" 
                     excludes="**/*.zip **/*.so"
                     includes="deploy/server/report.sh
                               abl/**
                               cfg/*
                               rpt/*
                               rptdb/*
                               callgraph/*"
                     filemode="755" />
      </zip>
   </target>

   <target name="deploy.dist" description="Create an archive with the application jars and other files required for deployment on other systems.">
      <mkdir dir="${dist.home}/"/>
      <zip destfile="${dist.home}/${appname}_deploy_${DSTAMP}.zip" >
         <zipfileset dir="${basedir}" 
                     excludes="**/*.sh **/*.zip **/*.so"
                     includes="build/lib/**
                               ddl/**
                               data/*.df
                               data/dump/**
                               data/**/*.dict
                               data/**/*.p2o
                               cfg/*.xml
                               deploy/lib/**
                               deploy/client/**
                               deploy/server/**
                               *.xml
                               *.properties" />
         <zipfileset dir="${basedir}" 
                     filemode="755" 
                     includes="docker/*.sh
                               deploy/*.sh
                               deploy/client/*.sh
                               deploy/server/*.sh" />
      </zip>
      <chmod file="${archive.dist_destfile}" perm="755"/>
   </target>

   <target name="deploy.prepare" depends="prepare.embedded,deployapp.prepare"
           description="Deploys the FWD *and* generated jars by copying them to the location expected by the runtime scripts." >
     
      <!-- Copy external jars -->
      <copy todir="${deploy.home}/lib">
        <!--  exclude the AspectJ tools -->
        <fileset dir="${fwd.lib.home}" excludes="aspectjtools.jar,fwdspi.jar"/>
      </copy>
   </target>

   <!-- deploy the application jars, NOT FWD -->
   <target name="deployapp.prepare" depends="deployapp.prepare.java8,deployapp.prepare.java11"
           description="Deploys the generated jars for the application (not FWD) by copying them to the location expected by the runtime scripts." >
           
      <!-- copy the required jars to the distribution directory -->
      <copy file="${build.home}/lib/${appname}.jar" todir="${deploy.home}/lib"/>      
   </target>
   <target name="deployapp.prepare.java8" unless="java_11_spi"
           description="Deploys the FWD *and* generated jars by copying them to the location expected by the runtime scripts, Java 8 version." >
           
      <!-- create the deploy lib folder -->
      <delete dir="${deploy.home}/lib" />
      <mkdir dir="${deploy.home}/lib"/>

      <!-- create and populate the extension lib folder -->
      <mkdir dir="${deploy.home}/lib/spi"/>
      <copy file="${fwd.lib.home}/fwdspi.jar" todir="${deploy.home}/lib/spi/"/>
   </target>
   <target name="deployapp.prepare.java11" if="java_11_spi"
           description="Deploys the FWD *and* generated jars by copying them to the location expected by the runtime scripts for Java 11+" >

      <!-- create the deploy lib folder -->
      <delete dir="${deploy.home}/lib" />
      <mkdir dir="${deploy.home}/lib"/>
      <copy file="${fwd.lib.home}/fwdspi.jar" todir="${deploy.home}/lib/"/>
   </target>

   <target name="distapp" depends="deployapp.all" description="Does a deployapp.all and then tars the result." >
      <mkdir dir="${dist.home}"/>
      <tar destfile="${dist.home}/${appname}-${DSTAMP}.tar" basedir="${deploy.home}" />
   </target>
   
   <target name="dist" depends="deploy.all" description="Does a deploy.all and then tars the result." >
      <mkdir dir="${dist.home}"/>
      <tar destfile="${dist.home}/${appname}-${DSTAMP}.tar" basedir="${deploy.home}" />
   </target>
   
   <target name="dist.abl" depends="init" description="Create a zip archive with the files needed to run the sample on OpenEdge" >
      <mkdir dir="${dist.home}/"/>
      <zip destfile="${dist.home}/${appname}-abl-${DSTAMP}.zip" basedir="${basedir}" 
           includes="abldb/
                     *.bat
                     *.ini
                     *.pf
                     start_*_abl.sh
                     start_*_abl.cmd
                     abl/**/*
                     data/*.df
                     data/dump/**/*.d"/>
   </target>
   
   <target name="check.fwd.dir" depends="dir.check" unless="dir.exists">
      <echo>"${fwd.home}/p2j directory is missing."</echo>
      <fail message="Build failed: Please add a link to the FWD directory location."/>
   </target>

   <target name="clean" depends="clean.convert, clean.build, clean.dist" description="Remove all clean.build, clean.convert, clean.dist files." />

   <target name="clean.build" description="Remove all built files from the conversion process." >
      <delete includeemptydirs="true" failonerror="false">
         <fileset dir="${build.home}">
            <include name="**/*"/>
            <exclude name="ant.contrib/**"/>
         </fileset>
      </delete>

      <delete dir="${deploy.home}/spawner"/>
      <delete dir="${deploy.home}/lib" />
      <delete>
         <fileset dir="${deploy.home}/client/" includes="**/*.log" />
      </delete>
      <delete>
         <fileset dir="${deploy.home}/server/" includes="**/*.log" />
      </delete>
      <delete dir="${dist.home}"/>
   </target>

   <!-- Delete embedded 4GL files created by pre-processing of HTML files -->
   <target name="clean-e4gl" description="Clean files with the /*E4GL-W*/ or /*E4GL-I*/ markers (exclude ADE and possenet)">
      <exec executable="sh" failonerror="true" osfamily="unix">
         <arg value="-c" />
         <arg value="find ${app.4gl.src} -type f -name '*.w' ${e4gl-noclean} -exec grep -l ${e4gl.pattern} {} \; -exec rm -f {} +" />
      </exec>

      <exec executable="powershell" failonerror="true" osfamily="windows">
         <arg value="-Command"/>
         <arg value="Get-ChildItem -Path '${app.4gl.src}' -Filter '*.w' -Recurse | Select-String -Pattern ${e4gl-ps1.pattern} | ForEach-Object { Remove-Item $_.Path -Force }"/>
      </exec>
   </target>
   
   <target name="clean.convert" depends="clean-e4gl" description="Remove all generated files from the conversion process." >
      <delete includeemptydirs="true" failonerror="false">
         <fileset dir="${basedir}/src"
            includes="**/*"
            excludes="text-metrics.xml
                      font-metrics-ext.xml" /> 
      </delete>
      <!-- only delete the src dir if it is empty -->
      <delete includeemptydirs="true" failonerror="false">
        <fileset dir="${src.home}" excludes="**/*" />
      </delete>
      <delete dir="${cvt.home}"/>
      <delete dir="${basedir}/ddl"/>
      <delete dir="${basedir}/local"/>
      <delete dir="${basedir}/com"/>
      <delete dir="${basedir}/lib"/>
      <delete dir="${basedir}/rptdb"/>
      <delete>
         <fileset dir="${app.4gl.src}" 
                  includes="**/*.pphints 
                            **/*.cache
                            **/*.lexer 
                            **/*.parser 
                            **/*.ast
                            **/*.ast.original
                            **/*.jast 
                            **/*.dict
                            **/*.p2o
                            **/*.schema
                            **/*.log" />
      </delete>
      <delete failonerror="false">
         <fileset dir="${basedir}/data/namespace" includes="*.dict *.p2o *.schema" />
      </delete>
      <delete failonerror="false">
         <fileset dir="${basedir}" includes="schema_names*.rpt *.log" />
      </delete>
      <delete file="ui_strings.txt"/>
      <delete file="orphan-frames.dat"/>
      <delete file="schema-triggers.xml"/>
      <delete file="internal_procedures.xml"/>
      <delete file="name_map.cache"/>
      <delete file="file_sys.xml"/>
      <delete file="name_map.xml"/>
      <delete file="function_return_types.xml"/>
      <delete file="duplicate_function_return_types.xml"/>
      <delete file="cfg/registry.xml"/>
      <delete file="unindexed_queries.txt"/>
      <delete file="unused_database_indices.txt"/>
      <delete file="indeterminate_sort_queries.csv" />
      <delete file="unreferenced_database_fields.txt" />
      <delete file="unused_database_indices.txt" />
     </target>
   
   <!-- delete the ${build} and ${dist} directory trees -->
   <target name="clean.dist" description="Remove the distribution (docs and tar files) directories." >
      <delete dir="${dist.home}"/>
   </target>   
   
   <target name="dir.check">
      <condition property="dir.exists">
         <available file="${fwd.home}/p2j" type="dir"/>
      </condition>
   </target>

   <!-- delete the ${rpt.home} directory trees -->
   <target name="clean.rpt" description="Remove generated reports directory.">
      <delete dir="${rpt.home}" /> 
   </target>
   
   <!-- delete the ${cg.home} directory trees -->
   <target name="clean.cg" description="Remove generated call graph.">
      <delete dir="${cg.home}" /> 
      <delete file="callgraph.graphml" /> 
   </target>
   
   <!-- Remove all traces of conversion, import or execution of the result (generated files, logs, imported dbs). -->
   <target name="clean.all"
           depends="clean, clean.build, clean.db, clean.rpt, clean.cg"
           description="Remove all traces of conversion, reporting, callgraph analysis, import or execution of the result (generated files, logs, imported dbs)." >
   </target>
   
   <target name="clean.db" description="Drop all databases."
           depends="clean.db.h2, clean.db.pg, clean.db.mariadb" />

   <target name="create.db" depends="create.db.h2, create.db.pg, create.db.mariadb"
           description="Create empty database instance for all enabled database types." />
   
   <target name="import.db" depends="import.db.h2, import.db.pg, import.db.mariadb"
           description="Create empty database instance and import data for all enabled database types." />

   <target name="export.db" depends="export.db.h2, export.db.pg, export.db.mariadb"
           description="Export data for all enabled database types into .d files." />

   <target name="db.task" depends="init-ant-contrib">
      <if>
         <available file="${fwd.lib.home}/spi/fwdspi.jar" />
         <then>
            <property name="db.spi.lib" value="${fwd.lib.home}/spi" />
         </then>
         <else>
            <property name="db.spi.lib" value="${deploy.home}/lib/spi" />
         </else>
      </if>
      <for list="${db.names}" param="db.name">
         <sequential>
            <var name="db.sql.name" value="@{db.name}" />
            <if>
               <isset property="db.sql.@{db.name}"  />
               <then>
                  <var name="db.sql.name" value="${db.sql.@{db.name}}" />
               </then>
            </if>
            <ant antfile="build_db.xml" inheritRefs="true" target="${db.task}">
               <property name="db.name" value="@{db.name}"/>
               <property name="db.sql.name" value="${db.sql.name}"/>
            </ant>
         </sequential>
      </for>
   </target>

   <!-- drop the H2 database by deleting the ${db.home} directory -->
   <target name="clean.db.h2" description="Remove generated database directory." if="${db.h2}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="clean.db.h2"/>
      </antcall>
   </target>

   <target name="create.db.h2" depends="check.fwd.dir, init, clean.db.h2"
           description="Creates an empty H2 database instance using the DDL generated during conversion."
           if="${db.h2}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="create.db.h2"/>
      </antcall>
   </target>
   
   <target name="import.db.h2" depends="create.db.h2"
           description="Import data (.d files) into database."
           if="${db.h2}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="import.db.h2"/>
      </antcall>
   </target>

   <target name="export.db.h2" depends="check.fwd.dir, init, setup-package-name"
           description="Export data from H2 database into (.d files) ."
           if="${db.h2}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="export.db.h2"/>
      </antcall>
   </target>

   <!-- drop the PostgreSQL database -->
   <target name="clean.db.pg" description="Drop PostgreSQL database." if="${db.postgresql}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="clean.db.pg"/>
      </antcall>
   </target>
   
   <target name="create.db.pg" depends="check.fwd.dir, init, clean.db.pg"
           description="Create an empty PostgreSQL database instance using the DDL generated during conversion."
           if="${db.postgresql}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="create.db.pg"/>
      </antcall>
   </target>
   
   <target name="import.db.pg" depends="create.db.pg"
           description="Import data (.d files) into PostgreSQL database."
           if="${db.postgresql}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="import.db.pg"/>
      </antcall>
   </target>

   <target name="export.db.pg" depends="check.fwd.dir, init, setup-package-name"
           description="Export data from PostgreSQL database into (.d files) ."
           if="${db.postgresql}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="export.db.pg"/>
      </antcall>
   </target>
   
   <!-- drop the MariaDb database -->
   <target name="clean.db.mariadb" description="Drop MariaDb database." if="${db.mariadb}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="clean.db.mariadb"/>
      </antcall>
   </target>
   
   <target name="create.db.mariadb" depends="check.fwd.dir, init, clean.db.mariadb"
           description="Create an empty MariaDb database instance using the DDL generated during conversion."
           if="${db.mariadb}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="create.db.mariadb"/>
      </antcall>
   </target>
   
   <target name="import.db.mariadb" depends="create.db.mariadb"
           description="Import data (.d files) into MariaDb database."
           if="${db.mariadb}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="import.db.mariadb"/>
      </antcall>
   </target>

   <target name="export.db.mariadb" depends="check.fwd.dir, init, setup-package-name"
           description="Export data from MariaDb database into (.d files) ."
           if="${db.mariadb}">
      <antcall target="db.task" inheritRefs="true">
         <param name="db.task" value="export.db.mariadb"/>
      </antcall>
   </target>

   <target name="setup-package-name" depends="init-ant-contrib">
      <!-- Convert slashes to dots -->
      <propertyregex 
         property="pkgroot.dot" 
         input="${pkgroot}" 
         regexp="/" 
         replace="." />
   </target>
</project> 
