<project name="grails-release">
    <patternset id="binary.files.pattern">
        <include name="*/bin/cygrails"/>
        <include name="*/bin/grails"/>
        <include name="*/bin/grails-debug"/>
        <include name="*/ant/bin/ant"/>
        <include name="*/bin/startGrails"/>
    </patternset>

    <!--
       Ideally we would use an "invert" selector rather than an explicit
       pattern set for exclusions, but that's only available in Ant 1.7.1.
     -->
    <patternset id="non-binary.files.pattern">
        <exclude name="*/ant/bin/ant"/>
        <exclude name="*/bin/cygrails"/>
        <exclude name="*/bin/grails"/>
        <exclude name="*/bin/grails-debug"/>
        <exclude name="*/bin/startGrails"/>
    </patternset>

    <target name="create-tmp-release-dir">
        <delete dir="${dir.location}" failonerror="false" quiet="true"/>
        <mkdir dir="${dir.location}"/>
    </target>

    <target name="copy-dir">
        <mkdir dir="${dir.location}"/>
        <copy todir="${dir.destination}" failonerror="true" overwrite="true" verbose="false">
            <fileset dir="${dir.location}">
                <exclude name="**/CVS/**"/>
            </fileset>
        </copy>
    </target>

    <target name="copy-common-release-content">
        <mkdir dir="${release.dir}/lib" />
        <copy todir="${release.dir}/lib" failonerror="true" overwrite="true" verbose="false">
           <fileset dir="lib">
               <exclude name="**/CVS/**"/>
           </fileset>
        </copy>
        <antcall target="copy-dir">
            <param name="dir.location" value="bin"/>
            <param name="dir.destination" value="${release.dir}/bin"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="ant"/>
            <param name="dir.destination" value="${release.dir}/ant"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="media"/>
            <param name="dir.destination" value="${release.dir}/media"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="samples"/>
            <param name="dir.destination" value="${release.dir}/samples"/>
        </antcall>

        <antcall target="copy-dir">
            <param name="dir.location" value="scripts"/>
            <param name="dir.destination" value="${release.dir}/scripts"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="conf"/>
            <param name="dir.destination" value="${release.dir}/conf"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="doc"/>
            <param name="dir.destination" value="${release.dir}/doc"/>
        </antcall>
        <copy todir="${release.dir}" file="src/resources/docs/README" overwrite="true" />
        <copy todir="${release.dir}/bin" file="src/resources/bash/startGrails" overwrite="true" />
        <copy todir="${release.dir}/bin" file="src/resources/bash/startGrails.bat" overwrite="true" />
		<replace 
		    file="${release.dir}/README">
		  <replacefilter 
		    token="@grails.version@" 
		    value="${grails.version}"/>
		</replace>
		

		
		<replace 
		    dir="${release.dir}/bin">
		  <replacefilter 
		    token="@grails.version@" 
		    value="${grails.version}"/>
		</replace>
		

        <copy todir="${release.dir}">
            <fileset dir=".">
                <include name="build.xml"/>
                <include name="build.properties"/>
                <include name="LICENSE"/>
                <include name="INSTALL"/>
            </fileset>
        </copy>


    </target>

    <target name="copy-exclusive-binary-release-content">
        <antcall target="copy-dir">
            <param name="dir.location" value="src/grails"/>
            <param name="dir.destination" value="${release.dir}/src/grails"/>
        </antcall>
		
		<replace 
		    file="${release.dir}/src/grails/grails-macros.xml">
		  <replacefilter 
		    token="@grails.version@" 
		    value="${grails.version}"/>
		</replace>
		
        <antcall target="copy-dir">
            <param name="dir.location" value="src/war"/>
            <param name="dir.destination" value="${release.dir}/src/war"/>
        </antcall>
        <copy todir="${release.dir}/dist">
            <fileset dir="${grails.dist}" includes="*.jar">
                <exclude name="*.zip"/>
                <exclude name="*.tar.gz"/>
            </fileset>
        </copy>
    </target>

    <target name="copy-exclusive-source-release-content">
        <antcall target="copy-dir">
            <param name="dir.location" value="cruise/coverage/lib"/>
            <param name="dir.destination" value="${release.dir}/cruise/coverage/lib"/>
        </antcall>
        <antcall target="copy-dir">
            <param name="dir.location" value="src"/>
            <param name="dir.destination" value="${release.dir}/src"/>
        </antcall>
        <copy todir="${release.dir}/lib" failonerror="true" overwrite="true" verbose="false">
           <fileset dir="lib" includes="hibernate**"/>
        </copy>
        <copy todir="${release.dir}/dist">
            <fileset dir="${grails.dist}" includes="*.jar">
                <exclude name="*.zip"/>
                <exclude name="*.tar.gz"/>
            </fileset>
        </copy>
    </target>

    <target name="create-archives">
        <delete failonerror="true">
			<fileset dir="${grails.dist}" includes="*.zip" />
		</delete>
        <zip destfile="${grails.dist}/${archive}.zip">
            <zipfileset dir="${archive.dir}">
                <patternset refid="non-binary.files.pattern"/>
            </zipfileset>
            <zipfileset dir="${archive.dir}" filemode="755">
                <patternset refid="binary.files.pattern"/>
            </zipfileset>
        </zip>
        <zip destfile="${grails.dist}/grails-docs-${grails.version}.zip">
            <zipfileset dir="${basedir}/doc" />
        </zip>

    </target>

    <target name="create-release-package">
        <property name="grails.release" value="${release.name}"/>
        <property name="grails.release.base.dir.name" value="grails-${grails.version}"/>
        <property name="grails.release.dir" location="tmp/${grails.release.base.dir.name}"/>



        <antcall target="create-tmp-release-dir">
            <param name="dir.location" value="${grails.release.dir}"/>
        </antcall>
        <antcall target="copy-common-release-content">
            <param name="release.dir" value="${grails.release.dir}"/>
        </antcall>

        <mkdir dir="${grails.release.dir}/plugins" />
        <get src="http://svn.codehaus.org/grails/trunk/grails-plugins/grails-hibernate/trunk/grails-hibernate-${grails.version}.zip"
             dest="${grails.release.dir}/plugins/grails-hibernate-${grails.version}.zip"
             verbose="true"
             usetimestamp="true" />
        <get src="http://svn.codehaus.org/grails/trunk/grails-plugins/grails-tomcat/trunk/grails-tomcat-${grails.version}.zip"
             dest="${grails.release.dir}/plugins/grails-tomcat-${grails.version}.zip"
             verbose="true"
             usetimestamp="true" />
        
        <antcall target="copy-exclusive-binary-release-content">
	            <param name="release.dir" value="${grails.release.dir}"/>
		</antcall>
        <antcall target="copy-exclusive-source-release-content">
	            <param name="release.dir" value="${grails.release.dir}"/>
		</antcall>
        <antcall target="create-archives">
            <param name="archive" value="${grails.release}"/>
            <param name="archive.dir" value="tmp"/>
            <param name="archive.release.dir" value="tmp"/>
        </antcall>
        <delete dir="${grails.release.dir}" failonerror="true"/>
    </target>


    <target name="create-binary-release">
        <antcall target="create-release-package">
            <param name="release.name" value="${grails.dist.binary}"/>
        </antcall>
    </target>


</project>
