<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="all" name="Create Runnable Jar for Limelight">
  <property name="ant.build.javac.target" value="1.7"/>
  <property name="ant.build.javac.source" value="1.7"/>
  
  <property name="src.dir" location="${basedir}/src"/>
  <property name="libs.dir" location="${basedir}/libs"/>

  <property name="common.dir" location="${libs.dir}/common"/>
  <property name="common.src.dir" location="${common.dir}/src"/>
  <property name="common.libs.dir" location="${common.dir}/libs"/>
  
  <property name="libs.opus.dir" location="${basedir}/jni/nv_opus_dec/"/>
  <property name="libs.omx.dir" location="${basedir}/jni/nv_omx_dec/"/>
  <property name="libs.imx.dir" location="${basedir}/jni/nv_imx_dec/"/>
  <property name="libs.alsa.dir" location="${basedir}/jni/nv_alsa/"/>
  <property name="libs.io.dir" location="${basedir}/jni/nv_io/"/>
  
  <property name="lib.dir" location="${basedir}/lib"/>
  <property name="build.dir" location="${basedir}/build"/>
  
  <property name="one-jar.dist.dir" value="${libs.dir}/build"/>

  <property name="classes.dir" location="${basedir}/classes"/>
  
  <property name="device" value="all"/>

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

  <import file="${one-jar.dist.dir}/one-jar-ant-task.xml"/>

  <taskdef resource="proguard/ant/task.properties" classpath="${one-jar.dist.dir}/proguard.jar" />
  
  <target name="clean">
    <delete dir="${build.dir}"/>
    <delete dir="${classes.dir}"/>
	<exec executable="make" dir="${libs.omx.dir}">
		<arg line="clean"/>
	</exec>
	<exec executable="make" dir="${libs.imx.dir}">
		<arg line="clean"/>
	</exec>
	<exec executable="make" dir="${libs.imx.dir}">
		<arg line="clean"/>
	</exec>
	<exec executable="make" dir="${libs.opus.dir}">
		<arg line="clean"/>
	</exec>
	<exec executable="make" dir="${libs.alsa.dir}">
		<arg line="clean"/>
	</exec>
	<exec executable="make" dir="${libs.io.dir}">
		<arg line="clean"/>
	</exec>
  </target>

  <target name="all" depends="clean, dist"/>
  
  <target name="build" depends="compile-native, compile-common, compile-java"/>
  
  <target name="check-device">
	<condition property="enable.omx" value="true">
	  <or>
        <equals arg1="${device}" arg2="pi" />
        <equals arg1="${device}" arg2="all" />
      </or>
	</condition>
	<condition property="enable.imx" value="true">
	  <or>
        <equals arg1="${device}" arg2="imx" />
        <equals arg1="${device}" arg2="all" />
      </or>
	</condition>
  </target>
  
  <target name="compile-native" depends="check-device, compile-native-pi, compile-native-imx">
	<exec executable="make" dir="${libs.opus.dir}"/>
	<exec executable="make" dir="${libs.alsa.dir}"/>
	<exec executable="make" dir="${libs.io.dir}"/>
  </target>
  
  <target name="compile-native-pi" if="enable.omx">
	<exec executable="make" dir="${libs.omx.dir}"/>
  </target>
  
  <target name="compile-native-imx" if="enable.imx">
    <exec executable="make" dir="${libs.imx.dir}"/>
  </target>  
  
  <target name="compile-java" depends="init">
    <!-- compile limelight -->
    <javac includeantruntime="false" destdir="${classes.dir}/src">
      <src path="${src.dir}"/>
	  <classpath path="${classes.dir}/common"/>
      <classpath>
        <fileset dir="${common.libs.dir}" includes="*.jar"/>
		<fileset dir="${libs.dir}" includes="*.jar"/>
      </classpath>
    </javac>
  </target>
  
  <target name="compile-common" depends="init">
    <!-- compile limelight -->
    <javac includeantruntime="false" destdir="${classes.dir}/common">
      <src path="${common.src.dir}"/>java
      
      <classpath>
        <fileset dir="${common.libs.dir}" includes="*.jar"/>
      </classpath>
    </javac>
  </target>

  <target name="dist" depends="build">
    <!-- construct one-jar file -->
    <one-jar destfile="${build.dir}/limelight.jar">
      <manifest>
        <attribute name="One-Jar-Main-Class" value="com.limelight.Limelight"/>
      </manifest>

      <main>
        <!-- construct limelight.jar -->
        <fileset dir="${classes.dir}/common"/>
        <fileset dir="${classes.dir}/src"/>
      </main>

      <lib>
        <fileset dir="${common.libs.dir}" includes="*.jar"/>
		<fileset dir="${libs.dir}" includes="*.jar"/>
      </lib>

      <binlib>
        <fileset dir="${libs.opus.dir}" includes="*.so"/>
        <fileset dir="${libs.omx.dir}" includes="*.so"/>
		<fileset dir="${libs.imx.dir}" includes="*.so"/>
		<fileset dir="${libs.alsa.dir}" includes="*.so"/>
		<fileset dir="${libs.io.dir}" includes="*.so"/>
      </binlib>
    </one-jar>
  </target>
	
  <target name="proguard">
	<proguard>
      -libraryjars ${java.home}/lib/rt.jar
      -injars      ${build.dir}/limelight.jar
      -outjars     ${build.dir}/limelight-dist.jar

      -keepclasseswithmembers public class * {
          public static void main(java.lang.String[]);
      }
    </proguard>
  </target>

</project>
