<!--
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 -->
<project name="Faban Benchmark" default="deploy.jar" basedir=".">

    <property file="build.properties"/>
    <property name="src.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="lib.dir" value="lib"/>
    <property name="buildlib.dir" value="${build.dir}/lib"/>
    <property name="faban.libs" value="${faban.home}/lib"/>
    <property name="faban.classes" value="${faban.home}/master/webapps/faban/WEB-INF/classes"/>
  
   
    <path id="taskclasspath">
        <fileset dir="${faban.home}/ant/lib" includes="*.jar"/>
        <fileset dir="${faban.libs}" includes="*.jar"/>
    </path>

    <path id="classpath" location="${faban.libs}" >
        <pathelement location="${classes.dir}"/>
        <fileset dir="${lib.dir}" includes="*.jar"/>
        <fileset dir="${faban.libs}" includes="*.jar"/>
        <pathelement location="${faban.classes}"/>
    </path>
    
    <taskdef name="deploy" classname="com.sun.faban.harness.util.DeployTask">
        <classpath refid="taskclasspath"/>
    </taskdef>

    <target name="init">
        <mkdir dir="${classes.dir}"/>
    </target>

    <target name="compile" depends="init" description="Compiling all source files">
        <javac srcdir="${src.dir}"
            deprecation="on" target="${compiler.target.version}"
            destdir="${classes.dir}" debug="on">
            <include name="**/*.java" />
            <classpath refid="classpath"/>
        </javac>
    </target>

    <target name="clean" description="cleanup module">
      <delete>
        <fileset dir="${build.dir}" includes="**/*"/>
      </delete>
    </target>


    <target name="bench.jar" depends="compile" description="Assembles library jar with benchmark classes">
        <mkdir dir="${buildlib.dir}"/>
        <jar jarfile="${buildlib.dir}/${bench.shortname}.jar">
            <fileset dir="${classes.dir}" includes="**/*.class"/>
        </jar>
    </target>

    <target name="deploy.jar" depends="bench.jar"
        description="Assembles deployment jar image for Faban harness">
    <fixcrlf srcdir="bin"/>
        <jar jarfile="${build.dir}/${bench.shortname}.jar">
            <metainf dir="deploy" includes="*"/>
            <fileset dir="." includes="bin/**/*, lib/**/*, resources/**/*"/>
            <fileset dir="${build.dir}" includes="lib/**/*"/>
        </jar>
    </target>

    <target name="deploy" depends="deploy.jar"
        description="Deploys benchmark on the Faban harness">
        <deploy url="${faban.url}"
            jar="${build.dir}/${bench.shortname}.jar"
            user="${deploy.user}" password="${deploy.password}"
            clearConfig="${deploy.clearconfig}"/>
    </target>

    <target name="run" depends="bench.jar" 
        description="Test runs the benchmark outside the Faban Harness">
        <java classname="com.sun.faban.driver.core.MasterImpl"
              classpathref="classpath"
              fork="true"
              failonerror="true"
              dir="config">
              <jvmarg value="-XX:+DisableExplicitGC"/>
              <jvmarg value="-Djava.security.policy=security/driver.policy"/>
              <jvmarg value="-Djava.util.logging.config.file=logging.properties"/>
              <jvmarg value="-Dbenchmark.config=../deploy/run.xml"/>
        </java>
    </target>
    
    <target name="registry"
        description="Starts the Faban registry">
        <java classname="com.sun.faban.common.RegistryImpl"
              classpathref="classpath"
              fork="true"
              failonerror="true">
              <jvmarg value="-XX:+DisableExplicitGC"/>
              <jvmarg value="-Djava.security.policy=security/driver.policy"/>
              <jvmarg value="-Djava.util.logging.config.file=logging.properties"/>
        </java>            
    </target>
    
    <target name="agent"
        description="Start a Faban agent">
        <!-- To start the agent, properties driver.name, driver.id, 
             and master.host need to be declared -->
        <java classname="com.sun.faban.driver.core.AgentImpl"
              classpathref="classpath"
              fork="true"
              failonerror="true">
              <jvmarg value="-XX:+DisableExplicitGC"/>
              <jvmarg value="-Djava.security.policy=security/driver.policy"/>
              <jvmarg value="-Djava.util.logging.config.file=logging.properties"/>
              <arg value="${driver.name}"/>
              <arg value="${driver.id}"/>
              <arg value="${master.host}"/>
        </java>                        
    </target>
    
    <target name="multi"
        description="Start a distributed benchmark run">
        <parallel>
            <antcall target="registry"/>
            <sequential>
                <sleep seconds="2"/>
                <antcall target="agent"/>                
            </sequential>
            <sequential>
                <sleep seconds="5"/>
                <antcall target="run"/>
            </sequential>
        </parallel>
    </target>
      
    <target name="cvs-status">
        <echo message="testing cvs via ant"/>
          
        <cvs command="-d :pserver:kl111120@paedata.sfbay:/data/CVS/Faban -q status" output="patch.txt"/>                     
    </target>
    
    <target name="cvs-add-files">
        <loadresource property="message">
        <file file="patch.txt"/>
        <filterchain>
        <linecontains>
            <contains value="?"/>            
        </linecontains>
        <linecontains negate="true">
        <contains value="build"/>
        </linecontains>
        </filterchain>
        </loadresource>
        <echo message="${message}"/>
        <cvs command="-d :pserver:kl111120@paedata.sfbay:/data/CVS/Faban -q status ${message}"/>
    </target>
    
    <target name="cvs-make-removal-list">
        <loadresource property="message">
        <file file="patch.txt"/>
        <filterchain>
            <tokenfilter>
                <containsstring contains="File: no file"/>
            </tokenfilter> 
            <tokenfilter>
                <ignoreblank/>
            </tokenfilter>            
        </filterchain>
        </loadresource>        
        <echo message="${message}" file="filesToRemove.txt"/>
        
        <!--cvs command="-d :pserver:kl111120@paedata.sfbay:/data/CVS/Faban -q status ${message}"/-->
    </target>
    
    <target name="cvs-remove-files">
        <typedef name="class-parse" classname="my.ant.CVSStatParser" classpath="${classes.dir}"/>
        <typedef name="path-parse" classname="my.ant.CVSPathParser" classpath="${classes.dir}"/>
        
        <loadresource property="filename.remove">
        <file file="filesToRemove.txt"/>
        <filterchain>
            <tokenfilter>  
                <class-parse/>
            </tokenfilter>
       </filterchain>
       <filterchain>                
            <tokenfilter>
                <ignoreblank/>
            </tokenfilter>
        </filterchain>
        <!-- filterchain>
                <striplinebreaks/>       
        </filterchain -->                        
        </loadresource>
        <!-- putting the filename of files to remove in the file nameOfFiles -->
        <echo message="${filename.remove}" file="nameOfFiles"/>
        
        <!-- testing loading of file -->
        
        <loadresource property="filename.echo">
            <file file="nameOfFiles"/>
        
        <filterchain>
         <linecontains>
            <contains value="java"/>            
        </linecontains>
        </filterchain>
        <filterchain>
            <striplinebreaks/>
        </filterchain>
        </loadresource>
        <echo message="echoing filename: ${filename.echo}." />
        
        
        <echo message="from custom parse class, the name of the file is ${filename.remove}." />
        <!-- property name="filename.to.remove" value="${filename.remove}"/ -->
        <loadresource property="cvsfile.path">
            <!-- now have filename in nameOfFiles file.  need to get path -->
        <file file="patch.txt"/>
        
        <filterchain>
            <expandproperties/>
            <linecontains>
                <contains value="Repository revision"/>
            </linecontains>
            
            <linecontains>               
            <contains value="${filename.remove}"/>
            </linecontains>            
                <striplinebreaks/>                
        </filterchain>
        <filterchain>
            <tokenfilter>
                <path-parse/>
            </tokenfilter>
        </filterchain>
        </loadresource>
        <echo message="path taken from file is: ${cvsfile.path}." />
        <!--cvs command="-d :pserver:kl111120@paedata.sfbay:/data/CVS/Faban -q status ${cvsfile.path}"/ -->
        
        
    </target>
        
    
    
</project>
