<project name="ShARPE" default="install" basedir=".">
    <dirname property="sharpe.root" file="${ant.file.ShARPE}"/>
    <property name="sharpe.lib" value="${sharpe.root}/sharpe-lib"/>
    <property name="sharpe.conf" value="${sharpe.root}/sharpe-conf"/>
    <property name="sharpe.custom" value="${sharpe.root}/custom"/>
    <property name="sharpe.web.config" value="${sharpe.root}/sharpe-webAppConfig"/>
    <property name="sharpe.temp" value="${sharpe.root}/temp"/>
    <property name="metainf" value="${sharpe.web.config}/meta-infs"/>
    <property name="temp.metainf" value="${sharpe.root}/temp/meta-infs"/>
    <property name="sharpe.metainf" value="${temp.metainf}/sharpe-metainf"/>
    <property name="autograph.metainf" value="${temp.metainf}/autograph-metainf"/>
    <property name="spdescription.metainf" value="${temp.metainf}/spde-metainf"/>

    <!-- antcontrib tasks -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${sharpe.lib}/ant-contrib.jar"/>

    <!-- include the properties irrespective whatever task executed -->
    <if>
        <not> <available file="${sharpe.root}/build.properties" /></not>
        <then>
            <copy file="${sharpe.conf}/default.build.properties" tofile="${sharpe.root}/build.properties" />
        </then>
    </if>
    <property file="${sharpe.root}/build.properties" />

    <!-- check if shib.src is given -->
    <if><not><isset property="shib.src"/></not>
        <then>
        <!-- <fail message="shib.src is not defined. Please supply this in the build.properties file or with -Dshib.src= option."/> -->
        <input message="Please enter SHIB_SRC location: " addproperty="shib.src"/>
	      <propertyfile file="build.properties" comment="for full detail meanings of each entry, please check conf/default.build.properties">
	         <entry key="shib.src" value="${shib.src}" />
	      </propertyfile>
        </then>
    </if>
    
    <!-- just double check -->
    <if><not>
    		<and>
    			<isset property="shib.src"/>
    			<available file="${shib.src}"/>
        		<available file="${shib.src}/build.xml"/>
         </and>
        </not>
        <then>
        		<fail message="${shib.src} is not valid. Quitting..."/>
        </then>
    </if>
      
    <!-- ensuring force-deployment has its default value -->
    <if><not><isset property="force-deployment"/></not>
        <then><var name="force-deployment" value="true"/></then>
    </if>

    <property name="shib.dist" value="${shib.src}/dist"/>
    <property name="shib.lib" value="${shib.src}/lib"/>
    <property name="shib.custom" value="${shib.src}/custom"/>
    <property name="shib.custom.lib" value="${shib.custom}/lib"/>
    <property name="shib.web" value="${shib.src}/webApplication"/>
    <property name="shib.web.lib" value="${shib.src}/webApplication/WEB-INF/lib"/>
    <property name="shib.build" value="${shib.src}/webApplication/WEB-INF/classes/"/>

    <!-- prepare ShARPE working space -->
    <target name="-init">
        <if><not><available file="${sharpe.temp}"/></not>
            <then>
                <mkdir dir="${sharpe.temp}"/>
            </then>
        </if>
    </target>
   
    <!-- remove ShARPE working space -->
    <target name="-uninit">
        <delete quiet="true" dir="${sharpe.temp}"/>
    </target>

    <!-- backup important files -->
    <target name="-backup" depends="-load-shib-properties">
        <if><available file="${idp.home}"/>
        <then>
           <if><not><isset property="sharpe.backup"/></not>
               <then><var name="sharpe.backup" value="${idp.home}/etc/sharpe-backup"/></then>
           </if>
           <if><not><isset property="force.backup.overwrite"/></not>
               <then><var name="force.backup.overwrite" value="false"/></then>
           </if>

           <mkdir dir="${sharpe.backup}"/>

           <copy todir="${sharpe.backup}" includeEmptyDirs="true" failonerror="false" overwrite="${force.backup.overwrite}">
               <fileset dir="${idp.home}/etc">
                   <include name="idp.xml"/>
                   <include name="*resolver*"/>
                   <include name="arps/**/*"/>
                   <include name="mams-*/**/*"/>
               </fileset>
               <fileset file="${resolver.path}"/> <!-- should be ok if undefined? -->
           </copy>
           <!-- put comments -->
           <echo file="${sharpe.backup}/readme.txt" append="false">This folder contains ShARPE's backup files, please only modify
           it through ShARPE's install script</echo>
        </then>
        </if>
    </target>

    <target name="-load-shib-properties-nocompile" unless="idp.home">
        <if>
            <available file="${shib.src}/build.properties"/>
            <then>
                <property file="${shib.src}/build.properties"/>
            </then>
            <else>
                <fail>No build.properties found in ${shib.src}</fail>
            </else>
        </if>
    </target>
    
    <!-- load necessary properties from Shib -->
    <target name="-load-shib-properties" depends="-load-shib-properties-nocompile" unless="idp.home.url">
        <if>
            <not><available classname="edu.internet2.middleware.shibboleth.utils.ant.URLConvert" classpathref="classpath"/></not>
            <then>
                <antcall target="-compile"/>
            </then>
        </if>
        <taskdef name="urlconvert" classname="edu.internet2.middleware.shibboleth.utils.ant.URLConvert">
            <classpath refid="classpath"/>
        </taskdef>
        <urlconvert path="${idp.home}" addProperty="idp.home.url"/>
    </target>
    <path id="classpath">
        <pathelement location="${shib.build}"/>
        <fileset dir="${sharpe.lib}">
            <include name="**/*.jar"/>
        </fileset>
    </path>

    <target name="-copy-noconfig-files" unless="noconfig.fully.copied" depends="-init, -copy-to-shib-src">
        <var name="noconfig.fully.copied" value="true"/>
    </target>

    <!-- not used -->
    <target name="-copy" unless="fully.copied" depends="-copy-noconfig-files, -copy-to-temp">
        <var name="fully.copied" value="true"/>
    </target>
    
    <target name="-copy-to-temp" unless="temp.fully.copied" depends="-load-shib-properties">
        <!-- copy the web xmls -->
        <copy todir="${sharpe.temp}">
            <fileset dir="${sharpe.web.config}">
                <include name="**"/>
            </fileset>
        </copy>

        <!-- copy this here so the replacement patterns below can apply -->
        <if><available file="${sharpe.conf}/sharpe-conf.include"/>
            <then>
                <copy todir="${sharpe.temp}">
                    <fileset dir="${sharpe.conf}" includesfile="${sharpe.conf}/sharpe-conf.include"/>
                </copy>
            </then>
        </if>

        <if>
            <and>
                <not><isset property="sharpe.token.value.output"/></not>
                <isset property="sharpe.token.value"/>
            </and>
            <then>
                <var name="sharpe.token.value.output" value="${sharpe.token.value}"/>
            </then>
        </if>
        
        <replace dir="${sharpe.temp}" token="$IDP_HOME$" value="${idp.home.url}" />
        <replace dir="${sharpe.temp}" token="$IDP_WEBAPP_NAME$" value="${idp.webapp.name}"/>
        <replace dir="${sharpe.temp}" token="$SHARPE_WEBAPP_NAME$" value="${sharpe.webapp.name}"/>
        <replace dir="${sharpe.temp}" token="$AUTOGRAPH_WEBAPP_NAME$" value="${autograph.webapp.name}"/>
        <replace dir="${sharpe.temp}" token="$SPDE_WEBAPP_NAME$" value="${spdescription.webapp.name}"/>
        <replace dir="${sharpe.temp}" token="${sharpe.token.replace}" value="${sharpe.token.value.output}"/>    
        
        <var name="temp.fully.copied" value="true"/>
    </target>
    <target name="-copy-to-shib-src">
        <echo>--- Copy required files/libraries to Shib</echo>
        <!-- copy all the required libs -->
        <if>
            <available file="${shib.lib}"/>
            <then>
                <copy todir="${shib.lib}" overwrite="true" preservelastmodified="true">
                    <fileset dir="${sharpe.lib}"/>
                </copy>
            </then>
            <else>
                <fail message="${shib.src} doesn't have lib folder (${shib.lib})."/>
            </else>
        </if>
        <!-- replace old jars explicitly, old version doesnt support struts 1.2 -->
        <if>
            <and>
                <available file="${shib.web.lib}/struts.jar"/>
                <not><available file="${shib.web.lib}/struts.jar.old"/></not>
            </and>
            <then>
                <if><available file="${sharpe.lib}/struts.jar"/>
                    <then>
                        <!-- should be a 'move' but a copy is safer -->
                        <copy failonerror="false" file="${shib.web.lib}/struts.jar" tofile="${shib.web.lib}/struts.jar.old" overwrite="true" preservelastmodified="true"/>
                        <copy failonerror="false" file="${shib.web.lib}/commons-digester.jar" tofile="${shib.web.lib}/commons-digester.jar.old" overwrite="false" preservelastmodified="true"/>
                        <copy failonerror="false" file="${shib.web.lib}/commons-beanutils.jar" tofile="${shib.web.lib}/commons-beanutils.jar.old" overwrite="false" preservelastmodified="true"/>

                        <copy file="${sharpe.lib}/struts.jar" tofile="${shib.web.lib}/struts.jar" overwrite="true" preservelastmodified="true"/>
                        <copy file="${sharpe.lib}/commons-digester.jar" tofile="${shib.web.lib}/commons-digester.jar" overwrite="true" preservelastmodified="true"/>
                        <copy file="${sharpe.lib}/commons-beanutils.jar" tofile="${shib.web.lib}/commons-beanutils.jar" overwrite="true" preservelastmodified="true"/>
                    </then>
                </if>
            </then>
        </if>
        
        <!-- copy all the extensions -->
        <antcall target="-apply-extension">
            <param name="target-name" value="-copy-extension" />
        </antcall>
    </target>

    <!-- copy ShARPE's modules to Shib extension -->
    <target name="-copy-extension">
        <basename property="extension-path-name" file="${extension-path}"/>
        <if>
            <and>
                <available file="${shib.custom}/${extension-path-name}"/>
                <or>
                    <not><isset property="force.delete.copy"/></not>
                    <istrue value="${force.delete.copy}"/>
                </or>
            </and>
            <then>
                <delete dir="${shib.custom}/${extension-path-name}" quiet="true" failonerror="false"/>
            </then>
        </if>
        <copy todir="${shib.custom}/${extension-path-name}" overwrite="false" includeEmptyDirs="true" preservelastmodified="true" failonerror="false">
            <fileset dir="${extension-path}"/>
        </copy>
        <property file="${shib.custom}/${extension-path-name}/build.properties"/>

        <if>
            <and>
                <isset property="ext.value.replace"/>
                <isset property="ext.value"/>
            </and>
            <then>
                <replace dir="${shib.custom}/${extension-path-name}" token="${ext.value.replace}" value="${ext.value}">
                    <include name="**/*"/>
                    <exclude name="${shib.custom}/${extension-path-name}/build.properties"/>
                    <exclude name="**/*.java"/>
                </replace>
            </then>
        </if>
    </target>

    <!-- remove ShARPE's modules from shib.src extension folder -->
    <target name="-remove-extension">
        <basename property="extension-path-name" file="${extension-path}"/>
        <delete dir="${shib.custom}/${extension-path-name}" quiet="true" failonerror="false"/>
        <if><istrue value="${force.remove.home}"/>
            <then>
                <delete dir="${idp.home}/etc/${extension-path-name}" quiet="true" failonerror="false"/>
            </then>
        </if>
    </target>

    <!-- apply task to each of ShARPE's module -->
    <target name="-apply-extension">
        <fail unless="target-name">Must provide "target-name" parameter</fail>
        <for keepgoing="true" param="module-path">
            <dirset dir="${sharpe.custom}">
                <include name="*" />
                <exclude name="lib" />
                <exclude name="web" />
            </dirset>
            <sequential>
                <antcall target="${target-name}">
                    <param name="extension-path" value="@{module-path}" />
                </antcall>
            </sequential>
        </for>
    </target>

    <target name="-replace-idp-descriptor">
        <if><not><available file="${shib.src}/webAppConfig/dist.idp.xml.old"/></not>
            <then>
                <copy file="${shib.src}/webAppConfig/dist.idp.xml" tofile="${shib.src}/webAppConfig/dist.idp.xml.old" overwrite="true" preservelastmodified="true"/>
                <copy file="${sharpe.web.config}/dist.idp.xml" todir="${shib.src}/webAppConfig" overwrite="true" preservelastmodified="true"/>
            </then>
        </if>
    </target>
    <target name="-restore-idp-descriptor">
        <if><available file="${shib.src}/webAppConfig/dist.idp.xml.old"/>
            <then>
                <copy file="${shib.src}/webAppConfig/dist.idp.xml.old" tofile="${shib.src}/webAppConfig/dist.idp.xml" overwrite="true" preservelastmodified="true"/>
                <delete quiet="true" file="${shib.src}/webAppConfig/dist.idp.xml.old"/>
            </then>
        </if>
    </target>

    <!-- execute a Shib task -->
    <target name="-call-shib-script">
        <fail unless="procedure">Must provide "procedure" parameter</fail>
        <echo>--- Executing "${procedure}" task in Shib</echo>

        <!-- just a little cleverer, compile first in case it wasn't compiled yet -->
        <if>
            <not>
                <or>
                    <equals arg1="${procedure}" arg2="compile"/>
                    <equals arg1="${procedure}" arg2="clean"/>
                    <equals arg1="${procedure}" arg2="clean-install"/>
                    <available classname="edu.internet2.middleware.shibboleth.utils.ant.URLConvert">
                        <classpath><pathelement location="${shib.build}"/></classpath>
                    </available>
                </or>
            </not>
            <then>
                <antcall target="compile"/>
            </then>
        </if>
      
        <ant dir="${shib.src}" target="${procedure}" inheritall="false" inheritrefs="false"/>
    </target>

    <target name="compile" depends="-copy-noconfig-files, -replace-idp-descriptor, -compile"/>
   
    <target name="-compile">
        <antcall target="-call-shib-script">
            <param name="procedure" value="compile"/>
        </antcall>
    </target>

    <target name="package" depends="-ask-question, -copy, -replace-idp-descriptor" description="Package ShARPE into Shib">
        <!-- package idp first -->
        <if>
            <or>
                <not><isset property="ignore.call.shib.package"/></not>
                <not><istrue value="${ignore.call.shib.package}"/></not>
            </or>
            <then>
                <antcall target="-call-shib-script">
                    <param name="procedure" value="package-idp"/>
                </antcall>
            </then>
        </if>
        <antcall target="-package"/>
    </target>

    <!-- package each ShARPE's module to Shib -->
    <target name="-package" depends="-load-shib-properties">
        <echo>--- Package ShARPE webapps</echo>
        <antcall target="-package-war">
            <param name="app.metainf" value="${sharpe.metainf}"/>
            <param name="app.webapp.name" value="${sharpe.webapp.name}"/>
            <param name="app.deployment.descriptor" value="${sharpe.deployment.descriptor}"/>
        </antcall>
        <antcall target="-package-war">
            <param name="app.metainf" value="${spdescription.metainf}"/>
            <param name="app.webapp.name" value="${spdescription.webapp.name}"/>
            <param name="app.deployment.descriptor" value="${spdescription.deployment.descriptor}"/>
        </antcall>
    </target>
    <!-- package a ShARPE's module -->
    <target name="-package-war">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <war destfile="${shib.dist}/${app.webapp.name}.war" webxml="${sharpe.temp}/${app.deployment.descriptor}" update="no" duplicate="preserve">
            <fileset dir="${app.metainf}">
                <include name="**" />
            </fileset>

            <!-- make sure the .include has something in it, otherwise the whole thing is copied over -->         
            <fileset dir="${shib.custom}/web" includesfile="${sharpe.conf}/${app.webapp.name}-web.include"/>

            <!-- since we're putting on different webapps, we need to package the web files too -->
            <fileset dir="${shib.custom}/web/${app.webapp.name}">
                <include name="**/*"/>
            </fileset>
         
            <!-- dont forget shib web-inf -->
            <fileset dir="${shib.web}">
                <include name="WEB-INF/**/*"/>
            </fileset>
         
            <lib dir="${shib.custom.lib}"/>
            <lib dir="${sharpe.lib}"/>
        </war>
        <echo>--- Finish constructing ${app.webapp.name}.war</echo>
    </target>
    <target name="-copy-conf">
        <if><available file="${sharpe.conf}/sharpe-conf.include"/>
            <then>
                <copy todir="${idp.home}/etc">
                    <fileset dir="${sharpe.temp}" includesfile="${sharpe.conf}/sharpe-conf.include"/>
                </copy>
            </then>
        </if>
    </target>

    <!-- Installation tasks -->

    <target name="-install-idp-only">
        <echo>--- Installing IdP</echo>
        <antcall target="-call-shib-script">
            <param name="procedure" value="install"/>
        </antcall>
    </target>

    <target name="install" depends="-ask-question, -copy-noconfig-files, -replace-idp-descriptor, -backup" description="Install ShARPE">
        <echo>--- Installation of ShARPE started</echo>

        <antcall target="-install-idp-only"/>
        
        <antcall target="-copy-to-temp" inheritall="false">
            <param name="sharpe.temp" value="${sharpe.temp}"/>
            <param name="sharpe.web.config" value="${sharpe.web.config}"/>
            <param name="sharpe.conf" value="${sharpe.conf}"/>
            <param name="sharpe.token.replace" value="${sharpe.token.replace}"/>
            <param name="sharpe.token.value.output" value="${sharpe.token.value.output}"/>
            <param name="sharpe.webapp.name" value="${sharpe.webapp.name}"/>
            <param name="autograph.webapp.name" value="${autograph.webapp.name}"/>
            <param name="spdescription.webapp.name" value="${spdescription.webapp.name}"/>
        </antcall>
      
        <antcall target="-package">
            <param name="ignore.call.shib.package" value="true"/>
        </antcall>
      
        <antcall target="-install" inheritall="false">
            <param name="force-deployment" value="${force-deployment}"/>
            <param name="shib.dist" value="${shib.dist}"/>
        </antcall>

        <antcall target="-copy-conf"/>

        <!-- just remove the temp folder -->      
        <antcall target="-uninit"/>

        <antcall target="-display-message-install"/>
    </target>

    <!-- install each of ShARPE's module -->
    <target name="-install" depends="-load-shib-properties">
        <echo>--- Installation of ShARPE's modules started</echo>
        <antcall target="-install-war">
            <param name="app.webapp.name" value="${sharpe.webapp.name}"/>
        </antcall>
        <antcall target="-install-war">
            <param name="app.webapp.name" value="${autograph.webapp.name}"/>
        </antcall>
        <antcall target="-install-war">
            <param name="app.webapp.name" value="${spdescription.webapp.name}"/>
        </antcall>
    </target>

    <target name="-display-message-uninstall">
        <echo>Please make sure that you re-configure idp.xml according</echo>
        <echo>to the uninstallation guide of ShARPE. You will need to restart</echo>
        <echo>your tomcat to fully re-deploy the IdP. Refer to </echo>
        <echo>http://federation.org.au/ShARPE for details of ShARPE's </echo>
        <echo>uninstallation and troubleshootings at http://federation.org.au/ShARPE.</echo>
    </target>

    <target name="-display-message-install">
        <echo>Please make sure that you configure idp.xml according to</echo>
        <echo>the installation guide of ShARPE. You will need to restart your</echo>
        <echo>tomcat to fully deploy the applications. ShARPE, Autograph, and</echo>
        <echo>SPDescription can be accessed through http://xxx/ShARPE, </echo>
        <echo>http://xxx/Autograph and http://xxx/SPDescription respectively.</echo>
        <echo></echo>
        <echo>Refer to http://federation.org.au/ShARPE for details of</echo>
        <echo>ShARPE's functionalities and troubleshootings.</echo>
    </target>

    <!-- install one of ShARPE's module -->
    <target name="-install-war">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <echo>--- Deploying ${app.webapp.name}</echo>
        <if>
            <and>
                <istrue value="${force-deployment}"/>
                <available file="${tomcat.home}/webapps/${app.webapp.name}"/>
            </and>
            <then>
                <echo>--- Forcing undeployment of ${app.webapp.name} first before installing a fresh copy</echo>
                <antcall target="-undeploy-war"/>
            </then>
        </if>
        <antcall target="-install-war-${idp.install-type}"/>
    </target>

    <!-- restore important files -->
    <target name="-restore" depends="-load-shib-properties, -restore-idp-descriptor">
        <if><available file="${idp.home}/etc/sharpe-backup"/>
            <then>
                <if><xinput type="confirm" defaultvalue="y">Do you want to keep the existing ShARPE configurations (to be stored at ${idp.home}/etc/sharpe-backup-2)?</xinput>
                    <then><antcall target="-backup">
                        <param name="sharpe.backup" value="${idp.home}/etc/sharpe-backup-2"/>
                        <param name="force.backup.overwrite" value="true"/>
                    </antcall></then>
                </if>
            
                <var name="sharpe.backup" value="${idp.home}/etc/sharpe-backup"/>

                <!-- wipe off the arps first, since it may have more contents than original backup -->
                <delete>
                    <fileset dir="${idp.home}/etc/arps" includes="**"/>
                </delete>
            
                <copy todir="${idp.home}/etc" includeEmptyDirs="true" failonerror="false" overwrite="true">
                    <fileset dir="${sharpe.backup}" includes="**/*"/>
                </copy>
            </then>
            <else>
                <echo>No backup files recorded, restore failed.</echo>
            </else>
        </if>
    </target>

    <target name="uninstall" depends="-install.init, -restore" description="Uninstall ShARPE and restore IdP">
        <!-- remove all ShARPE modules from shib.home -->
        <var name="force.remove.home" value="true"/>
        <if><xinput type="confirm" defaultvalue="y">Do you want to re-install the IdP webapp (removing cleanly all ShARPE's libraries)?</xinput>
            <then>
                <var name="keep.idp.webapp" value="false"/>
            </then>
            <else>
                <var name="keep.idp.webapp" value="true"/>
            </else>
        </if>
        <antcall target="-uninstall"/>
      
        <!-- since we don't want to keep the webapp, we better re-install the webapp -->
        <if><not><istrue value="${keep.idp.webapp}"/></not>
            <then>
                <antcall target="-install-idp-only"/>
            </then>
        </if>
      
        <antcall target="-display-message-uninstall"/>
    </target>

    <!-- uninstall each of ShARPE's module -->
    <target name="-uninstall">
        <echo>--- Removing ShARPE modules</echo>
 
        <!-- only remove the idp webapp when either keep.idp.webapp is not set or it is set to false -->
        <if>
            <or>
                <not><isset property="keep.idp.webapp"/></not>
                <not><istrue value="${keep.idp.webapp}"/></not>
            </or>
            <then>
                <antcall target="-undeploy-war">
                    <param name="app.webapp.name" value="${idp.webapp.name}"/>
                </antcall>
            </then>
        </if>
      
        <!-- remove the rest of ShARPE -->
        <antcall target="-undeploy-war">
            <param name="app.webapp.name" value="${sharpe.webapp.name}"/>
        </antcall>
        <antcall target="-undeploy-war">
            <param name="app.webapp.name" value="${autograph.webapp.name}"/>
        </antcall>
        <antcall target="-undeploy-war">
            <param name="app.webapp.name" value="${spdescription.webapp.name}"/>
        </antcall>
    </target>
    
    <!-- uninstall a ShARPE's module war -->
    <target name="-undeploy-war" depends="-load-shib-properties-nocompile">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <echo>--- Undeploying ${app.webapp.name}</echo>
        <antcall target="-undeploy-war-${idp.install-type}"/>
    </target>
   
    <target name="-stop-start-manager">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <antcall target="-tomcat.stop"/>
        <antcall target="-tomcat.start"/>
    </target>

    <!-- remove a ShARPE's module directly from filesystem -->
    <target name="-undeploy-war-filesystem.prompt">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <delete file="${tomcat.home}/webapps/${app.webapp.name}.war" failonerror="false"/>
        <delete dir="${tomcat.home}/webapps/${app.webapp.name}" failonerror="false"/>
    </target>
   
    <!-- remove a ShARPE's module via Tomcat manager -->
    <target name="-undeploy-war-manager">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <antcall target="-tomcat.undeploy"/>
    </target>

    <!-- copy ShARPE's module war to tomcat -->
    <target name="-install-war-filesystem.prompt">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <copy file="${shib.dist}/${app.webapp.name}.war" tofile="${tomcat.home}/webapps/${app.webapp.name}.war" failonerror="false"/>
    </target>

    <!-- install ShARPE's module war via Tomcat manager -->
    <target name="-install-war-manager">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <antcall target="-tomcat.deploy"/>
    </target>

    <!-- Tomcat Management -->
    <!-- setup Tomcat's tasks -->
    <target name="-setupTomcatTasks" unless="tomcat.defined" depends="-load-shib-properties-nocompile">
        <path id="tomcat.classpath">
            <fileset dir="${tomcat.home}/server/lib" includes="catalina-ant.jar"/>
        </path>

        <taskdef name="tomcatStart" classname="org.apache.catalina.ant.StartTask" classpathref="tomcat.classpath"/>
        <taskdef name="tomcatStop" classname="org.apache.catalina.ant.StopTask" classpathref="tomcat.classpath"/>
        <taskdef name="tomcatDeploy" classname="org.apache.catalina.ant.DeployTask" classpathref="tomcat.classpath"/>
        <taskdef name="tomcatUndeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="tomcat.classpath"/>
        
        <var name="tomcat.defined" value="true"/>
    </target>

    <!-- start a deployed webapp using Tomcat manager -->
    <target name="-tomcat.start" depends="-setupTomcatTasks">
        <tomcatStart url="${tomcat.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${app.webapp.name}"/>
    </target>

    <!-- stop a deployed webapp using Tomcat manager -->
    <target name="-tomcat.stop" depends="-setupTomcatTasks">
        <tomcatStop url="${tomcat.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${app.webapp.name}"/>
    </target>

    <!-- upload and deploy using Tomcat manager -->
    <target name="-tomcat.deploy" depends="-setupTomcatTasks">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <tomcatDeploy url="${tomcat.url}" username="${tomcat.username}" password="${tomcat.password}"
        war="file:${shib.dist}/${app.webapp.name}.war"/>
    </target>

    <!-- undeploy using Tomcat manager -->
    <target name="-tomcat.undeploy" depends="-setupTomcatTasks">
        <fail unless="app.webapp.name">Must provide "app.webapp.name" parameter</fail>
        <tomcatUndeploy url="${tomcat.url}" username="${tomcat.username}" password="${tomcat.password}" path="/${app.webapp.name}"/>
    </target>

    <target name="clean" depends="-uninit" description="Remove all compiled classes">
        <antcall target="-call-shib-script">
            <param name="procedure" value="clean"/>
        </antcall>
    </target>
    
    <target name="-restore-copied-files">
        <!-- we can leave the jar in the shib.lib, it clutters but we don't know if other app uses it or not -->
        <!-- restore old jars -->
        <if>
            <and>
                <available file="${shib.web.lib}/struts.jar.old"/>
                <available file="${shib.web.lib}/struts.jar"/>
            </and>
            <then>
                <delete file="${shib.web.lib}/struts.jar" failonerror="false"/>
                <delete file="${shib.web.lib}/commons-digester.jar" failonerror="false"/>
                <delete file="${shib.web.lib}/commons-beanutils.jar" failonerror="false"/>
                <move failonerror="false" file="${shib.web.lib}/struts.jar.old" tofile="${shib.web.lib}/struts.jar" overwrite="true" preservelastmodified="true"/>
                <move failonerror="false" file="${shib.web.lib}/commons-digester.jar.old" tofile="${shib.web.lib}/commons-digester.jar" overwrite="false" preservelastmodified="true"/>
                <move failonerror="false" file="${shib.web.lib}/commons-beanutils.jar.old" tofile="${shib.web.lib}/commons-beanutils.jar" overwrite="false" preservelastmodified="true"/>
            </then>
        </if>    
    </target>
    
    <target name="clean-all" depends="clean, clean-source" description="Clean shib.src from ShARPE source/compiled files"/>
    
    <target name="clean-source" depends="-restore-copied-files" description="Remove ShARPE source files from ${shib.src}">
        <antcall target="-apply-extension">
            <param name="target-name" value="-remove-extension" />
        </antcall>
    </target>

    <target name="clean-install" depends="-load-shib-properties-nocompile" description="Wipe off all idp/sp installations">
        <echo>--- Removing all traces of installations both IdP and SP</echo>
        <antcall target="-call-shib-script">
            <param name="procedure" value="clean-install"/>
        </antcall>
      
        <!-- now remove all the webapps for ShARPE too -->
        <delete file="${tomcat.home}/webapps/${idp.webapp.name}.war" failonerror="false"/>
        <delete file="${tomcat.home}/webapps/${sharpe.webapp.name}.war" failonerror="false"/>
        <delete dir="${tomcat.home}/webapps/${sharpe.webapp.name}" failonerror="false"/>
        <delete file="${tomcat.home}/webapps/${autograph.webapp.name}.war" failonerror="false"/>
        <delete dir="${tomcat.home}/webapps/${autograph.webapp.name}" failonerror="false"/>
        <delete file="${tomcat.home}/webapps/${spdescription.webapp.name}.war" failonerror="false"/>
        <delete dir="${tomcat.home}/webapps/${spdescription.webapp.name}" failonerror="false"/>
    </target>
    
    <!-- initialize task definition -->
    <target name="-install.init">
        <if>
            <not><available classname="edu.internet2.middleware.shibboleth.utils.ant.XInput" classpathref="classpath"/></not>
            <then>
                <antcall target="compile"/>
            </then>
        </if>
        <taskdef name="xinput" classname="edu.internet2.middleware.shibboleth.utils.ant.XInput">
            <classpath refid="classpath"/>
        </taskdef>
        <taskdef name="sharpegen" classname="au.edu.mq.melcoe.mams.common.GenerateAdminPassword">
            <classpath refid="classpath"/>
        </taskdef>
    </target>

    <target name="generate-admin-password" description="Generate a new admin password to be used manually to change web.xml">
        <input message="Please enter password required to access ShARPE: [default: ${sharpe.token.value}]" 
        addproperty="sharpe.token.value.input" defaultValue="${sharpe.token.value}" />
        <sharpegen input="${sharpe.token.value.input}" addproperty="sharpe.token.value.output"/>
        <var name="sharpe.token.value" value="${sharpe.token.value.output}" />
        <echo>You need to edit Autograph's web.xml and ShARPE's web.xml manually.</echo>
        <echo>Please search for 'adminPassword' and replace its value with:</echo>
        <echo>${sharpe.token.value}</echo>
    </target>


    <!-- enable or disable mapping and replacement for adminPassword -->
    <target name="-ask-question" unless="sharpe.token.value.output" depends="-install.init">
        <if><xinput type="confirm" defaultvalue="n">Do you want to activate Attribute Mapping on ShARPE?</xinput>
            <then>
                <var name="ext.value" value="true"/>
            </then>
            <else>
                <var name="ext.value" value="false"/>
            </else>
        </if>
        <input message="Please enter password required to access ShARPE: [default: ${sharpe.token.value}]" 
        addproperty="sharpe.token.value.input" defaultValue="${sharpe.token.value}" />
        <sharpegen input="${sharpe.token.value.input}" addproperty="sharpe.token.value.output"/>
        <!--        <var name="sharpe.token.value" value="${sharpe.token.value.output}" /> -->
    </target>
</project>
