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

def isSnapshot = project.version.contains('SNAPSHOT')
def isReleaseVersion = !isSnapshot

publishing {
    publications {
        maven(MavenPublication) {
            pom {
                afterEvaluate {
                    name = "$project.group:$project.name" as String
                    description = project.description
                }

                url = 'https://conscrypt.org/'

                scm {
                    connection = 'scm:git:https://github.com/google/conscrypt.git'
                    developerConnection = 'scm:git:git@github.com:google/conscrypt.git'
                    url = 'https://github.com/google/conscrypt'
                }

                licenses {
                    license {
                        name = 'Apache 2'
                        url = 'https://www.apache.org/licenses/LICENSE-2.0'
                    }
                }

                developers {
                    developer {
                        id = 'conscrypt'
                        name = 'Conscrypt Contributors'
                        email = 'conscrypt@googlegroups.com'
                        url = 'https://conscrypt.org/'
                        organization = 'Google, Inc.'
                        organizationUrl = 'https://www.google.com'
                    }
                }
            }
        }
    }

    repositories {
        maven {
            def snapshotUrl = 'https://oss.sonatype.org/content/repositories/snapshots/'
            def stagingUrl = rootProject.hasProperty('repositoryId') ? \
                'https://oss.sonatype.org/service/local/staging/deployByRepositoryId/' \
                    + rootProject.repositoryId : \
                'https://oss.sonatype.org/service/local/staging/deploy/maven2/'
            url isSnapshot ? snapshotUrl : stagingUrl
            credentials {
                username = rootProject.findProperty('ossrhUsername') ?: ''
                password = rootProject.findProperty('ossrhPassword') ?: ''
            }
        }
    }
}

signing {
    required { isReleaseVersion && gradle.taskGraph.hasTask("publish") }
    sign publishing.publications.maven
}

signMavenPublication.doFirst {
    publishing.publications.maven.artifacts.each {
        if (it.file.absolutePath.endsWith('.jar') && it.classifier != 'sources' && it.classifier != 'javadoc') {
            logger.info("Signing jar: ${it.file.absolutePath}")
            signJar(it.file.absolutePath)
        }
   }
}

tasks.withType(Sign) {
    onlyIf { isReleaseVersion } 
}

def signJar(String jarPath) {
    if (rootProject.hasProperty('signingKeystore') && rootProject.hasProperty('signingPassword')) {
        def command = 'jarsigner -keystore ' + rootProject.signingKeystore +
                ' -storepass ' + rootProject.signingPassword +
                ' ' + jarPath + ' signingcert'
        def process = command.execute()
        process.in.eachLine { line -> logger.info(line) }
        process.err.eachLine { line -> logger.error(line) }
        process.waitFor()
        if (process.exitValue() != 0) {
            throw new GradleException("jarsigner failed for " + jarPath + " (exit " + process.exitValue() + ")")
        }
    } else {
        logger.lifecycle("Skipping jar signing for ${jarPath} (no signingKeystore/signingPassword)")
    }
}
