/*
 * PerlOnJava Build Configuration
 * This Gradle build script configures the build process for the PerlOnJava project
 */

buildscript {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}

// Core plugins configuration
plugins {
    id 'java'
    // Plugin for checking dependency updates
    id 'com.github.ben-manes.versions' version '0.52.0'
    // Plugin for automatically updating dependencies
    id 'se.patrikerdes.use-latest-versions' version '0.2.19'
    id 'application'
    // Plugin for creating OS packages (deb)
    id 'com.netflix.nebula.ospackage' version '12.1.0'
    // Plugin for creating fat/uber JARs
    id 'com.github.johnrengelman.shadow' version '8.1.1'
}

// Main application class configuration
application {
    mainClass = 'org.perlonjava.Main'
}

// Debian package build dependency
tasks.buildDeb {
    dependsOn installDist
}

// Project metadata
group = 'org.perlonjava'
version = '3.0.0'

// OS package configuration for Debian packaging
ospackage {
    packageName = 'perlonjava'
    version = project.version
    maintainer = 'Flavio Soibelmann Glock <fglock@gmail.com>'
    requires('java-21-openjdk-headless')

    into '/opt/perlonjava'
    from('build/install/perlonjava') {
        into '/opt/perlonjava'
    }

    link('/usr/local/bin/jperl', '/opt/perlonjava/bin/jperl')
}

// Java toolchain configuration - requires Java 21
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

// Repository configuration
repositories {
    mavenCentral()
}

// Project dependencies
dependencies {
    // Core dependencies
    implementation 'org.ow2.asm:asm:9.8'                           // ByteCode manipulation
    implementation 'org.ow2.asm:asm-util:9.8'                     // ASM utilities
    implementation 'com.ibm.icu:icu4j:77.1'                       // Unicode support
    implementation 'com.alibaba.fastjson2:fastjson2:2.0.59'       // JSON processing
    implementation 'org.snakeyaml:snakeyaml-engine:2.10'           // YAML processing
    implementation 'org.apache.commons:commons-csv:1.14.1'          // CSV processing
    implementation 'net.java.dev.jna:jna:5.18.0'                   // Native access
    implementation 'net.java.dev.jna:jna-platform:5.18.0'          // Native access

    // Testing dependencies
    testImplementation 'org.junit.jupiter:junit-jupiter-api:6.0.0-RC3'
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:6.0.0-RC3'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:6.0.0-RC3'
}

// JUnit configuration
testing {
    suites {
        test {
            useJUnitJupiter()
        }
    }
}

// Java compilation settings
tasks.withType(JavaCompile).configureEach {
    options.compilerArgs << '-Xlint:-options'
    options.compilerArgs << '-Xlint:deprecation'
}

// Test execution configuration with native access
tasks.withType(Test).configureEach {
    jvmArgs += '--enable-native-access=ALL-UNNAMED'
}

// Enable native access for all Java execution tasks
allprojects {
    tasks.withType(JavaExec).configureEach {
        jvmArgs += '--enable-native-access=ALL-UNNAMED'
    }
}

// JUnit platform configuration
test {
    useJUnitPlatform()
}

// Shadow JAR configuration for creating standalone executable
shadowJar {
    archiveClassifier.set('')
    destinationDirectory = file("$buildDir/../target")
    manifest {
        attributes 'Main-Class': 'org.perlonjava.Main'
    }
    exclude 'module-info.class'
    exclude 'META-INF/MANIFEST.MF'
}

// Make shadowJar part of the build process
tasks.named('build') {
    dependsOn shadowJar
}

// Source sets configuration for including Perl resources
sourceSets {
    main {
        resources {
            srcDir 'src/main/perl'
            srcDir 'src/main/resources'
            include '**/*.pm'
            include '**/*.pl'
            include 'META-INF/services/**'
        }
    }
    test {
        resources {
            srcDir 'src/test/resources'
        }
    }
}

// Resource processing configuration
tasks.named('processResources', Copy) {
    from(sourceSets.main.resources.srcDirs) {
        include '**/*.pm'
        include 'META-INF/services/**'
    }
    into("$buildDir/resources/main")
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

// Test resource processing configuration
tasks.named('processTestResources', Copy) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
