<?xml version="1.0" encoding="UTF-8"?>
<project name="Air Native Extension Build Scripts" default="all">

    <!-- Config -->
    <property file="build.config"/>

    <!-- All -->
    <target name="all" depends="android,swc,package,copy" description="Full build of extension"/>

    <!-- Android -->
    <target name="android" description="Build Android Library">
        <delete dir="../android/temp"/>
        <mkdir dir="../android/temp/classes"/>

        <echo message="Using Java version ${ant.java.version}."/>

        <javac source="1.6" srcdir="../android/src" destdir="../android/temp/classes" includeantruntime="false">
            <classpath>
                <pathelement location="${android.sdk}/android.jar"/>
                <pathelement location="${flex.sdk}/lib/android/FlashRuntimeExtensions.jar"/>
                <pathelement location="../android/AirMoPub/libs/android-support-v4.jar"/>
                <pathelement location="../android/google-play-services_lib/libs/google-play-services.jar"/>
            </classpath>
        </javac>
        
        <!-- <mkdir dir="../android/temp/zip"/>
        <unzip src="../android/google-play-services_lib/libs/google-play-services.jar" dest="../android/temp/zip" />
        <copydir src="../android/temp/zip/com" dest="../android/temp/classes/com"/>

        <copy todir="../temp/android/res/">
            <fileset dir="../android/google-play-services_lib/bin/res"/>
        </copy> -->
        
        <!-- <copy todir="../temp/android/res/values/">
            <fileset dir="${gplayservices.libs}/res/values"/>
        </copy> -->
        
        
        <mkdir dir="../temp/android/"/>
        <jar basedir="../android/temp/classes" destfile="../temp/android/lib${name}.jar"/>
        <delete dir="../android/temp"/>
    </target>

    <!-- Actionscript -->
    <target name="swc" description="Build SWC library">
        <mkdir dir="../temp/swc/content/"/>

        <fileset dir="../actionscript/src" casesensitive="yes" id="classfiles">
            <include name="**/*.as"/>
        </fileset>

        <pathconvert property="classlist" refid="classfiles" pathsep=" " dirsep=".">
            <regexpmapper from=".*src.(.*)\.as" to="\1"/>
        </pathconvert>

        <exec executable="${flex.sdk}/bin/compc${bin.ext}" failonerror="true">
            <env key="AIR_SDK_HOME" value="${flex.sdk}"/>
            <arg line='-source-path ../actionscript/src'/>
            <arg line='-output ../temp/swc/${name}.swc'/>
            <arg line='-swf-version=25'/>
            <arg line='-external-library-path+="${flex.sdk}/frameworks/libs/air/airglobal.swc"'/>
            <arg line='-include-classes ${classlist}'/>
        </exec>

        <unzip src="../temp/swc/${name}.swc" dest="../temp/swc/content" overwrite="true"/>
        <copy file="../temp/swc/content/library.swf" todir="../temp/ios" overwrite="true"/>
        <copy file="../temp/swc/content/library.swf" todir="../temp/android" overwrite="true"/>
        <copy file="../temp/swc/content/library.swf" todir="../temp/default" overwrite="true"/>
        <delete dir="../temp/swc/content/"/>
    </target>

    <!-- Package -->
    <target name="package" description="Create the extension package">
        <exec executable="${flex.sdk}/bin/adt${bin.ext}" failonerror="true" dir="../temp">
            <arg value="-package"/>
            <arg value="-target"/>
            <arg value="ane"/>
            <arg value="${name}.ane"/>
            <arg value="../build/extension.xml"/>
            <arg line="-swc swc/${name}.swc"/>
            <arg line="-platform iPhone-ARM -platformoptions ../build/platform-ios.xml -C ios/ ."/>
            <arg line="-platform Android-ARM -C android/ ."/>
            <arg line="-platform default -C default/ ."/>
        </exec>

        <move file="../temp/${name}.ane" todir="../bin"/>
        <delete dir="../temp"/>
    </target>
    
	<target name="copy" description="Copy the ane to the current directory">
        <copy file="../bin/${name}.ane" todir="${copyDir}" overwrite="true"/>
	</target>
</project>