/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
description = 'Fineract WAR'

apply plugin: 'war'
apply plugin: 'distribution'
apply from: "${rootDir}/buildSrc/src/main/groovy/org.apache.fineract.dependencies.gradle"

war {
    description = 'Assembles a WAR archive containing the web application'
    archiveFileName = 'fineract-provider.war'
    
    // Enable caching
    outputs.cacheIf { true }
    
    // Track inputs explicitly
    inputs.files(project.configurations.runtimeClasspath)
        .withPropertyName("runtimeClasspath")
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.files(project.sourceSets.main.output.classesDirs)
        .withPropertyName("classes")
        .withPathSensitivity(PathSensitivity.RELATIVE)
        
    def licensesDir = file("$rootDir/licenses/binary/")
    if (licensesDir.exists()) {
        inputs.dir(licensesDir)
            .withPropertyName("licenses")
            .withPathSensitivity(PathSensitivity.RELATIVE)
            
        from(licensesDir) {
            into "WEB-INF/licenses/binary/"
        }
    }
    
    def legalFiles = [
        file("$rootDir/LICENSE_RELEASE"),
        file("$rootDir/NOTICE_RELEASE"),
        file("$rootDir/DISCLAIMER")
    ].findAll { it.exists() }
    
    if (!legalFiles.empty) {
        inputs.files(legalFiles)
            .withPropertyName("legalFiles")
            .withPathSensitivity(PathSensitivity.RELATIVE)
    }
    
    legalFiles.each { file ->
        from(file) {
            into "WEB-INF/"
        }
    }
    
    rename ('LICENSE_RELEASE', 'LICENSE')
    rename ('NOTICE_RELEASE', 'NOTICE')
    
    enabled = true
    archiveClassifier = ''
    
    // Ensure reproducible output
    preserveFileTimestamps = false
    reproducibleFileOrder = true
}

dependencies {
    implementation project(':fineract-core')
    implementation project(':fineract-accounting')
    implementation project(':fineract-investor')
    implementation project(':fineract-branch')
    implementation project(':fineract-document')
    implementation project(':fineract-charge')
    implementation project(':fineract-rates')
    implementation project(':fineract-loan')
    implementation project(':fineract-progressive-loan')
    implementation project(':fineract-savings')
    implementation project(':fineract-provider')
}

tasks.withType(Tar) {
    compression Compression.GZIP
    archiveExtension = 'tar.gz'
    
    // Enable caching for all tar tasks
    outputs.cacheIf { true }
    
    // Ensure reproducible output
    preserveFileTimestamps = false
    reproducibleFileOrder = true
}

distributions {
    binary {
        distributionBaseName = 'apache-fineract-bin'
        contents {
            // Track inputs explicitly for binary distribution
            filesMatching('**/*.jar') {
                it.path = it.path.replaceAll('-\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?', '-' + version)
            }
            
            from ("$rootDir/fineract-client/build/libs/") {
                include 'fineract-client-*.jar'
            }
            from ("$rootDir/fineract-avro-schemas/build/libs/") {
                include 'fineract-avro-schemas-*.jar'
            }
            from ("$rootDir/fineract-provider/build/libs/") {
                include 'fineract-provider-*.jar'
                exclude 'fineract-provider-*-plain.jar'
            }
            from war
            from("$rootDir/licenses/") {
                into "licenses/"
            }
            from "$rootDir/LICENSE_RELEASE"
            from "$rootDir/NOTICE_RELEASE"
            rename ('LICENSE_RELEASE', 'LICENSE')
            rename ('NOTICE_RELEASE', 'NOTICE')

            from "$rootDir/DISCLAIMER"
            from "$rootDir/README.md"

            from "$rootDir/fineract-doc/build/docs/pdf/en/index.pdf"
            rename ('index.pdf', 'fineract-documentation.pdf')
        }
    }
    src {
        distributionBaseName = 'apache-fineract-src'
        contents {
            // Track inputs explicitly for source distribution
            from("$rootDir/") {
                exclude '**/build' , '.git', '**/.gradle', '.github', '**/.settings', '**/.project', 
                        '**/.classpath', '.idea', 'out', '._.DS_Store', '.DS_Store', 'WebContent', 
                        '**/.externalToolbuilders', '.theia', 'LICENSE_RELEASE', 
                        'NOTICE_RELEASE', '**/licenses', '*.class', '**/bin', '*.log', '.dockerignore', 
                        '**/.gitkeep'
                
                // Ensure consistent file paths for caching
                eachFile { details ->
                    details.path = details.path.replace('\\', '/')
                }
            }
            rename ('LICENSE_SOURCE', 'LICENSE')
            rename ('NOTICE_SOURCE', 'NOTICE')
        }
    }
}

// Configure specific tar tasks
tasks.named('binaryDistTar') {
    description = 'Assembles the binary distribution as a tar archive'
    outputs.cacheIf { true }
    
    // Track dependencies explicitly
    dependsOn(war, ':fineract-client:jar', ':fineract-avro-schemas:jar', 
              ':fineract-provider:build', ':fineract-doc:doc',
              ':fineract-client:javadocJar', ':fineract-client:sourcesJar',
              ':fineract-avro-schemas:javadocJar', ':fineract-avro-schemas:sourcesJar')
}

tasks.named('srcDistTar') {
    description = 'Assembles the source distribution as a tar archive'
    outputs.cacheIf { true }
}

// Disable zip distributions as they're not needed
binaryDistZip.enabled false
srcDistZip.enabled false
