<project name="generic-building-block" basedir="." default="build">

  <!-- ====================================================================== -->
  <!-- =========================== deploy targets =========================== -->
  <!-- ====================================================================== -->

  <target name="deploy" depends="init, -build, -set-deploy-url"
          description="Dynamically deploys all changes to the building block.">
    <get src="${deploy.url}" dest="${distrib.dir}/deploy.out"/>
  </target>

  <target name="deploy-available" depends="init, -build, -set-deploy-url"
          description="Dynamically deploys all changes and makes the building block available within courses/organizations.">
    <get src="${deploy.url}&amp;available=true" dest="${distrib.dir}/deploy.out"/>
  </target>

  <target name="deploy-clean" depends="init, -build, -set-deploy-url"
          description="Refreshes the currently installed building block.">
    <get src="${deploy.url}&amp;clean=true" dest="${distrib.dir}/deploy.out"/>
  </target>

  <target name="deploy-clean-available" depends="init, -build, -set-deploy-url"
          description="Dynamically deploys all changes after removing the current building block and makes it available within courses/organizations.">
    <get src="${deploy.url}&amp;clean=true&amp;available=true" dest="${distrib.dir}/deploy.out"/>
  </target>

  <!-- ====================================================================== -->
  <!-- ========================= core build targets ========================= -->
  <!-- ====================================================================== -->

  <target name="build" depends="init, -build" description="Generates the Building Block distribution file (WAR)."/>
  <target name="-build" depends="-compile, -prepare">
    <jar jarfile="${warfile}" compress="true" index="true">
      
      <fileset dir="${staging.dir}"/>
    </jar>
    <antcall target="hook-post-build"/>
  </target>

  <target name="compile" depends="init, -compile" description="Compiles all Java source files."/>
  <target name="-compile">
    <mkdir dir="${staging.dir}/WEB-INF/classes"/>
    
    <javac srcdir="../src" destdir="${staging.dir}/WEB-INF/classes" debug="true" deprecation="true">
      <classpath>
        <fileset dir="../WebContent/WEB-INF/lib"                       includes="*.jar"/>
        <fileset dir="${tomcat.home}/lib" includes="*.jar"/>
      	<!--<fileset dir="${staging.dir}/WebContent/WEB-INF/lib" includes="*.jar"/>-->
      </classpath>
    </javac>
  </target>

  <target name="-prepare">
    <!--<mkdir dir="${staging.dir}/WebContent/WEB-INF/classes"/>-->
  	 <copydir src="../WebContent"
  	           dest="${distrib.dir}/generic-building-block"
  	  />
    <antcall target="hook-post-prepare"/>
  </target>

  <!-- Override this target to perform B2-specific actions after the "build" target -->
  <target name="hook-post-build"/>

  <!-- Override this target to perform B2-specific actions after the "prepare" target -->
  <target name="hook-post-prepare"/>

 

  <target name="init" depends="-init, -set-bb-home, -set-bb-host, hook-init">
    <property name="tomcat.home" location="${bb.home}/apps/tomcat"/>

   
    <mkdir dir="${distrib.dir}"/>
  </target>

  <!-- Override this target to set B2 specific properties -->
  <target name="hook-init"/>

  <target name="-init">
    <property name="distrib.dir" location="_distrib_"/>
    <property name="staging.dir" location="${distrib.dir}/${ant.project.name}"/>
    <property name="warfile"     location="${staging.dir}.war"/>

    <property environment="env"/>
  </target>

  <target name="-set-bb-home" depends="-set-bb-home-env, -set-bb-home-default"/>

  <target name="-set-bb-home-env" if="env.BB_HOME">
    <property name="bb.home" location="${env.BB_HOME}"/>
    <echo>Using BB_HOME environment value: ${bb.home}</echo>
  </target>

  <target name="-set-bb-home-default" unless="env.BB_HOME">
    <!-- TODO: Unix environments -->
    <property name="bb.home" location="c:/blackboard"/>
    <echo>Using default BB_HOME value: ${bb.home}</echo>
  </target>

  <target name="-set-bb-host" depends="-set-bb-host-env, -set-bb-host-default"/>

  <target name="-set-bb-host-env" if="env.BB_HOST">
    <property name="bb.host" value="${env.BB_HOST}"/>
    <echo>Using BB_HOST enironment value: ${bb.host}</echo>
  </target>

  <target name="-set-bb-host-default" unless="env.BB_HOST">
    <property name="bb.host" value="localhost"/>
    <echo>Using default BB_HOST value: ${bb.host}</echo>
  </target>

  <target name="-set-deploy-url">
    <!-- TODO: does not work with Ant 1.6
    <makeurl property="warfile.url" file="${warfile}"/>
    -->
    <property name="deploy.url" value="http://${bb.host}/webapps/bb-starting-block-bb_bb60/execute/install?fileName=${warfile}"/>
  </target>

  <!-- ====================================================================== -->
  <!-- =========================== clean targets ============================ -->
  <!-- ====================================================================== -->

  <target name="clean" depends="-init" description="Removes all build generated files.">
    <delete dir="${distrib.dir}"/>
  	<delete includeemptydirs="true" failonerror="false">
  	    <fileset dir="classes" includes="**/*"/>
   </delete>
  	<delete dir="classes" failonerror="false"/>
  </target>

</project>

