plugins {
    id 'java-library'
    id 'base'
    id 'maven-publish'
}

group 'com.frostwire'

base {
    archivesName = 'jlibtorrent'
}
// Just changing version here should be all that's necessary to bump the version on the library
// Dynamic from tag for Github workflow, fallback for local testing/building binaries
version = version = System.getenv('RELEASE_VERSION') ?: System.getenv('GITHUB_REF_NAME')?.replace('release/', '') ?: '2.0.13.6'

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
    withSourcesJar()
    withJavadocJar()
    compileJava.options.encoding = "UTF-8"
    compileTestJava.options.encoding = "UTF-8"
}

// Ensure jar task depends on sourcesJar and javadocJar
tasks.named('jar') {
    dependsOn tasks.named('sourcesJar')
    dependsOn tasks.named('javadocJar')
}

// make build stop at the first error
tasks.withType(JavaCompile).configureEach {
    options.compilerArgs << "-Xmaxerrs" << "1"
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'junit:junit:4.13.2'

    def os = org.gradle.internal.os.OperatingSystem.current()
    def arch = System.getProperty('os.arch').toLowerCase()
    def is_arm64 = arch == "aarch64" || arch == "arm64"

    def flavor = 'windows'

    if (os.isMacOsX()) {
        flavor = is_arm64 ? 'macosx-arm64' : 'macosx-x86_64'
    } else if (os.isLinux()) {
        flavor = 'linux'
    }

    testImplementation files("build/libs/jlibtorrent-${flavor}-${version}.jar")
}

tasks.withType(Test).configureEach {
    systemProperty "java.library.path", "."
}

tasks.test {
    outputs.upToDateWhen { false }
    testLogging {
        events "passed", "skipped", "failed", "standard_out"
    }
}

tasks.register('nativeMacOSArm64Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-macosx-arm64'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/macosx', excludes: ['**/ed25519', '**/src'], include: 'arm64/*libjlibtorrent.arm64.dylib')
    into 'lib/'
    rename(".dylib", "-${version}.dylib")
}

tasks.register('nativeMacOSX86_64Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-macosx-x86_64'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/macosx', excludes: ['**/ed25519', '**/src'], include: 'x86_64/*libjlibtorrent.x86_64.dylib')
    into 'lib/'
    rename(".dylib", "-${version}.dylib")
}

tasks.register('nativeWindowsX86_64Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-windows'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/windows', excludes: ['**/ed25519', '**/src'], include: '**/*jlibtorrent.dll')
    into 'lib/'
    rename(".dll", "-${version}.dll")
}

tasks.register('nativeLinuxJar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-linux'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/linux', excludes: ['**/ed25519', '**/src'], include: '**/*libjlibtorrent.so')
    into 'lib/'
    rename(".so", "-${version}.so")
}

tasks.register('nativeAndroidArmJar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-android-arm'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/android', include: 'armeabi-v7a/libjlibtorrent.so')
    into 'lib/'
    rename(".so", "-${version}.so")
}

tasks.register('nativeAndroidX86Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-android-x86'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/android', include: 'x86/libjlibtorrent.so')
    into 'lib/'
    rename(".so", "-${version}.so")
}

tasks.register('nativeAndroidArm64Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-android-arm64'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/android', include: 'arm64-v8a/libjlibtorrent.so')
    into 'lib/'
    rename(".so", "-${version}.so")
}

tasks.register('nativeAndroidX64Jar', Zip) {
    destinationDirectory = layout.buildDirectory.dir("libs")
    archiveBaseName = 'jlibtorrent-android-x86_64'
    archiveExtension = 'jar'
    from fileTree(dir: 'swig/bin/release/android', include: 'x86_64/libjlibtorrent.so')
    into 'lib/'
    rename(".so", "-${version}.so")
}

// Clean task for nativeMacOSArm64Jar
tasks.register('cleanNativeMacOSArm64Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-macosx-arm64-${version}.jar")
}

// Clean task for nativeMacOSX86_64Jar
tasks.register('cleanNativeMacOSX86_64Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-macosx-x86_64-${version}.jar")
}

// Clean task for nativeWindowsX86_64Jar
tasks.register('cleanNativeWindowsX86_64Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-windows-${version}.jar")
}

// Clean task for nativeLinuxJar
tasks.register('cleanNativeLinuxJar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-linux-${version}.jar")
}

// Clean task for nativeAndroidArmJar
tasks.register('cleanNativeAndroidArmJar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-android-arm-${version}.jar")
}

// Clean task for nativeAndroidX86Jar
tasks.register('cleanNativeAndroidX86Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-android-x86-${version}.jar")
}

// Clean task for nativeAndroidArm64Jar
tasks.register('cleanNativeAndroidArm64Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-android-arm64-${version}.jar")
}

// Clean task for nativeAndroidX64Jar
tasks.register('cleanNativeAndroidX64Jar', Delete) {
    delete layout.buildDirectory.file("libs/jlibtorrent-android-x86_64-${version}.jar")
}

artifacts {
    archives sourcesJar
    archives javadocJar
    archives nativeMacOSArm64Jar
    archives nativeMacOSX86_64Jar
    archives nativeWindowsX86_64Jar
    archives nativeLinuxJar
    archives nativeAndroidArmJar
    archives nativeAndroidX86Jar
    archives nativeAndroidArm64Jar
    archives nativeAndroidX64Jar
}


// Reusable POM configuration method
def configurePom(MavenPom pom, String artifactName, String desc) {
    pom.name = artifactName
    pom.description = desc
    pom.url = 'https://github.com/frostwire/frostwire-jlibtorrent'
    pom.scm {
        connection = 'scm:git:git://github.com/frostwire/frostwire-jlibtorrent.git'
        developerConnection = 'scm:git:ssh:git@github.com/frostwire/frostwire-jlibtorrent.git'
        url = 'https://github.com/frostwire/frostwire-jlibtorrent'
    }
    pom.licenses {
        license {
            name = 'The MIT License'
            url = 'https://github.com/frostwire/frostwire-jlibtorrent/blob/master/LICENSE.md'
        }
    }
    pom.developers {
        developer {
            id = 'gubatron'
            name = 'Angel Leon'
            email = 'gubatron@gmail.com'
        }
    }
}

// Maven publishing to GitHub Packages
publishing {
    repositories {
        maven {
            name = 'GitHubPackages'
            url = "https://maven.pkg.github.com/${System.getenv('GITHUB_REPOSITORY') ?: 'frostwire/frostwire-jlibtorrent'}"
            credentials {
                username = System.getenv('USERNAME')
                // github.actor -> defined in publish.yml when publish action is invoked
                password = System.getenv('TOKEN')
                // secrets.GITHUB_TOKEN -> generated automatically when a github action is ran, has package:write permissions
            }
        }
    }
    publications {
        // Platform-independent jlibtorrent JAR
        jlibtorrent(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-${version}.jar")
            artifact(file("$buildDir/libs/jlibtorrent-${version}-sources.jar")) {
                classifier = 'sources'
            }
            artifact(file("$buildDir/libs/jlibtorrent-${version}-javadoc.jar")) {
                classifier = 'javadoc'
            }
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent', 'The swig Java interface for libtorrent by the makers of FrostWire. Contains only .class files to be used in any architecture alongside the native binary jar')
            }
        }
        // macOS ARM64
        macOSArm64(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-macosx-arm64'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-macosx-arm64-${version}.jar")  // Built on macOS ARM64 with swig/build tasks and ./gradlew nativeMacOSArm64Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-macosx-arm64', 'Native macOS ARM64 binaries for jlibtorrent.')
            }
        }
        // macOS x86_64
        macOSX86_64(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-macosx-x86_64'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-macosx-x86_64-${version}.jar")  // Built on macOS x86_64 with swig/build tasks and ./gradlew nativeMacOSX86_64Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-macosx-x86_64', 'Native macOS x86_64 binaries for jlibtorrent.')
            }
        }
        // Windows
        windows(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-windows'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-windows-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeWindowsX86_64Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-windows', 'Native Windows binaries for jlibtorrent.')
            }
        }
        // Linux
        linux(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-linux'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-linux-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeLinuxJar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-linux', 'Native Linux binaries for jlibtorrent.')
            }
        }
        // Android ARM
        androidArm(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-android-arm'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-android-arm-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeAndroidArmJar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-android-arm', 'Native Android ARM binaries for jlibtorrent.')
            }
        }
        // Android x86
        androidX86(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-android-x86'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-android-x86-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeAndroidX86Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-android-x86', 'Native Android x86 binaries for jlibtorrent.')
            }
        }
        // Android ARM64
        androidArm64(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-android-arm64'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-android-arm64-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeAndroidArm64Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-android-arm64', 'Native Android ARM64 binaries for jlibtorrent.')
            }
        }
        // Android x86_64
        androidX64(MavenPublication) {
            groupId = project.group
            artifactId = 'jlibtorrent-android-x86_64'
            version = project.version
            artifact file("$buildDir/libs/jlibtorrent-android-x86_64-${version}.jar")  // Built in Docker with swig/build tasks and ./gradlew nativeAndroidX64Jar
            pom {
                configurePom(delegate, 'frostwire-jlibtorrent-android-x86_64', 'Native Android x86_64 binaries for jlibtorrent.')
            }
        }
    } // publications
} // publishing
