<?xml version="1.0" encoding="UTF-8" ?>
<project name="gitbucket" default="all" basedir="..">

  <property environment="env"/>
  <property name="target.dir" value="target"/>
  <property name="embed.classes.dir" value="${target.dir}/embed-classes"/>
  <property name="jetty.dir" value="embed-jetty"/>
  <property name="scala.version" value="2.11"/>
  <property name="gitbucket.version" value="${env.GITBUCKET_VERSION}"/>
  <property name="jetty.version" value="8.1.16.v20140903"/>
  <property name="servlet.version" value="3.0.0.v201112011016"/>

  <condition property="sbt.exec" value="sbt.bat" else="sbt.sh">
    <os family="windows" />
  </condition>

  <target name="clean">
    <delete dir="${embed.classes.dir}"/>
    <delete file="${target.dir}/scala-${scala.version}/gitbucket.war"/>
  </target>

  <target name="war" depends="clean">
    <exec executable="${sbt.exec}" resolveexecutable="true" failonerror="true">
      <arg line="clean compile test package" />
    </exec>
  </target>

  <target name="embed" depends="war">
    <mkdir dir="${embed.classes.dir}"/>

    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/javax.servlet-${servlet.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-continuation-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-http-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-io-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-security-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-server-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-servlet-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-util-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-webapp-${jetty.version}.jar" />
    <unzip dest="${embed.classes.dir}" src="${jetty.dir}/jetty-xml-${jetty.version}.jar" />

    <zip destfile="${target.dir}/scala-${scala.version}/gitbucket_${scala.version}-${gitbucket.version}.war"
         basedir="${embed.classes.dir}"
         update = "true"
         includes="javax/**,org/**"/>

    <zip destfile="${target.dir}/scala-${scala.version}/gitbucket_${scala.version}-${gitbucket.version}.war"
         basedir="${target.dir}/scala-${scala.version}/classes"
         update = "true"
         includes="JettyLauncher.class,HttpsSupportConnector.class"/>
  </target>

  <target name="rename" depends="embed">
    <move file="${target.dir}/scala-${scala.version}/gitbucket_${scala.version}-${gitbucket.version}.war"
            tofile="${target.dir}/scala-${scala.version}/gitbucket.war"/>
  </target>

  <target name="all" depends="rename">
  </target>


</project>
