/*
 * (C) Copyright IBM Deutschland GmbH 2021
 * (C) Copyright IBM Corp. 2021
 */

class PublishingConfig {
    String url
    String username
    String password
    String name
    String repoUrl
    Object pomAction
    Boolean withSources = true
    Boolean withPublication = true
    Boolean isPlatformProject = false
    Boolean sign = false
}

ext.LICENSE = [
    MIT: {
        name = "The MIT License"
        url = "https://opensource.org/licenses/mit-license.php"
    },
    APACHE2: {
        name = "The Apache Software License, Version 2.0"
        url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
    },
]

ext.publish = { args ->
    PublishingConfig config = new PublishingConfig(args)

    apply plugin: "maven-publish"
    apply plugin: "signing"

    if (!config.name) {
        config.name = "$rootProject.name: $project.name"
    }

    def isMultiplatform = !project.file("src/main").exists() && !config.isPlatformProject
    def isAndroidProject = ["main", "androidMain"].any { project.file("src/$it/AndroidManifest.xml").exists() }

    if (config.withPublication) {
        if (!config.isPlatformProject && config.withSources) {
            task javadocJar(type: Jar) {
                archiveClassifier.set('javadoc')
            }
            if (!isMultiplatform) {
                task sourcesJar(type: Jar) {
                    archiveClassifier.set('sources')
                    if (isAndroidProject) {
                        from android.sourceSets.main.java.srcDirs
                        from android.sourceSets.main.kotlin.srcDirs
                    } else {
                        if (project.file("src/main/java").exists()) {
                            from sourceSets.main.java.srcDirs
                        }
                        if (project.file("src/main/kotlin").exists()) {
                            from sourceSets.main.kotlin.srcDirs
                        }
                    }
                }
            }
        }

        // Some components are only available in the afterEvaluate phase (e.g. components.release)
        afterEvaluate {
            publishing {
                publications {
                    if (!isMultiplatform) {
                        maven(MavenPublication) {
                            if (isAndroidProject) {
                                from components.release
                            } else if (config.isPlatformProject) {
                                from components.javaPlatform
                            } else {
                                from components.java
                            }
                            if (!config.isPlatformProject && config.withSources) {
                                artifact sourcesJar
                            }
                        }
                    }
                }

                publications.all {
                    if (!config.isPlatformProject) {
                        artifact javadocJar
                    }

                    pom {
                        name = config.name
                        if (config.repoUrl) {
                            scm {
                                url = config.repoUrl
                                def regex = ~"^https://(github.com/[^/]+/[^/]+).*"
                                def (_, path) = (config.repoUrl =~ regex)[0]
                                if (path) {
                                    connection = "scm:git:git://${path}.git"
                                    developerConnection = "scm:git:ssh://${path}.git"
                                }
                            }
                        }
                    }
                    if (config.pomAction) {
                        pom(config.pomAction)
                    }
                }
            }
            if (config.sign) {
                signing {
                    sign publishing.publications
                }
            }
        }
    }

    publishing {
        repositories {
            maven {
                url config.url
                credentials {
                    username config.username
                    password config.password
                }
            }
        }
    }
}
