<?xml version="1.0"?>
<project name="Audiobook Recorder" default="build">

    <property file="resources/uk/co/majenko/audiobookrecorder/config.txt"/>

    <taskdef name="jarbundler" classname="com.ultramixer.jarbundler.JarBundler" classpath="ant-libs/jarbundler-2.3.2.jar" />

    <taskdef name="launch4j" classname="net.sf.launch4j.ant.Launch4jTask" classpath="launch4j/launch4j.jar:launch4g/lib/xstream.jar" />


    <target name="clean" description="Clean out the build directories">
        <delete dir="bin" />
        <delete file="AudiobookRecorder.jar" />
    </target>

    <target name="compile" description="Compile" depends="">
        <mkdir dir="bin" />
        <path id="class.path">
            <fileset dir="deps">
                <include name="*.jar" />
            </fileset>
        </path>
        <javac target="1.8" 
               source="1.8"
               debug="true"
               debuglevel="lines,vars,source"
               encoding="UTF-8"
               bootclasspath="${bootclass.path}"
               includeAntRuntime="false"
               deprecation="true"
               srcdir="src" 
               destdir="bin">
            <classpath refid="class.path" />
            <compilerarg value="-Xlint:unchecked"/>
        </javac>
    </target>		

    <target name="build" depends="compile" description="Build">
        <jar basedir="bin" destfile="AudiobookRecorder.jar">
            <fileset dir="bin" />
            <fileset dir="resources" />
            <zipgroupfileset dir="deps" includes="*.jar" />
            <manifest>
                <attribute name="Main-Class" value="uk.co.majenko.audiobookrecorder.AudiobookRecorder" />
                <attribute name="Class-Path" value="AudiobookRecorder.jar" />
                <attribute name="Version" value="${version}" />
            </manifest>
        </jar>
        <chmod perm="0755" file="uecide.jar" />
    </target>

    <target name="macapp" depends="build">

        <mkdir dir="tmp"/>

        <jarbundler
            name="AudiobookRecorder"
            shortname="AudiobookRecorder"
            icon="dist/macosx/audiobookrecorder.icns"
            stubfile="universalJavaApplicationStub/src/universalJavaApplicationStub"
            dir="tmp"
            jar="AudiobookRecorder.jar"
            mainclass="uk.co.majenko.audiobookrecorder.AudiobookRecorder"
            jvmversion="1.7+"
            >
        </jarbundler>

        <symlink link="tmp/Applications" resource="/Applications" overwrite="true" />

        <!--copy file="dist/macosx/dmg.icns" tofile="tmp/.VolumeIcon.icns" /-->

        <exec executable="genisoimage">
            <arg value="-D" />
            <arg value="-V" />
            <arg value="AudiobookRecorder" />
            <arg value="-no-pad" />
            <arg value="-r" />
            <arg value="-apple" />
            <arg value="-o" />
            <arg value="AudiobookRecorder-osx.dmg" />
            <arg value="-dir-mode" />
            <arg value="0755" />
            <arg value="-file-mode" />
            <arg value="0755" />
            <arg value="tmp" />
        </exec>

        <delete dir="tmp" />
    </target>

    <target name="winapp" depends="build">
        <launch4j configFile="dist/windows/windows.xml" outfile="AudiobookRecorder-win.exe" />
    </target>

    <target name="linuxapp" depends="build">
        <concat destfile="AudiobookRecorder-linux" binary="true">
            <file file="dist/linux/stub" />
            <file file="AudiobookRecorder.jar" />
        </concat>
        <chmod perm="0755" file="AudiobookRecorder-linux" />
    </target>

    <target name="dist" depends="macapp,winapp,linuxapp" />

    <target name="tag">
        <exec executable="git">
            <arg value="tag" />
            <arg value="-m" />
            <arg value="Auto generated tag" />
            <arg value="-s" />
            <arg value="v${version}" />
        </exec>
        <exec executable="git">
            <arg value="push" />
            <arg value="--tags" />
        </exec>
    </target>

    <target name="release" depends="dist,tag">
        <echo>Creating release v${version}</echo>
        <exec executable="./mklog"
              outputproperty="changelog">
            <arg value="v${version}"/>
        </exec>
        <exec executable="github-release">
            <arg value="release"/>
            <arg value="-u"/> <arg value="MajenkoProjects"/>
            <arg value="-r"/> <arg value="AudiobookRecorder"/>
            <arg value="-t"/> <arg value="v${version}"/>
            <arg value="-d"/> <arg value="${changelog}"/>
        </exec>
        <echo>Uploading AudiobookRecorder.jar</echo>
        <exec executable="github-release">
            <arg value="upload"/>
            <arg value="-u"/> <arg value="MajenkoProjects"/>
            <arg value="-r"/> <arg value="AudiobookRecorder"/>
            <arg value="-t"/> <arg value="v${version}"/>
            <arg value="-f"/> <arg value="AudiobookRecorder.jar"/>
            <arg value="-n"/> <arg value="AudiobookRecorder.jar"/>
        </exec>
        <echo>Uploading AudiobookRecorder-osx.dmg</echo>
        <exec executable="github-release">
            <arg value="upload"/>
            <arg value="-u"/> <arg value="MajenkoProjects"/>
            <arg value="-r"/> <arg value="AudiobookRecorder"/>
            <arg value="-t"/> <arg value="v${version}"/>
            <arg value="-f"/> <arg value="AudiobookRecorder-osx.dmg"/>
            <arg value="-n"/> <arg value="AudiobookRecorder-osx.dmg"/>
        </exec>
        <echo>Uploading AudiobookRecorder-win.exe</echo>
        <exec executable="github-release">
            <arg value="upload"/>
            <arg value="-u"/> <arg value="MajenkoProjects"/>
            <arg value="-r"/> <arg value="AudiobookRecorder"/>
            <arg value="-t"/> <arg value="v${version}"/>
            <arg value="-f"/> <arg value="AudiobookRecorder-win.exe"/>
            <arg value="-n"/> <arg value="AudiobookRecorder-win.exe"/>
        </exec>
        <echo>Uploading AudiobookRecorder-linux</echo>
        <exec executable="github-release">
            <arg value="upload"/>
            <arg value="-u"/> <arg value="MajenkoProjects"/>
            <arg value="-r"/> <arg value="AudiobookRecorder"/>
            <arg value="-t"/> <arg value="v${version}"/>
            <arg value="-f"/> <arg value="AudiobookRecorder-linux"/>
            <arg value="-n"/> <arg value="AudiobookRecorder-linux"/>
        </exec>
    </target>

</project>
