<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" />

    <!-- 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" />

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

                <echo message="Generating signing and encryption key, certificate, and keystore. " />
                <selfSignedCert hostname="${idp.hostname}"
                                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}"
                                uriSubjectAltNames="${idp.entity.id}" />

                <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>
            	
                <!-- 
                    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="${webapp.dir}/WEB-INF/web.xml"
              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"/>
            <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>

</project>