<?xml version="1.0" encoding="UTF-8"?>
<project name="DMDirc-versioning" default="default" basedir=".">
    <description>Adds automatic versioning information to DMDirc</description>

    <property name="version.config" value="build/classes/com/dmdirc/version.config"/>
    <property name="defaults.path" value="modules/defaults/.git/"/>
    <property name="defaults.output" value="build/classes/com/dmdirc/config/defaults/"/>
    <property name="defaults.target" value="${defaults.output}default/defaults"/>
    <property name="modealiases.target" value="${defaults.output}modealiases/generic"/>

    <available file=".git" property="is.git"/>

    <target name="-write-version" depends="-write-version-header, -add-version, -write-updater, -write-identities"/>

    <target name="-write-version-header">
        <echo file="${version.config}"># This is a DMDirc configuration file automatically generated by
# the build process.

keysections:
   identity
   version
   updater

identity:
   name=DMDirc version information
   globaldefault=true
   order=95000

version:
   version=</echo>
    </target>

    <target name="-add-version" depends="-add-git-version"/>

    <target name="-add-git-version" if="is.git">
        <exec executable="git" output="${version.config}" append="true">
            <arg value="describe"/>
            <arg value="--tags"/>
        </exec>
    </target>

    <target name="-init-channel" unless="channel">
        <property name="channel" value="NONE"/>
    </target>

    <target name="-write-updater" depends="-init-channel, -read-identities">
        <echo file="${version.config}" append="true">
updater:
    channel=${channel}
    bundleddefaultsversion=${defaults.version}
    bundledmodealiasesversion=${modealiases.version}
</echo>
    </target>

    <target name="-write-identities" depends="-read-identities, -copy-identities">
        <echo file="${defaults.target}" append="true">
identity:
  defaultsversion=${defaults.version}
</echo>
        <echo file="${modealiases.target}" append="true">
identity:
  modealiasversion=${modealiases.version}
</echo>
    </target>

    <target name="-copy-identities">
        <mkdir dir="${defaults.output}"/>
        <copy todir="${defaults.output}" overwrite="true">
            <fileset dir="modules/defaults/" excludes=".git/"/>
        </copy>
    </target>

    <target name="-read-identities" depends="-read-identities-git"/>

    <target name="-read-identities-git" if="is.git">
        <exec dir="." executable="/bin/bash" outputproperty="defaults.version">
            <arg value="-c"/>
            <arg value="git --git-dir ${defaults.path} rev-list --all -n 1 -- default | xargs git --git-dir ${defaults.path} describe --tags"/>
        </exec>
        <exec dir="." executable="/bin/bash" outputproperty="modealiases.version">
            <arg value="-c"/>
            <arg value="git --git-dir ${defaults.path} rev-list --all -n 1 -- modealiases | xargs git --git-dir ${defaults.path} describe --tags"/>
        </exec>
    </target>

</project>
