<?xml version="1.0"?>

<project name="Helma" default="usage" basedir=".">

    <!-- =================================================================== -->
    <!-- Initializes some variables                                          -->
    <!-- =================================================================== -->
    <target name="init">
        <property name="Name" value="helma"/>
        <property name="year" value="1998-${year}"/>
        <property name="version" value="1.2pre3"/>
        <property name="project" value="helma"/>
        <property name="build.compiler" value="classic"/>

        <property name="cvs.root.apps" value=":pserver:anonymous@adele.helma.at:/opt/cvs/apps"/>
        <property name="cvs.root.helma" value=":pserver:anonymous@adele.helma.at:/opt/cvs/helma"/>

        <property name="home.dir" value=".."/>
        <property name="build.dir" value="${home.dir}/build"/>
        <property name="build.src" value="${home.dir}/src"/>
        <property name="build.lib" value="${home.dir}/lib"/>
        <property name="build.classes" value="${home.dir}/classes"/>
        <property name="build.docs" value="${home.dir}/docs"/>
        <property name="build.javadocs" value="${home.dir}/docs/api"/>

        <property name="build.work" value="${home.dir}/work"/>
        <property name="build.dist" value="${home.dir}/dist"/>

        <property name="jar.name" value="${project}"/>
        <property name="package.name" value="${project}-${version}"/>
        <property name="antclick.name" value="antclick-1.0pre2"/>

        <property name="debug" value="on"/>
        <property name="optimize" value="on"/>
        <property name="deprecation" value="off"/>

        <path id="build.class.path">
            <fileset dir="${home.dir}/lib">
                <exclude name="**/helma*.jar" />
                <include name="**/*.jar" />
            </fileset>
        </path>

        <tstamp/>

        <filter token="year" value="${year}"/>
        <filter token="version" value="${version}"/>
        <filter token="date" value="${TODAY}"/>
    </target>


    <!-- =================================================================== -->
    <!-- Help on usage                                                       -->
    <!-- =================================================================== -->
    <target name="help" depends="usage" />
    <target name="usage">
        <echo message=""/>
        <echo message=""/>
        <echo message="Helma build instructions"/>
        <echo message="-------------------------------------------------------------"/>
        <echo message=""/>
        <echo message=" available targets are:"/>
        <echo message=""/>
        <echo message=" compile    --> compiles the source code to ./classes"/>
        <echo message=" jar        --> generates the ./lib/helma-YYYYMMDD.jar file"/>
        <echo message=" javadocs   --> generates the API docs"/>
        <echo message=" docs       --> tries to retrieve the HTML documentation "/>
        <echo message="                (may need proxy settings in startscript)"/>
        <echo message=" package    --> generates the distribution (zip and tar.gz)"/>
        <echo message=" antclick   --> generates the distribution (zip and tar.gz)"/>
        <echo message="                with antville preconfigured"/>
        <echo message=" app [name] --> gets an application from the cvs and zips it"/>
        <echo message=""/>
        <echo message=" usage      --> provides help on using the build tool (default)"/>
        <echo message=""/>
        <echo message=" See comments inside the build.xml file for more details."/>
        <echo message="-------------------------------------------------------------"/>
        <echo message=""/>
        <echo message=""/>
    </target>


    <!-- =================================================================== -->
    <!-- Compiles the source directory                                       -->
    <!-- =================================================================== -->
    <target name="compile" depends="init">
        <mkdir dir="${build.classes}"/>
        <javac srcdir="${build.src}"
            destdir="${build.classes}"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}">
            <classpath refid="build.class.path" />
        </javac>
        <rmic classname="helma.framework.core.RemoteApplication" base="${build.classes}"/>
    </target>


    <!-- =================================================================== -->
    <!-- Creates a helma.jar file (snapshot) in the lib-directory            -->
    <!-- =================================================================== -->
    <target name="jar" depends="compile">
        <jar jarfile="${build.lib}/${jar.name}-${DSTAMP}.jar"
            basedir="${build.classes}"
            excludes="**/package.html,**/main/launcher/**"/>
        <jar jarfile="${home.dir}/launcher.jar"
            basedir="${build.classes}"
            includes="**/main/launcher/**"
            manifest="${build.src}/helma/main/launcher/manifest.txt"/>
    </target>


    <!-- =================================================================== -->
    <!-- Creates the API documentation                                       -->
    <!-- =================================================================== -->
    <target name="javadocs" depends="init">
        <mkdir dir="${build.javadocs}"/>
        <javadoc packagenames="helma.*"
            sourcepath="${build.src}"
            destdir="${build.javadocs}"
            author="false"
            private="false"
            version="false"
            windowtitle="${Name} ${version} API"
            doctitle="${Name} ${version} API"
            bottom="Copyright &#169; ${year} Helma.org. All Rights Reserved."
            classpathref="build.class.path"
        />
    </target>


    <!-- =================================================================== -->
    <!-- Get the documentation (currently can fail due to request time-out   -->
    <!-- or missing support for proxies)                                     -->
    <!-- =================================================================== -->
    <target name="docs" depends="init">
        <get src="http://www.helma.org/docs/reference/print"
             dest="${build.docs}/reference.html"
             ignoreerrors="true"
        />
    </target>


    <!-- =================================================================== -->
    <!-- Creates the full helma distribution                                 -->
    <!-- =================================================================== -->
    <target name="package" depends="init">
        <mkdir dir="${build.work}"/>
        
        <!-- create the main part of helma -->
        <antcall target="package-raw">
            <param name="distribution" value="main" />
        </antcall>

        <chmod perm="755">
            <fileset dir="${build.work}">
                <include name="hop.sh"/>
            </fileset>
        </chmod>

        <!-- checkout the demo apps (and zip manage-app) -->
        <antcall target="package-apps" />

        <!-- zip up the whole thing -->
		<antcall target="package-zip">
            <param name="filename" value="${package.name}"/>
        </antcall>
        <antcall target="package-tgz">
            <param name="filename" value="${package.name}"/>
        </antcall>

        <!-- clean up -->
        <delete dir="${build.work}"/>
    </target>


    <!-- =================================================================== -->
    <!-- Compile Helma and prepare the skeleton in a temporary directory.    -->
    <!-- Used by package and antclick.                                       -->
    <!-- =================================================================== -->
    <target name="package-raw" depends="init, jar">

        <!-- copy the framework (apps.props, server.props, hop/db, hop/static) -->
        <copy todir="${build.work}">
            <fileset dir="${build.dir}/${distribution}" excludes="**/CVS**"/>
        </copy>

        <!-- copy the whole docs-directory -->
        <copy todir="${build.work}/docs">
            <fileset dir="${build.docs}"/>
        </copy>

        <!-- copy all libraries except helma-YYYYMMDD.jar -->
        <copy todir="${build.work}/lib">
            <fileset dir="${home.dir}/lib">
                <exclude name="**/helma*.jar" />
                <include name="**/*.jar" />
            </fileset>
        </copy>

        <!-- copy the whole licenses-directory -->
        <copy todir="${build.work}/licenses">
            <fileset dir="${home.dir}/licenses" excludes="**/CVS**"/>
        </copy>

        <!-- rename the current jar-file -->
        <copy file="${build.lib}/${jar.name}-${DSTAMP}.jar" tofile="${build.work}/lib/helma.jar"/>

        <!-- zip the sourcecode -->
        <mkdir dir="${build.work}/src"/>
        <tar tarfile="${build.work}/src/helma-src.tar" basedir="${build.src}/">
             <tarfileset dir="${build.src}">
                  <include name="${build.src}/**"/>
             </tarfileset>
        </tar>
        <gzip zipfile="${build.work}/src/helma-src.tar.gz" src="${build.work}/src/helma-src.tar"/>
        <delete file="${build.work}/src/helma-src.tar"/>
    </target>


    <!-- =================================================================== -->
    <!-- Checkout demo apps, put them in work directory and zip manage app   -->
    <!-- =================================================================== -->
    <target name="package-apps" depends="init">

      	<mkdir dir="${build.work}/apps" />

        <!-- get demo apps -->
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="base" dest="${build.work}/apps" />
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="bloggerapi" dest="${build.work}/apps" />
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="himp" dest="${build.work}/apps" />
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="hopblog" dest="${build.work}/apps" />
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="lillebror" dest="${build.work}/apps" />

        <antcall target="package-manage" />

        <!-- write out apps.properties file -->
        <echo file="${build.work}/apps.properties" append="false">
# list of applications to be started by helma
base
base.mountpoint = /
bloggerapi
himp
hopblog
lillebror
manage
</echo>
    </target>


    <!-- =================================================================== -->
    <!-- Checkout and zip manage application                                 -->
    <!-- =================================================================== -->
    <target name="package-manage" depends="init">
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="manage" dest="${build.work}" />
        <mkdir dir="${build.work}/apps/manage"/>
        <zip zipfile="${build.work}/apps/manage/manage.zip" basedir="${build.work}/manage/" includes="**" excludes="**/properties,readme/**" />
            <copy todir="${build.work}/apps/manage">
                <fileset dir="${build.work}/manage" includes="app.properties,class.properties,readme.txt"/>
            </copy>
        <delete dir="${build.work}/manage" />
    </target>


    <!-- =================================================================== -->
    <!-- Packages the work directory with TAR-GZIP                           -->
    <!-- needs parameter ${filename} for final dist-file                     -->
    <!-- =================================================================== -->
    <target name="package-tgz" depends="init">
        <mkdir dir="${build.dist}" />
        <fixcrlf srcdir="${build.work}" eol="lf" eof="remove" includes="**/*.txt, **/*.properties, **/*.hac, **/*.js, **/*.skin" />
        <tar tarfile="${build.dist}/${filename}.tar" basedir="${build.work}" excludes="**">
            <tarfileset prefix="${filename}" dir="${build.work}" mode="755">
                <include name="hop.sh"/>
            </tarfileset>
            <tarfileset prefix="${filename}" dir="${build.work}">
                <include name="**"/>
                <exclude name="hop.sh"/>
            </tarfileset>
        </tar>
        <gzip zipfile="${build.dist}/${filename}.tar.gz" src="${build.dist}/${filename}.tar"/>
        <delete file="${build.dist}/${filename}.tar"/>
    </target>


    <!-- =================================================================== -->
    <!-- Packages the work directory with ZIP                                -->
    <!-- needs parameter ${filename} for final dist-file                     -->
    <!-- =================================================================== -->
    <target name="package-zip" depends="init">
    	<mkdir dir="${build.dist}" />
        <fixcrlf srcdir="${build.work}" eol="crlf" includes="**/*.txt, **/*.properties, **/*.hac, **/*.js, **/*.skin, **/*.xml" />
        <zip zipfile="${build.dist}/${filename}.zip">
            <zipfileset dir="${build.work}" prefix="${filename}" includes="**" />
        </zip>
    </target>


    <!-- =================================================================== -->
    <!-- Make Antclick package (helma plus Antville and manage apps)         -->
    <!-- =================================================================== -->
    <target name="antclick" depends="init">
        <mkdir dir="${build.work}"/>
        
        <!-- create the main part of helma -->
        <antcall target="package-raw">
            <param name="distribution" value="antclick" />
        </antcall>

        <!-- get antville -->
        <mkdir dir="${build.work}/apps" />
        <!-- to retrieve special versions of antville insert additional attributes:
             tag="TAGNAME", date="1972-09-24" or date="1972-09-24 20:05" -->
        <cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="antville" dest="${build.work}/apps" />

        <!-- unzip images -->
        <mkdir dir="${build.work}/static/images"/>
        <unzip src="${build.work}/apps/antville/images.zip" dest="${build.work}/static/images">
            <patternset>
                <include name="**"/>
            </patternset>
        </unzip>

        <!-- delete antville's mysql-scripts, image-zip etc -->
        <delete>
            <fileset dir="${build.work}/apps/antville" includes="images.zip,*.sql" />
        </delete>

        <!-- get and zip manage-app -->
        <antcall target="package-manage" />

        <!-- zip up the whole thing -->
        <antcall target="package-zip">
            <param name="filename" value="${antclick.name}"/>
        </antcall>
        <antcall target="package-tgz">
            <param name="filename" value="${antclick.name}"/>
        </antcall>

        <!-- clean up -->
        <delete dir="${build.work}"/>
    </target>


    <!-- =================================================================== -->
    <!-- Gets an application from the cvs and zips/targzs it                 -->
    <!-- =================================================================== -->
	<target name="app" depends="init">
        <mkdir dir="${build.dist}" />
        <mkdir dir="${build.work}" />

        <!-- to retrieve special versions of an application insert
        additional attributes: tag="TAGNAME" or date="1972-09-24 20:05" -->
      	<cvs cvsRoot="${cvs.root.apps}" command="export" tag="HEAD" package="${application}" dest="${build.work}" />

        <fixcrlf srcdir="${build.work}" eol="crlf" eof="add"  includes="**/*.txt, **/*.properties, **/*.hac, **/*.js, **/*.skin, **/*.xml" />
        <zip zipfile="${build.dist}/${application}-${DSTAMP}.zip" basedir="${build.work}" includes="**"/>

        <fixcrlf srcdir="${build.work}" eol="lf" eof="remove" includes="**/*.txt, **/*.properties, **/*.hac, **/*.js, **/*.skin" />
        <tar tarfile="${build.dist}/${application}-${DSTAMP}.tar" basedir="${build.work}">
			<tarfileset dir="${build.work}">
                <include name="${build.work}/**"/>
            </tarfileset>
        </tar>
        <gzip zipfile="${build.dist}/${application}-${DSTAMP}.tar.gz" src="${build.dist}/${application}-${DSTAMP}.tar" />

        <delete file="${build.dist}/${application}-${DSTAMP}.tar" />
        <delete dir="${build.work}" />
	</target>

</project>