<!--
    P5 applet exporter for Eclipse
    v0.4 : exports java1.1 compatible applet, source code .zip archive and valid xhtml1.0 wrapper
    v0.5 : added optional obfuscation step using http://proguard.sourceforge.net/
    
    last update : 13/05/04
    author : info@toxi.co.uk
    usage and more info : http://proce55ing.net/discourse/yabb/YaBB.cgi?board=general;action=display;num=1072722668;
-->

<project name="${project_name}" default="dist" basedir=".">
    <description>
        basic P5 applet export build script (java1.1 compatible)
    </description>
    <!-- set global properties for this build -->
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist" location="applet"/>
    <property name="data" location="bin/data"/>
    <property name="JAVA_HOME" location="C:\j2sdk1.4.2"/>

    <target name="init">
        <!-- check if user properties are set, terminate if necessary -->
        <fail unless="project_name">no project name specified</fail>
        <fail unless="classpath.processing.export">path to Processing's export classes missing</fail>
        <fail unless="applet.width">no applet width specified</fail>
        <fail unless="applet.height">no applet height specified</fail>
    
        <!-- check if data folder exists -->
        <available file="${data}" type="dir" property="data_exists"/>
        
        <!-- make sure the ${build} directory is empty -->
        <delete dir="${build}"/>
        <!-- (re)create the build directory used by the compile task -->
        <mkdir dir="${build}"/>
    </target>

    <target name="compile" depends="init" description="compile the source">
        <!-- Compile the java code from ${src} into ${build} -->
        <javac fork="true" target="1.1" srcdir="${src}" destdir="${build}"/>
    </target>

    <target name="dist" depends="compile" description="generate the distribution">
        <!-- Create the distribution directory -->
        <mkdir dir="${dist}"/>
    
        <!-- copy sketch classes into dist dir -->
        <copy todir="${dist}">
            <fileset dir="${build}">
                <include name="**/*.class"/>
            </fileset>
        </copy>
    
        <!-- Copy P5 export classes -->
        <copy todir="${build}">
            <fileset dir="${classpath.processing.export}">
                <include name="**/*.class"/>
            </fileset>
        </copy>

        <!-- copy data if necessary -->
        <antcall target="copyData"/>
        
        <!-- Put everything in ${build} into a .jar file -->
        <property name="final_jar_file" location="${dist}/${project_name}.jar"/>
        <jar jarfile="${final_jar_file}" basedir="${build}"/>
    
    	<!-- call Proguard obfuscator -->
        <available classname="proguard.ProGuard" property="proguard.present"/>
        <antcall target="obfuscate"/>
    	
        <!-- create HTML wrapper file -->
        <antcall target="makeHTML"/>
    </target>
	
    <target name="obfuscate" if="proguard.present">
        <property name="temp_jar_file" location="${dist}/${project_name}_orig.jar"/>
        <move file="${final_jar_file}" tofile="${temp_jar_file}"/>
        <java classname="proguard.ProGuard">
            <arg value="-injars ${temp_jar_file} -outjar ${final_jar_file} -libraryjars ${JAVA_HOME}/jre/lib/rt.jar -keep public class ${project_name}"/>
        </java>
        <echo>NOTE: in case the obfuscated archive is damaged, the original .jar file (without obfuscation) has been moved to:</echo>
        <echo>${temp_jar_file}</echo>
    </target>

    <target name="copyData" if="data_exists">
        <!-- copy contents of data folder -->
        <copy todir="${build}">
            <fileset dir="${data}">
                <include name="**/*.*"/>
            </fileset>
        </copy>
    </target>

    <target name="makeHTML" description="generate HTML wrapper file">
        <!-- building source code zip file -->
        <property name="src.zipfile" value="${project_name}_src.zip"/>
        <echo>archiving source code...</echo>
        <zip destfile="${dist}/${src.zipfile}" basedir="${src}" update="true"/>
        
        <!-- generating html -->
        <echo file="${dist}/index.html"><![CDATA[
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>${project_name}: Built with Processing</title>
<script type="text/javascript">
<!--
function insertApplet() {
document.write('<applet code="${project_name}" archive="${project_name}.jar" width="${applet.width}" height="${applet.height}"><'+'/applet>');
}
//-->
</script>
<style type="text/css">
html, body { background: #333; color: #eee; }
a:link { color: #fc3; }
a:visited { color: #ccc; }
td  { font-size: 11px; font-family: Verdana,Arial,sans-serif; text-align: left; }
</style>
</head>
<body>
<center>
<table width="600" border="0" cellspacing="0" cellpadding="10">
<tr>
<td><script type="text/javascript">insertApplet();</script></td>
</tr>
]]></echo>
        <antcall target="insertComments"/>
        <echo file="${dist}/index.html" append="true"><![CDATA[
<tr>
<td><a href="${src.zipfile}">Source code</a></td>
</tr>
</table>
</center>
</body>
</html>
]]></echo>
    </target>

    <target name="insertComments" if="applet.comments">
        <echo>${applet.comments}</echo>
        <echo file="${dist}/index.html" append="true"><![CDATA[<tr><td>${applet.comments}</td></tr>]]></echo>
    </target>

</project>