apply plugin: 'maven-publish'
apply plugin: 'signing'

ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''

File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
    Properties p = new Properties()
    p.load(new FileInputStream(secretPropsFile))
    p.each { name, value ->
        ext[name] = value
    }
} else {
    ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
    ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
    ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
    ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
    ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
}

project.afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release

                groupId POM_GROUP_ID
                artifactId POM_ARTIFACT_ID
                version POM_VERSION_NAME

                pom {
                    name = POM_NAME
                    description = POM_DESCRIPTION
                    url = POM_URL
                    inceptionYear = POM_INCEPTION_YEAR
                    packaging = POM_PACKAGING
                    scm {
                        connection = POM_SCM_CONNECTION
                        url = POM_URL
                    }
                    issueManagement {
                        system = POM_ISSUE_SYSTEM
                        url = POM_ISSUE_URL
                    }
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            name = POM_DEVELOPER_NAME
                        }
                    }
                }
            }
        }
        repositories {
            maven {
                name = "sonatype"

                def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
                def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
                url = POM_VERSION_NAME.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl

                credentials {
                    username ossrhUsername
                    password ossrhPassword
                }
            }
        }
    }
}

signing {
    sign publishing.publications
}
