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

def localPropertiesFile = new File(getRootDir(), "local.properties")
Properties localProperties = new Properties()
if (localPropertiesFile.exists()) {
    localPropertiesFile.withInputStream { instr ->
        localProperties.load(instr)
    }
}

def versionFile = new File(getRootDir(),  "version.properties")
Properties versionProperties = new Properties()
if (versionFile.exists()) {
    versionFile.withInputStream { instr ->
        versionProperties.load(instr)
    }
}
version = versionProperties.getProperty('VERSION')
def media_version = versionProperties.getProperty('MEDIA_VERSION')
def machineLearningVersion = versionProperties.getProperty('MACHINE_LEARNING_VERSION')

signing {
    if (!project.hasProperty('signing.key') || !project.hasProperty('signing.password') || !project.hasProperty('signing.keyId')) {
        logger.error('Missing GPG Signing Properties.')
    }
    def signingKey = findProperty("signing.key")
    def signingPassword = findProperty("signing.password")
    def keyId = findProperty("signing.keyId")
    useInMemoryPgpKeys(keyId, signingKey, signingPassword)

    sign(publishing.publications)
}

project.afterEvaluate {
    publishing {
        publications {
            publishChimeSDK(MavenPublication) {
                afterEvaluate {
                    project.pluginManager.withPlugin("com.android.library") {
                        from(components.named("release").get())
                    }
                }
                groupId = GROUP
                version = version
                artifactId = ARTIFACT_ID_SDK

                pom {
                    name = POM_SDK_NAME
                    description = POM_DESCRIPTION_SDK
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }
            publishChimeSDKMedia(MavenPublication) {
                groupId = GROUP
                version = media_version
                artifactId = ARTIFACT_ID_SDK_MEDIA
                artifact("$buildDir/../libs/amazon-chime-sdk-media.aar")

                pom {
                    name = POM_SDK_MEDIA_NAME
                    description = POM_DESCRIPTION_SDK_MEDIA
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }
            publishChimeSDKMediaX86Stub(MavenPublication) {
                groupId = GROUP
                version = media_version
                artifactId = ARTIFACT_ID_SDK_MEDIA_X86_STUB
                artifact("$buildDir/../libs/amazon-chime-sdk-media-x86-stub.aar")

                pom {
                    name = POM_SDK_MEDIA_X86_STUB_NAME
                    description = POM_DESCRIPTION_SDK_MEDIA_X86_STUB
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }
            publishChimeSDKMediaNoVideoCodecs(MavenPublication) {
                groupId = GROUP
                version = media_version
                artifactId = ARTIFACT_ID_SDK_MEDIA_NO_VIDEO_CODECS
                artifact("$buildDir/../libs/amazon-chime-sdk-media-no-video-codecs.aar")

                pom {
                    name = POM_SDK_MEDIA_NO_VIDEO_CODECS_NAME
                    description = POM_DESCRIPTION_SDK_MEDIA_NO_VIDEO_CODECS
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }
            publishChimeSDKMediaNoVideoCodecsX86Stub(MavenPublication) {
                groupId = GROUP
                version = media_version
                artifactId = ARTIFACT_ID_SDK_MEDIA_NO_VIDEO_CODECS_X86_STUB
                artifact("$buildDir/../libs/amazon-chime-sdk-media-no-video-codecs-x86-stub.aar")

                pom {
                    name = POM_SDK_MEDIA_NO_VIDEO_CODECS_X86_STUB_NAME
                    description = POM_DESCRIPTION_SDK_MEDIA_NO_VIDEO_CODECS_X86_STUB
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }
            publishChimeSDKMachineLearning(MavenPublication) {
                groupId = GROUP
                version = machineLearningVersion
                artifactId = ARTIFACT_ID_SDK_MACHINE_LEARNING
                artifact("$buildDir/../libs/amazon-chime-sdk-machine-learning.aar")

                pom {
                    name = POM_SDK_MACHINE_LEARNING_NAME
                    description = POM_DESCRIPTION_SDK_MACHINE_LEARNING
                    url = POM_URL
                    licenses {
                        license {
                            name = POM_LICENCE_NAME
                            url = POM_LICENCE_URL
                            distribution = POM_LICENCE_DIST
                        }
                    }
                    scm {
                        url = POM_SCM_URL
                        connection = POM_SCM_CONNECTION
                        developerConnection = POM_SCM_DEV_CONNECTION
                    }
                    developers {
                        developer {
                            id = POM_DEVELOPER_ID
                            organization = POM_DEVELOPER_ORGANIZATION
                            organizationUrl = POM_DEVELOPER_ORGANIZATION_URL
                        }
                    }
                }
            }

        }

        repositories {
            maven {
                def releasesRepoUrl = "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2/"
                def snapshotsRepoUrl = "https://central.sonatype.com/repository/maven-snapshots/"
                url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
                credentials {
                    username = localProperties.getProperty('SONATYPE_USERNAME_TOKEN')
                    password = localProperties.getProperty('SONATYPE_PASSWORD_TOKEN')
                }
            }
        }
    }
}


