apply plugin: 'maven'
apply plugin: 'signing'

def isLocalBuild() {
    return getProperty('VERSION_NAME').toString().contains("LOCAL")
}

def isReleaseBuild() {
    return getProperty('VERSION_NAME').toString().contains("SNAPSHOT")
}

def getLocalRepositoryUrl() {
    return getProperty('LOCAL_REPOSITORY_URL')
}

def getSnapshotRepositoryUrl() {
    return getProperty('SNAPSHOT_REPOSITORY_URL')
}

def getDebugRepositoryUrl() {
    return isLocalBuild() ? getLocalRepositoryUrl() : getSnapshotRepositoryUrl()
}

def getReleaseRepositoryUrl() {
    return getProperty('RELEASE_REPOSITORY_URL')
}

def getRepositoryUsername() {
    return hasProperty('MAVEN_USERNAME') ? getProperty('MAVEN_USERNAME') : ""
}

def getRepositoryPassword() {
    return hasProperty('MAVEN_PASSWORD') ? getProperty('MAVEN_PASSWORD') : ""
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                configurePOM(pom)

                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                repository(url: getReleaseRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
                snapshotRepository(url: getDebugRepositoryUrl()) {
                    authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
                }
            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task androidJavadocs(type: Javadoc) {
        failOnError false
        source = android.sourceSets.main.java.source
        options {
            links "http://docs.oracle.com/javase/7/docs/api/"
            linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
        }
        classpath += project.android.libraryVariants.toList().first().javaCompile.classpath
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }

    task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
    }

    task apklib(type: Zip) {
        appendix = extension = 'apklib'
        from 'AndroidManifest.xml'
        into('res') {
            from 'res'
        }
        into('src') {
            from 'src'
        }
    }

    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
        archives apklib
    }
}

private configurePOM(def pom) {
    pom.project {
        groupId getProperty('POM_GROUP_ID')
        artifactId getProperty('POM_ARTIFACT_ID')
        version getProperty('VERSION_NAME')
        name getProperty('POM_NAME')
        packaging getProperty('POM_PACKAGING')
        description getProperty('POM_DESCRIPTION')
        url getProperty('POM_URL')
        inceptionYear getProperty('POM_INCEPTION_YEAR')

        scm {
            url getProperty('POM_SCM_URL')
            connection getProperty('POM_SCM_CONNECTION')
            developerConnection getProperty('POM_SCM_DEV_CONNECTION')
        }

        licenses {
            license {
                name getProperty('POM_LICENCE_NAME')
                url getProperty('POM_LICENCE_URL')
                distribution getProperty('POM_LICENCE_DIST')
                comments getProperty('POM_LICENCE_COMMENTS')
            }
        }

        developers {
            developer {
                id getProperty('POM_DEVELOPER_ID')
                name getProperty('POM_DEVELOPER_NAME')
                email getProperty('POM_DEVELOPER_EMAIL')
                url getProperty('POM_DEVELOPER_URL')
            }
        }

        issueManagement {
            system getProperty('POM_ISSUE_MANAGEMENT_SYSTEM')
            url getProperty('POM_ISSUE_MANAGEMENT_URL')
        }
    }
}