<!--
~ Copyright (c) 2022, 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="login-portal-layouts" basedir=".">

    <!-- Copy layouts to the authentication-portal -->
    <target name="check-authentication-portal-includes-layouts-dir">
        <available
            property="authentication-portal.includes.layouts.dir.exists"
            file="../../apps/authentication-portal/src/main/webapp/includes/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-authentication-portal-includes-layouts-dir"
        depends="check-authentication-portal-includes-layouts-dir"
        unless="${authentication-portal.includes.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/authentication-portal/src/main/webapp/includes/layouts"/>
    </target>

    <target name="copy-default-layout-into-authentication-portal" depends="create-authentication-portal-includes-layouts-dir">
        <echo message="--- copying default layout to Authentication Portal ---"/>
        <copy todir="../../apps/authentication-portal/src/main/webapp/includes/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <include name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target name="check-authentication-portal-extensions-layouts-dir">
        <available
            property="authentication-portal.extenions.layouts.dir.exists"
            file="../../apps/authentication-portal/src/main/webapp/extensions/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-authentication-portal-extensions-layouts-dir"
        depends="check-authentication-portal-extensions-layouts-dir"
        unless="${authentication-portal.extenions.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/authentication-portal/src/main/webapp/extensions/layouts"/>
    </target>

    <target 
        name="copy-custom-layouts-into-authentication-portal"
        depends="create-authentication-portal-extensions-layouts-dir"
    >
        <echo message="--- copying custom layouts to Authentication Portal ---"/>
        <copy todir="../../apps/authentication-portal/src/main/webapp/extensions/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <exclude name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target
        name="copy-layouts-into-authentication-portal"
        depends="
        copy-default-layout-into-authentication-portal,
        copy-custom-layouts-into-authentication-portal
        "
    />

    <!-- Copy layouts to the recovery-portal -->
    <target name="check-recovery-portal-includes-layouts-dir">
        <available
            property="recovery-portal.includes.layouts.dir.exists"
            file="../../apps/recovery-portal/src/main/webapp/includes/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-recovery-portal-includes-layouts-dir"
        depends="check-recovery-portal-includes-layouts-dir"
        unless="${recovery-portal.includes.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/recovery-portal/src/main/webapp/includes/layouts"/>
    </target>

    <target name="copy-default-layout-into-recovery-portal" depends="create-recovery-portal-includes-layouts-dir">
        <echo message="--- copying default layout to Recovery Portal ---"/>
        <copy todir="../../apps/recovery-portal/src/main/webapp/includes/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <include name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target name="check-recovery-portal-extensions-layouts-dir">
        <available
            property="recovery-portal.extenions.layouts.dir.exists"
            file="../../apps/recovery-portal/src/main/webapp/extensions/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-recovery-portal-extensions-layouts-dir"
        depends="check-recovery-portal-extensions-layouts-dir"
        unless="${recovery-portal.extenions.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/recovery-portal/src/main/webapp/extensions/layouts"/>
    </target>

    <target 
        name="copy-custom-layouts-into-recovery-portal"
        depends="create-recovery-portal-extensions-layouts-dir"
    >
        <echo message="--- copying custom layouts to Recovery Portal ---"/>
        <copy todir="../../apps/recovery-portal/src/main/webapp/extensions/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <exclude name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target
        name="copy-layouts-into-recovery-portal"
        depends="
        copy-default-layout-into-recovery-portal,
        copy-custom-layouts-into-recovery-portal
        "
    />

    <!-- Copy layouts to the x509-certificate-authentication-portal -->
    <target name="check-x509-certificate-authentication-portal-includes-layouts-dir">
        <available
            property="x509-certificate-authentication-portal.includes.layouts.dir.exists"
            file="../../apps/x509-certificate-authentication-portal/src/main/webapp/includes/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-x509-certificate-authentication-portal-includes-layouts-dir"
        depends="check-x509-certificate-authentication-portal-includes-layouts-dir"
        unless="${x509-certificate-authentication-portal.includes.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/x509-certificate-authentication-portal/src/main/webapp/includes/layouts"/>
    </target>

    <target name="copy-default-layout-into-x509-certificate-authentication-portal" depends="create-x509-certificate-authentication-portal-includes-layouts-dir">
        <echo message="--- copying default layout to X509 Certificate Authentication Portal ---"/>
        <copy todir="../../apps/x509-certificate-authentication-portal/src/main/webapp/includes/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <include name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target name="check-x509-certificate-authentication-portal-extensions-layouts-dir">
        <available
            property="x509-certificate-authentication-portal.extenions.layouts.dir.exists"
            file="../../apps/x509-certificate-authentication-portal/src/main/webapp/extensions/layouts"
            type="dir"
        />
    </target>

    <target
        name="create-x509-certificate-authentication-portal-extensions-layouts-dir"
        depends="check-x509-certificate-authentication-portal-extensions-layouts-dir"
        unless="${x509-certificate-authentication-portal.extenions.layouts.dir.exists}"
    >
        <mkdir dir="../../apps/x509-certificate-authentication-portal/src/main/webapp/extensions/layouts"/>
    </target>

    <target 
        name="copy-custom-layouts-into-x509-certificate-authentication-portal"
        depends="create-x509-certificate-authentication-portal-extensions-layouts-dir"
    >
        <echo message="--- copying custom layouts to X509 Certificate Authentication Portal ---"/>
        <copy todir="../../apps/x509-certificate-authentication-portal/src/main/webapp/extensions/layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <exclude name="default/**"/>
            </fileset>
        </copy>
    </target>

    <target
        name="copy-layouts-into-x509-certificate-authentication-portal"
        depends="
        copy-default-layout-into-x509-certificate-authentication-portal,
        copy-custom-layouts-into-x509-certificate-authentication-portal
        "
    />

    <!-- Copy layouts to the console -->
    <target name="check-console-layout-dir">
        <available 
            property="console.layouts.dir.exists" 
            file="../../apps/console/src/login-portal-layouts" 
            type="dir"
        />
    </target>

    <target 
        name="create-console-layouts-dir" 
        depends="check-console-layout-dir" 
        unless="check-console-layout-dir"
    >
        <mkdir dir="../../apps/console/src/login-portal-layouts"/>
    </target>

    <target name="copy-layouts-into-console" depends="create-console-layouts-dir">
        <echo message="--- copying layouts to Console ---"/>
        <copy todir="../../apps/console/src/login-portal-layouts/" overwrite="true">
            <fileset dir="target/layouts/">
                <exclude name="**/body.ser"/>
            </fileset>
        </copy>
    </target>

    <target
        name="move-layouts"
        depends="
            copy-layouts-into-authentication-portal,
            copy-layouts-into-recovery-portal,
            copy-layouts-into-x509-certificate-authentication-portal,
            copy-layouts-into-console"
    />

</project>
