<!--
 * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.com). All Rights Reserved.
 * 
 * This software is the property of WSO2 Inc. and its suppliers, if any.
 * Dissemination of any information or reproduction of any material contained
 * herein in any form is strictly forbidden, unless permitted by WSO2 expressly.
 * You may not alter or remove any copyright or other notice from copies of this content."
-->

<project name="identity-apps" basedir=".">

    <!-- Check for dependency directory existance -->
    <target name="check-dependencies-dir">
        <available property="dependencies.dir.exists" file="dependencies/" type="dir"/>
    </target>

    <!-- Create dependency directory unless -->
    <target name="create-dependencies-dir" depends="check-dependencies-dir" unless="dependencies.dir.exists">
        <mkdir dir="dependencies"/>  
    </target>

    <!-- Check for distribution directory existance -->
    <target name="check-distribution-dir">
        <available property="distribution.auth.portal.dir.exists" file="distributions/authentication-portal" type="dir"/>
        <available property="distribution.email.otp.auth.portal.dir.exists" file="distributions/email-otp-authentication-portal" type="dir"/>
        <available property="distribution.recovery.portal.dir.exists" file="distributions/recovery-portal" type="dir"/>
        <available property="distribution.sms.otp.auth.portal.dir.exists" file="distributions/sms-otp-authentication-portal" type="dir"/>
        <available property="distribution.x509.cert.portal.dir.exists" file="distributions/x509-certificate-authentication-portal" type="dir"/>
    </target>

    <!-- Create distribution directory unless -->
    <target name="create-auth-distribution-dir" depends="check-distribution-dir" unless="distribution.auth.portal.dir.exists">
        <mkdir dir="distributions/authentication-portal"/>  
    </target>

    <target name="create-email-auth-distribution-dir" depends="check-distribution-dir" unless="distribution.email.otp.auth.portal.dir.exists">
        <mkdir dir="distributions/email-otp-authentication-portal"/>  
    </target>

    <target name="create-recovery-distribution-dir" depends="check-distribution-dir" unless="distribution.recovery.portal.dir.exists">
        <mkdir dir="distributions/recovery-portal"/>  
    </target>

    <target name="create-sms-otp-distribution-dir" depends="check-distribution-dir" unless="distribution.sms.otp.auth.portal.dir.exists">
        <mkdir dir="distributions/sms-otp-authentication-portal"/>  
    </target>

    <target name="create-x509-cert-distribution-dir" depends="check-distribution-dir" unless="distribution.x509.cert.portal.dir.exists">
        <mkdir dir="distributions/x509-certificate-authentication-portal"/>  
    </target>


    <!-- Check if identity apps depenedency exists -->
    <target name="check-dependency" depends="create-dependencies-dir">
        <available file="dependencies/v${identity.apps.version}.zip" property="dependency.exists"/>
    </target>

    <!-- Download identity apps depenedency unless -->
    <target name="download-dependency" depends="check-dependency" unless="dependency.exists">
        <echo message="--- Itentity Apps not found. Downloading dependency. ---"/>
        <get src="https://github.com/wso2/identity-apps/archive/v${identity.apps.version}.zip"
             dest="dependencies/"
             verbose="false"
             usetimestamp="true"/>
    </target>

    <!-- Extract identity apps depenedency  -->
    <target name="extract-dependency">
        <unzip src="dependencies/v${identity.apps.version}.zip" dest="target" overwrite="true"/>
        <chmod file="target/identity-apps-${identity.apps.version}" perm="700"/>
        <move todir="target/identity-apps">
            <fileset dir="target/identity-apps-${identity.apps.version}"/>
        </move>
    </target>

    <target name="init-repo" 
        depends="check-dependency,
            download-dependency,
            extract-dependency,
            create-auth-distribution-dir,
            create-email-auth-distribution-dir,
            create-recovery-distribution-dir,
            create-sms-otp-distribution-dir,
            create-x509-cert-distribution-dir"
    />

</project>
