<?xml version="1.0" encoding="UTF-8"?>
<project name="Install S-RAMP from distro" default="install">

  <property name="s-ramp-distro.build.properties" location="build.properties" />
  <property file="${s-ramp-distro.build.properties}" />
  <property name="s-ramp-distro.tempdir" location=".temp" />
  <property name="s-ramp-distro.rootdir" location="." />
  <property name="tomcat.version" value="7.0.52" />
  <property name="s-ramp-distro.bindir" location="${s-ramp-distro.rootdir}/bin" />
  <property name="s-ramp-distro.overlord-commons-installer.jar" location="${s-ramp-distro.bindir}/overlord-commons-installer-${project.version}.jar" />
  <property name="s-ramp-distro.s-ramp-installer.jar" location="${s-ramp-distro.bindir}/s-ramp-installer-${project.version}.jar" />

  <!-- Install S-RAMP -->
  <target name="install">
     
    <echo message=" " />
    <echo message="#######################################################" />
    <echo message="# Installing S-RAMP.  Please tell us how you would    #" />
    <echo message="# like to proceed!                                    #" />
    <echo message="#######################################################" />
    <echo message=" " />
    <echo message=" Choose from one of the following options." />
    <echo message="    1. Install into JBoss EAP 6.1" />
    <echo message="    2. Install into Tomcat 7" />
    <echo message="    3. Install into Fuse 6.1" />
    <echo message=" " />
    <input message="Choice: " addproperty="s-ramp-distro.choices.platform" />

    <condition property="s-ramp-distro.choices.platform.jboss-eap-6.1">
      <equals arg1="${s-ramp-distro.choices.platform}" arg2="1" />
    </condition>
    <condition property="s-ramp-distro.choices.platform.tomcat-7">
      <equals arg1="${s-ramp-distro.choices.platform}" arg2="2" />
    </condition>
    <condition property="s-ramp-distro.choices.platform.fuse-6.1">
      <equals arg1="${s-ramp-distro.choices.platform}" arg2="3" />
    </condition>

    <antcall target="install-jboss-eap-6.1-wrapper" />
    <antcall target="install-tomcat-7-wrapper" />
    <antcall target="install-fuse-6.1-wrapper" />
  </target>

  <target name="extract-installer">
    <mkdir dir="${s-ramp-distro.tempdir}"/>
    <!-- Unpack the s-ramp installer into the temp directory -->
    <property name="s-ramp-distro.s-ramp-installer.dir" location="${s-ramp-distro.tempdir}/s-ramp-installer" />
    <unzip src="${s-ramp-distro.s-ramp-installer.jar}" dest="${s-ramp-distro.s-ramp-installer.dir}" overwrite="false" />
  </target>

  <!-- Install into EAP 6.1 -->
  <target name="install-jboss-eap-6.1-wrapper" if="s-ramp-distro.choices.platform.jboss-eap-6.1">
    <echo message=" " />
    <echo message="#######################################################" />
    <echo message="# Installing S-RAMP into EAP 6.1 (great choice!).     #" />
    <echo message="#######################################################" />
    <echo message=" " />
    <echo message=" Please tell us where JBoss EAP 6.1 is located (full path)." />
    <echo message=" " />
    <input message="Path to EAP 6.1: " addproperty="s-ramp-distro.choices.platform.jboss-eap-6.1.path" />
    
    <antcall target="install-jboss-eap-6.1" />
  </target>

  <target name="install-jboss-eap-6.1" depends="extract-installer">
    <condition property="s-ramp-distro.choices.platform.jboss-eap-6.1.path.valid">
      <available file="jboss-modules.jar" filepath="${s-ramp-distro.choices.platform.jboss-eap-6.1.path}" />
    </condition>
    <fail message="Failed to find JBoss EAP 6.1 at: ${s-ramp-distro.choices.platform.jboss-eap-6.1.path}" 
              unless="s-ramp-distro.choices.platform.jboss-eap-6.1.path.valid" />
    <echo message="Installing into: ${s-ramp-distro.choices.platform.jboss-eap-6.1.path} " />

    <!-- Call the S-RAMP installer -->
    <ant antfile="${s-ramp-distro.s-ramp-installer.dir}/build.xml" inheritall="true" target="install" 
         dir="${s-ramp-distro.s-ramp-installer.dir}">
      <property name="appserver.id" value="jboss-eap-6.1" />
      <property name="s-ramp.install.dir" location="${s-ramp-distro.tempdir}" />
      <property name="s-ramp.appserver.dir" location="${s-ramp-distro.choices.platform.jboss-eap-6.1.path}" />
      <property name="s-ramp.overlord-commons.installer.jar" location="${s-ramp-distro.bindir}/overlord-commons-installer-${overlord-commons.version}.jar" />
      <property name="overlord-commons.idp.file" value="${s-ramp-distro.bindir}/overlord-commons-idp-eap61-${overlord-commons.version}.war" />
      <property name="overlord-commons.services.file" location="${s-ramp-distro.bindir}/overlord-commons-services-${overlord-commons.version}.jar" />
      <property name="overlord-commons.auth.file" location="${s-ramp-distro.bindir}/overlord-commons-auth-${overlord-commons.version}.jar" />
      <property name="overlord-commons.auth.tomcat7.file" location="${s-ramp-distro.bindir}/overlord-commons-auth-tomcat7-${overlord-commons.version}.jar" />
      <property name="overlord-commons.auth.jboss7.file" location="${s-ramp-distro.bindir}/overlord-commons-auth-jboss7-${overlord-commons.version}.jar" />
      <property name="s-ramp.s-ramp-server.war.path" location="${s-ramp-distro.bindir}/s-ramp-server-eap61-${s-ramp.version}.war" />
      <property name="s-ramp.s-ramp-ui.war.path" location="${s-ramp-distro.bindir}/s-ramp-ui-war-eap61-${s-ramp.version}.war" />
      <property name="s-ramp.modeshape-distribution.zip.path" location="${s-ramp-distro.rootdir}/AS/EAP/modeshape-distribution-${org.modeshape.version}-jbosseap-61-dist.zip" />
    </ant>
  </target>


  <!-- Install into Tomcat 7 -->
  <target name="install-tomcat-7-wrapper" if="s-ramp-distro.choices.platform.tomcat-7">
    <echo message=" " />
    <echo message="########################################" />
    <echo message="# Installing S-RAMP into Tomcat 7.     #" />
    <echo message="########################################" />
    <echo message=" " />
    <echo message=" Please tell us where Tomcat 7 is located (full path).  Simply" />
    <echo message=" leave this blank if you want us to download Tomcat 7 for you." />
    <echo message=" " />
    <input message="Path to Tomcat 7: " addproperty="s-ramp-distro.choices.platform.tomcat-7.path" />
    <antcall target="install-tomcat-7" />
  </target>
  
  <target name="install-tomcat-7" depends="extract-installer">
    <!-- If the path is empty, download Tomcat 7 -->
    <condition property="s-ramp-distro.choices.platform.tomcat-7.path.empty">
      <equals arg1="${s-ramp-distro.choices.platform.tomcat-7.path}" arg2="" />
    </condition>

    <antcall target="install-tomcat-7-into" />
  </target>

  <!-- Download Tomcat 7 for the user -->
  <target name="install-tomcat-7-download" if="s-ramp-distro.choices.platform.tomcat-7.path.empty">
    <property name="s-ramp-distro.tomcat-7.download.url" 
              value="http://archive.apache.org/dist/tomcat/tomcat-7/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.zip" />
    
    <echo message=" " />
    <echo message="########################################" />
    <echo message="# Downloading Tomcat 7, please wait... #" />
    <echo message="########################################" />
    <get src="${s-ramp-distro.tomcat-7.download.url}" dest="${s-ramp-distro.tempdir}" usetimestamp="true" />
    
    <property name="s-ramp-distro.tomcat-7.zip" value="${s-ramp-distro.tempdir}/apache-tomcat-${tomcat.version}.zip" />
    <echo message="########################################" />
    <echo message="# Unzipping Tomcat 7, please wait...   #" />
    <echo message="########################################" />
    <unzip src="${s-ramp-distro.tomcat-7.zip}" dest="${s-ramp-distro.rootdir}" overwrite="false" />
    <property name="s-ramp-distro.choices.platform.tomcat-7.real-path" location="${s-ramp-distro.rootdir}/apache-tomcat-${tomcat.version}" />
  </target>


  <!-- Install into Tomcat 7 -->
  <target name="install-tomcat-7-into" depends="install-tomcat-7-download">
    <!-- Now set the real path and install into it -->
    <property name="s-ramp-distro.choices.platform.tomcat-7.real-path" value="${s-ramp-distro.choices.platform.tomcat-7.path}" />

    <!-- If the path is invalid, fail -->
    <condition property="s-ramp-distro.choices.platform.tomcat-7.path.valid">
      <available file="catalina.sh" filepath="${s-ramp-distro.choices.platform.tomcat-7.real-path}/bin" />
    </condition>
    <fail message="Failed to find Tomcat 7 at: ${s-ramp-distro.choices.platform.tomcat-7.real-path}" 
          unless="s-ramp-distro.choices.platform.tomcat-7.path.valid" />

    <echo message="Installing into: ${s-ramp-distro.choices.platform.tomcat-7.real-path} " />

    <!-- Call the S-RAMP installer -->
    <ant antfile="${s-ramp-distro.s-ramp-installer.dir}/build.xml" inheritall="true" target="install" 
         dir="${s-ramp-distro.s-ramp-installer.dir}">
      <property name="appserver.id" value="tomcat-7" />
      <property name="s-ramp.install.dir" location="${s-ramp-distro.tempdir}" />
      <property name="s-ramp.appserver.dir" location="${s-ramp-distro.choices.platform.tomcat-7.real-path}" />
      <property name="s-ramp.overlord-commons.installer.jar" location="${s-ramp-distro.bindir}/overlord-commons-installer-${overlord-commons.version}.jar" />
      <property name="overlord-commons.idp.file" value="${s-ramp-distro.bindir}/overlord-commons-idp-tomcat7-${overlord-commons.version}.war" />
      <property name="overlord-commons.services.file" location="${s-ramp-distro.bindir}/overlord-commons-services-${overlord-commons.version}.jar" />
      <property name="overlord-commons.auth.file" location="${s-ramp-distro.bindir}/overlord-commons-auth-${overlord-commons.version}.jar" />
      <property name="overlord-commons.auth.tomcat7.file" location="${s-ramp-distro.bindir}/overlord-commons-auth-tomcat7-${overlord-commons.version}.jar" />
      <property name="s-ramp.s-ramp-server.war.path" location="${s-ramp-distro.bindir}/s-ramp-server-tomcat7-${s-ramp.version}.war" />
      <property name="s-ramp.s-ramp-ui.war.path" location="${s-ramp-distro.bindir}/s-ramp-ui-war-tomcat7-${s-ramp.version}.war" />
    </ant>
  </target>
  

  <!-- Install into Fuse 6.1 -->
  <target name="install-fuse-6.1-wrapper" if="s-ramp-distro.choices.platform.fuse-6.1">
    <echo message=" " />
    <echo message="#######################################################" />
    <echo message="# Installing S-RAMP into Fuse 6.1                     #" />
    <echo message="#######################################################" />
    <echo message=" " />
    <echo message=" Please tell us where JBoss Fuse 6.1 is located (full path)." />
    <echo message=" " />
    <input message="Path to Fuse 6.1: " addproperty="s-ramp-distro.choices.platform.fuse-6.1.path" />

    <antcall target="install-fuse-6.1" />

    <echo message=" ******************************************" />
    <echo message=" * Installation Completed Successfully    *" />
    <echo message=" ******************************************" />
    <echo message=" *                                        *" />
    <echo message=" * Next you must start up Fuse 6.1        *" />
    <echo message=" * and execute the following command:     *" />
    <echo message="                                           " />
    <echo message="   > features:addurl mvn:org.overlord.sramp/s-ramp-distro-fuse61/${project.version}/xml/features"/>
    <echo message="   > features:install -v s-ramp            " />
    <echo message="                                           " />
    <echo message=" ******************************************" />
  </target>

  <target name="install-fuse-6.1" depends="extract-installer">
    <condition property="s-ramp-distro.choices.platform.fuse-6.1.path.valid">
      <available file="fuse-bai.xml" filepath="${s-ramp-distro.choices.platform.fuse-6.1.path}/etc" />
    </condition>
    <fail message="Failed to find JBoss Fuse 6.1 at: ${s-ramp-distro.choices.platform.fuse-6.1.path}" 
              unless="s-ramp-distro.choices.platform.fuse-6.1.path.valid" />
    <echo message="Installing into: ${s-ramp-distro.choices.platform.fuse-6.1.path} " />

    <!-- Call the S-RAMP installer -->
    <ant antfile="${s-ramp-distro.s-ramp-installer.dir}/build.xml" inheritall="true" target="install" 
         dir="${s-ramp-distro.s-ramp-installer.dir}">
      <property name="appserver.id" value="fuse-6.1" />
      <property name="s-ramp.install.dir" location="${s-ramp-distro.tempdir}" />
      <property name="s-ramp.appserver.dir" location="${s-ramp-distro.choices.platform.fuse-6.1.path}" />
      <property name="s-ramp.overlord-commons.installer.jar" location="${s-ramp-distro.bindir}/overlord-commons-installer-${overlord-commons.version}.jar" />
    </ant>
  </target>

</project>
