<?xml version="1.0" encoding="UTF-8"?>
<project name="Shibboleth Identity Provider" basedir="../../.." default="install">

    <property name="installer.dir" value="${basedir}/src/installer"/>
    <property name="resources.dir" value="${installer.dir}/resources"/>
    <property name="tools.dir" value="${basedir}/src/tools"/>
    <property name="webapp.dir" value="${basedir}/src/main/webapp"/>
    <property name="war.name" value="idp"/>
	
    <!-- Load property from the environment -->
    <property environment="env"/>

    <!-- Installation specific property file -->
    <property file="${resources.dir}/install.properties"/>
	
    <!-- Load ant-contrib tasks -->
    <taskdef resource="net/sf/antcontrib/antlib.xml"/>

    <!-- Load Internet2 ant extensions -->
    <taskdef resource="edu/internet2/middleware/ant/antlib.xml"/>

    <target name="install" description="Installs the identity provider software.">
    	
    	<echo message="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"/>
    	<echo message="Be sure you have read the installation/upgrade instructions on the Shibboleth website before proceeding."/>
    	<echo message="!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"/>
        
        <input message="Where should the Shibboleth Identity Provider software be installed?" addproperty="idp.home.input" defaultvalue="${idp.home}"/>
        <var name="idp.home" value="${idp.home.input}"/>

        <pathToAbsolutePath path="${idp.home}" addproperty="idp.home.path"/>
        <pathToUrl path="${idp.home}" addproperty="idp.home.url"/>
        
        <if>
            <available file="${idp.home.path}" property="idp.home.exists"/>
            <then>
                <input message="The directory '${idp.home.path}' already exists.  Would you like to overwrite this Shibboleth configuration?" addproperty="install.config" validargs="yes,no" defaultvalue="no"/>
            </then>
            <else>
                <var name="install.config" value="yes"/>
            </else>
        </if>

        <if>
            <equals arg1="${install.config}" arg2="yes"/>
            <then>
                <input message="What is the fully qualified hostname of the Shibboleth Identity Provider server?" addproperty="idp.hostname.input" defaultvalue="${idp.hostname}"/>
                <var name="idp.hostname" value="${idp.hostname.input}"/>

                <input message="A keystore is about to be generated for you. Please enter a password that will be used to protect it." addproperty="idp.keystore.pass"/>

                <propertyfile file="${resources.dir}/install.properties">
                    <entry key="idp.home" value="${idp.home.input}"/>
                    <entry key="idp.hostname" value="${idp.hostname.input}"/>
                </propertyfile>

                <mkdir dir="${idp.home.path}"/>
                <mkdir dir="${idp.home.path}/bin"/>
                <mkdir dir="${idp.home.path}/conf"/>
                <mkdir dir="${idp.home.path}/credentials"/>
                <mkdir dir="${idp.home.path}/lib"/>
                <mkdir dir="${idp.home.path}/lib/endorsed"/>
                <mkdir dir="${idp.home.path}/logs"/>
                <mkdir dir="${idp.home.path}/metadata"/>
                <mkdir dir="${idp.home.path}/war"/>

                <var name="idp.entity.id" value="https://${idp.hostname}/idp/shibboleth"/>
                <var name="idp.web.xml.name" value="${webapp.dir}/WEB-INF/web.xml"/>

                <regexSplit input="${idp.hostname}" regex="^.*\.(.*\..*$)" addproperty="idp.scope"/>

                <echo message="Generating signing and encryption key, certificate, and keystore. "/>
            	
            	<if>
            	    <isset property="env.IdPCertLifetime"/>
            		<then>
            			<var name="idp.cert.lifetime" value="${env.IdPCertLifetime}"/>
            		</then>
            		<else>
            			<var name="idp.cert.lifetime" value="20"/>
            		</else>
            	</if>
            		
                <selfSignedCert hostname="${idp.hostname}" uriSubjectAltNames="${idp.entity.id}" certificateLifetime="${idp.cert.lifetime}"
                	            privateKeyFile="${idp.home.path}/credentials/idp.key" certificateFile="${idp.home.path}/credentials/idp.crt" 
                	            keystoreFile="${idp.home.path}/credentials/idp.jks" keystorePassword="${idp.keystore.pass}" />

                <copy todir="${idp.home.path}/bin" preservelastmodified="true" overwrite="true">
                    <fileset dir="${tools.dir}/bash"/>
                    <fileset dir="${tools.dir}/bat"/>
                    <filterset begintoken="$" endtoken="$">
                        <filter token="IDP_HOME" value="${idp.home.path}"/>
                        <filter token="IDP_VERSION" value="${version}"/>
                    </filterset>
                </copy>

                <copy todir="${idp.home.path}/conf" preservelastmodified="true" overwrite="true">
                    <fileset dir="${resources.dir}/conf-tmpl"/>
                    <filterset begintoken="$" endtoken="$">
                        <filter token="IDP_HOME" value="${idp.home.path}"/>
                        <filter token="IDP_VERSION" value="${version}"/>
                        <filter token="IDP_ENTITY_ID" value="${idp.entity.id}"/>
                        <filter token="IDP_SCOPE" value="${idp.scope}"/>
                        <filter token="IDP_CERTIFICATE" value="${idp.cert}"/>
                        <filter token="IDP_HOSTNAME" value="${idp.hostname}"/>
                    </filterset>
                </copy>

                <stringFromFile input="${idp.home.path}/credentials/idp.crt" addProperty="idp.cert"/>
                <regexSplit input="${idp.cert}" regex="\A.*-----\s((?:.*\s)*)-----END.*\Z" addproperty="idp.metadata.cert"/>
                <copy todir="${idp.home.path}/metadata" preservelastmodified="true" overwrite="true">
                    <fileset dir="${resources.dir}/metadata-tmpl"/>
                    <filterset begintoken="$" endtoken="$">
                        <filter token="IDP_ENTITY_ID" value="${idp.entity.id}"/>
                        <filter token="IDP_SCOPE" value="${idp.scope}"/>
                        <filter token="IDP_CERTIFICATE" value="${idp.metadata.cert}"/>
                        <filter token="IDP_HOSTNAME" value="${idp.hostname}"/>
                    </filterset>
                </copy>
            </then>
            <else>
                <propertyfile file="${resources.dir}/install.properties">
                    <entry key="idp.home" value="${idp.home.input}"/>
                </propertyfile>
            	
                <if> 
                    <available file="${idp.home.path}/conf/web.xml" property="idp.web.xml.exists"/>
                    <then>
                        <echo message="Using ${idp.home.path}/conf/web.xml rather than default web.xml"/>
                        <var name="idp.web.xml.name" value="${idp.home.path}/conf/web.xml"/>
                    </then>
                    <else>
                        <var name="idp.web.xml.name" value="${webapp.dir}/WEB-INF/web.xml"/>
                    </else>
                </if>

                <!-- 
                    Even if we're not overwritting the config we should still add new files, for example, files added
                    in a new release.
                -->
                <var name="idp.entity.id" value="https://${idp.hostname}/idp/shibboleth"/>
                <regexSplit input="${idp.hostname}" regex="^.*\.(.*\..*$)" addproperty="idp.scope"/>
                <stringFromFile input="${idp.home.path}/credentials/idp.crt" addProperty="idp.cert"/>
                    
                <copy todir="${idp.home.path}/bin" preservelastmodified="true" overwrite="false">
                    <fileset dir="${tools.dir}/bash">
                        <present present="srconly" targetdir="${idp.home.path}/bin"/>
                	</fileset>
                    <fileset dir="${tools.dir}/bat">
                    	<present present="srconly" targetdir="${idp.home.path}/bin"/>
                	</fileset>
                    <filterset begintoken="$" endtoken="$">
                        <filter token="IDP_HOME" value="${idp.home.path}"/>
                        <filter token="IDP_VERSION" value="${version}"/>
                    </filterset>
                </copy>

                <copy todir="${idp.home.path}/conf" preservelastmodified="true" overwrite="false">
                    <fileset dir="${resources.dir}/conf-tmpl">
                    	<present present="srconly" targetdir="${idp.home.path}/conf"/>
                	</fileset>
                    <filterset begintoken="$" endtoken="$">
                        <filter token="IDP_HOME" value="${idp.home.path}"/>
                        <filter token="IDP_VERSION" value="${version}"/>
                        <filter token="IDP_ENTITY_ID" value="${idp.entity.id}"/>
                        <filter token="IDP_SCOPE" value="${idp.scope}"/>
                        <filter token="IDP_CERTIFICATE" value="${idp.cert}"/>
                        <filter token="IDP_HOSTNAME" value="${idp.hostname}"/>
                    </filterset>
                </copy>
            </else>
        </if>
                            
        <chmod file="${idp.home.path}/bin/*.sh" perm="+x"/>

    	<delete>
    	    <fileset dir="${idp.home.path}/lib" includes="**/*.jar"/>
        </delete>
        <copy todir="${idp.home.path}/lib" preservelastmodified="true" overwrite="true">
            <fileset dir="${basedir}/lib"/>
        </copy>
        <copy todir="${idp.home.path}/lib/endorsed" preservelastmodified="true" failonerror="false" overwrite="true">
            <fileset dir="endorsed"/>
        </copy>

        <!-- create web.xml -->
        <copy file="${idp.web.xml.name}" todir="${installer.dir}" preservelastmodified="true" overwrite="true">
            <filterset begintoken="$" endtoken="$">
                <filter token="IDP_HOME" value="${idp.home.url}"/>
            </filterset>
        </copy>

        <!-- build the war file -->
        <war warfile="${installer.dir}/${war.name}.war" webxml="${installer.dir}/web.xml">
            <lib dir="${basedir}/lib" excludes="servlet-api*.jar,jsp-api*.jar"/>
            <webinf dir="${webapp.dir}/WEB-INF" excludes="web.xml"/>
            <fileset dir="${webapp.dir}" excludes="WEB-INF/**"/>
        </war>
    	<copy file="${installer.dir}/${war.name}.war" todir="${idp.home.path}/war" preservelastmodified="true" overwrite="true"/>

        <!-- Remove generated files -->
        <delete file="${installer.dir}/web.xml"/>
    	<delete file="${installer.dir}/${war.name}.war"/>
    	
    </target>

    <target name="renew-cert" description="Create a new certificate/key pair."> 
        <input message="This will create a new set of credentials for your IdP.  If you ran this command previously and still have '*.new' files, they will be overwritten.  Do you wish to proceed?" 
        	   addproperty="renew.cert.do" validargs="yes,no" defaultvalue="no"/> 
        <if> 
        	<equals arg1="${renew.cert.do}" arg2="yes"/> 
            <then> 
            	
                <input message="Where is the Shibboleth Identity Provider installed?" addproperty="idp.home.input" defaultvalue="${idp.home}"/>
                <var name="idp.home" value="${idp.home.input}"/>
              
                <pathToAbsolutePath path="${idp.home}" addproperty="idp.home.path"/> 
            
                <input message="What is the fully qualified hostname of the Shibboleth Identity Provider server?" addproperty="idp.hostname.input" defaultvalue="${idp.hostname}"/> 
                <var name="idp.hostname" value="${idp.hostname.input}"/> 
                <var name="idp.entity.id" value="https://${idp.hostname}/idp/shibboleth"/> 

                <input message="A keystore is about to be generated for you. Please enter a password that will be used to protect it." addproperty="idp.keystore.pass"/> 

                <if>
                    <isset property="env.IdPCertLifetime"/>
                    <then>
                        <var name="idp.cert.lifetime" value="${env.IdPCertLifetime}"/>
                    </then>
                    <else>
                        <var name="idp.cert.lifetime" value="20"/>
                    </else>
                </if>
        
                <echo message="Generating new signing and encryption key, certificate, and keystore. "/> 
                <selfSignedCert hostname="${idp.hostname}" certificateLifetime="${idp.cert.lifetime}" uriSubjectAltNames="${idp.entity.id}"
                	            privateKeyFile="${idp.home.path}/credentials/idp.key.new" certificateFile="${idp.home.path}/credentials/idp.crt.new"  
                	            keystoreFile="${idp.home.path}/credentials/idp.jks.new"  keystorePassword="${idp.keystore.pass}" /> 
            </then>
        </if>
    </target>
</project>