<project name="ScalaSamples" default="all" >
    <description>
        Build the Streams Scala samples.
    </description>

  <property environment="env"/>
  <fail unless="env.STREAMS_INSTALL" message="STREAMS_INSTALL not set."/>
  <property name="streams.install" value="${env.STREAMS_INSTALL}"/>

  <!-- If the samples are at the same level as the toolkit
       then use the jar from that toolkit (open source distribution)
       otherwise assume they are in the product
  -->
  <property name="tk.lib.gen" value="com.ibm.streamsx.topology/lib"/>
  <property name="tk.jarname" value="com.ibm.streamsx.topology.jar"/>
  <property name="os.tk.lib" location="../../${tk.lib.gen}"/>
  <available file="${os.tk.lib}/${tk.jarname}"
             property="tk.lib"
             value="${os.tk.lib}/${tk.jarname}"/>
  <available file="${os.tk.lib}/${tk.jarname}"
             property="tk.opt"
             value="${os.tk.lib}/../opt"/>
  <property name="tk.lib"
     location="${streams.install}/toolkits/${tk.lib.gen}/${tk.jarname}"/>
  <property name="tk.opt"
     location="${streams.install}/toolkits/${tk.lib.gen}/../opt"/>

  <property name="scala.home" value="${env.SCALA_HOME}"/>
  <property name="scala-library.jar"
            value="${scala.home}/lib/scala-library.jar"/>
  <property name="src" location="src"/>
  <property name="classes" location="../java/functional/classes"/>


  <path id="compile.classpath">
    <pathelement location="${tk.lib}"/>
    <pathelement location="${streams.install}/lib/com.ibm.streams.operator.samples.jar" />
    <pathelement location="${streams.install}/lib/com.ibm.streams.operator.jar" />
    <pathelement location="${streams.install}/ext/lib/JSON4J.jar" />
    <pathelement location="${tk.opt}/gson-2.3.1/gson-2.3.1.jar" />
    <pathelement location="${scala-library.jar}"   /> </path>

  <path id="run.scala.classpath">
    <pathelement location="../java/functional/functionalsamples.jar" />
    <pathelement location="${tk.lib}"/>
    <pathelement location="${streams.install}/lib/com.ibm.streams.operator.samples.jar" />
  </path>
  <path id="run.java.classpath">
    <path refid="run.scala.classpath"/>
    <pathelement location="${scala-library.jar}"   />
  </path>

 <taskdef resource="scala/tools/ant/antlib.xml">
   <classpath>
     <pathelement location="${scala.home}/lib/scala-compiler.jar"   />
     <pathelement location="${scala.home}/lib/scala-reflect.jar"   />
     <pathelement location="${scala-library.jar}"   />
   </classpath>
 </taskdef>

  <target name="all" depends="compile"/> 

  <target name="init" if="env.SCALA_HOME">
     <echo message="Scala:${scala.home}"/>
     <mkdir dir="${classes}"/>
  </target>

  <target name="compile" depends="init" if="env.SCALA_HOME">
    <scalac srcdir="${src}" target="jvm-1.8" addparams="-usemanifestcp"
                destdir="${classes}"
                classpathref="compile.classpath">
    </scalac>
  </target>

  <target name="run.fizzbuzz">
     <echo message="Runs games.FizzBuzzScala using Scala (SCALA_HOME/bin/scala)"/>
     <echo message="SCALA_HOME:${scala.home}"/>
     <exec executable="${scala.home}/bin/scala">
        <arg value="-classpath"/>
        <arg value="${toString:run.scala.classpath}"/>
        <arg value="games.FizzBuzzScala"/>
     </exec>
  </target>

  <target name="run.java.fizzbuzz">
     <echo message="Runs games.FizzBuzzScala using Java (JAVA_HOME/bin/java)"/>
     <java classname="games.FizzBuzzScala" classpathref="run.java.classpath" fork="yes"/>
  </target>

</project>
