<project name="gbase" default="build.gbase" basedir=".">
  <!-- Path containing core.xml -->
  <dirname property="build_dir" file="${ant.file.gbase}"/>
  <import file="${build_dir}/core.xml"/>

  <!--  ===================================================================== -->
  <!--                 Build the GData Base API library.             -->
  <!--  ===================================================================== -->

  <!-- build Properties -->
  <property name="gdata-base.jar"
            value="${build.jars}/gdata-base-${base.spec.version}.jar"/>

  <!--  ===================================================================== -->
  <!--                 Build the GData Google Base API samples.               -->
  <!--  ===================================================================== -->

  <!-- samples build Properties -->
  <property name="sample.gbase.basedir"       value="${build}/sample/gbase"/>
  <property name="sample.gbase.cmdlinedir"    value="${sample.gbase.basedir}/cmdline"/>
  <property name="sample.gbase.recipe"        value="${sample.gbase.basedir}/recipe"/>
  <property name="sample.gbase.build.classes" value="${sample.gbase.basedir}/classes"/>
  <property name="sample.gbase.build.lib"     value="${sample.gbase.basedir}/lib"/>
  <property name="sample.gbase.cmdline.jar"   value="${sample.gbase.build.lib}/gdata-base-cmdline.jar"/>
  <property name="sample.gbase.recipe.war"    value="${sample.gbase.build.lib}/gdata-base-recipe.war"/>

  <!-- dependency check -->
  <target name="sample.gbase.dependencies">
    <antcall target="template.require.service.jar">
      <param name="template.service.name" value="base"/>
      <param name="template.service.version" value="${base.spec.version}"/>
    </antcall>
  </target>

  <target name="sample.gbase.dirs">
    <mkdir dir="${sample.gbase.build.classes}"/>
    <mkdir dir="${sample.gbase.build.lib}"/>
  </target>

  <target name="sample.gbase.clean"
   description="Delete old build and dist directories">
    <delete dir="${sample.gbase.build.classes}"/>
  </target>

  <!-- samples compile -->
  <path id="sample.gbase.cmdline.compile.classpath">
    <path refid="build.service.media.classpath"/>
    <pathelement location="${gdata-base.jar}"/>
  </path>

  <path id="sample.gbase.recipe.compile.classpath">
    <path refid="build.service.media.classpath"/>
    <pathelement location="${servlet.jar}"/>
    <pathelement location="${gdata-base.jar}"/>
  </path>

  <target name="sample.gbase.cmdline.compile" depends="sample.gbase.dirs,sample.gbase.dependencies"
   description="Compile Java sources">
    <javac srcdir="${sample.gbase.cmdlinedir}"
           destdir="${sample.gbase.build.classes}"
           debug="${javac.debug}" debuglevel="${javac.debuglevel}"
           deprecation="false"
           optimize="true">
        <classpath refid="sample.gbase.cmdline.compile.classpath"/>
    </javac>
  </target>

  <target name="sample.gbase.recipe.compile" depends="sample.gbase.dirs,core.require.servlet-api"
   description="Compile Java sources">
    <javac srcdir="${sample.gbase.recipe}"
           destdir="${sample.gbase.build.classes}"
           debug="false"
           deprecation="false"
           optimize="true">
        <classpath refid="sample.gbase.recipe.compile.classpath"/>
    </javac>
  </target>

  <target name="sample.gbase.compile">
    <antcall target="sample.gbase.cmdline.compile"/>
    <antcall target="sample.gbase.recipe.compile"/>
  </target>

  <!-- samples build -->
  <target name="sample.gbase.cmdline.build" depends="sample.gbase.cmdline.compile"
    description="build the command-line tools ${gdata-base-cmdline.jar}">
    <jar destfile="${sample.gbase.cmdline.jar}"
      basedir="${sample.gbase.build.classes}" >
      <manifest>
        <attribute name="Class-Path" value="../../../${gdata-base.jar}"/>
        <attribute name="Main-Class"
                   value="sample/gbase/cmdline/CustomerTool"/>
      </manifest>
    </jar>
  </target>

  <target name="sample.gbase.recipe.build" depends="sample.gbase.recipe.compile"
    description="build the example web application ${gdata-base-recipe.war}" >
    <war destfile="${sample.gbase.recipe.war}"
      webxml="${sample.gbase.recipe}/WEB-INF/web.xml">
      <classes dir="${sample.gbase.build.classes}">
        <include name="**/sample/gbase/recipe/**"/>
      </classes>
      <lib dir="${build.jars}" >
        <include name="${google-collect.jar}"/>
        <include name="${gdata-client.jar}"/>
        <include name="${gdata-base.jar}"/>
      </lib>
      <webinf dir="${sample.gbase.recipe}/WEB-INF">
        <include name="*.jsp"/>
        <include name="*.inc"/>
      </webinf>
      <zipfileset dir="${sample.gbase.recipe}">
        <include name="*.gif"/>
        <include name="*.css"/>
        <include name="index.jsp"/>
      </zipfileset>
    </war>
  </target>

  <target name="sample.gbase.build" depends="sample.gbase.compile">
    <antcall target="sample.gbase.cmdline.build" />
    <antcall target="sample.gbase.recipe.build" />
  </target>

  <!-- samples run -->
  <target name="sample.gbase.cmdline.customertool.query" depends="sample.gbase.cmdline.build"
    description="Runs the Google Base commandline query sample">
    <java fork="true" classname="sample.gbase.cmdline.CustomerTool">
      <arg line="query --user ${sample.credentials.username} --password ${sample.credentials.password}"/>
      <classpath>
        <path refid="sample.gbase.cmdline.compile.classpath"/>
        <path location="${sample.gbase.cmdline.jar}"/>
      </classpath>
    </java>
  </target>

  <target name="sample.gbase.cmdline.queryexample" depends="sample.gbase.cmdline.build"
    description="Runs the Google Base commandline query sample">
    <java fork="true" classname="sample.gbase.cmdline.QueryExample">
      <arg line="${sample.gbase.cmdline.queryexample.querystring}"/>
      <classpath>
        <path refid="sample.gbase.cmdline.compile.classpath"/>
        <path location="${sample.gbase.cmdline.jar}"/>
      </classpath>
    </java>
  </target>
 
  <target name="sample.gbase.run" description="Runs the Google Base commandline samples">
    <antcall target="sample.gbase.cmdline.customertool.query"/>
    <antcall target="sample.gbase.cmdline.queryexample"/>
  </target>

</project>
