import java.text.SimpleDateFormat

apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.bintray"
Properties properties = new Properties()
// for wrapper project structure
File propertyFile = project.rootProject.file('bintray.properties')
if (propertyFile.exists()) {
    properties.load(propertyFile.newDataInputStream())
}
def bintray_user = properties.getProperty('bintray_user')
def bintray_api_key = properties.getProperty('bintray_api_key')
version = rootProject.ext.releaseVersionName

install {
    repositories.mavenInstaller {
        pom {
            project {
                packaging 'aar'
                name project.name
                url rootProject.ext.siteUrl
                licenses {
                    license {
                        name rootProject.ext.license.name
                        url rootProject.ext.license.url
                    }
                }
                developers {
                    developer {
                        id rootProject.ext.developer.id
                        name rootProject.ext.developer.name
                        email rootProject.ext.developer.email
                    }
                }
                scm {
                    connection rootProject.ext.gitUrl
                    developerConnection rootProject.ext.gitUrl
                    url rootProject.ext.siteUrl
                }
            }
        }
    }
}

task sourcesJar(type: Jar) {
    if (project.getPlugins().hasPlugin('com.android.application') ||
            project.getPlugins().hasPlugin('com.android.library')) {
        from android.sourceSets.main.java.srcDirs
    }
    classifier = 'sources'
}

if (project.getPlugins().hasPlugin('com.android.application') ||
        project.getPlugins().hasPlugin('com.android.library')) {
    task javadoc(type: Javadoc) {
        failOnError = false
        source = android.sourceSets.main.java.srcDirs
        android.compileSdkVersion rootProject.ext.androidCompileSdkVersion
        android.buildToolsVersion rootProject.ext.androidBuildToolsVersion
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    }
}

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

artifacts {
    archives javadocJar
    archives sourcesJar
}


bintray {
    user = bintray_user
    key = bintray_api_key
    configurations = ['archives'] //When uploading Maven-based publication files
    dryRun = false //Whether to run this as dry-run, without deploying
    publish = true //If version should be auto published after an upload
    pkg {
        repo = 'maven'
        name = project.name
        desc = rootProject.ext.desc
        websiteUrl = rootProject.ext.siteUrl
        issueTrackerUrl = rootProject.ext.issueTrackerUrl
        vcsUrl = rootProject.ext.gitUrl
        licenses = [rootProject.ext.license.id]
        labels = rootProject.ext.artifactLabels
        publicDownloadNumbers = true
        //Optional version descriptor
        version {
            name = rootProject.ext.releaseVersionName //Bintray logical version name
            released = new SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss.SSSZZ').format(new Date())
            //2 possible values: date in the format of 'yyyy-MM-dd'T'HH:mm:ss.SSSZZ' OR a java.util.Date instance
            vcsTag = rootProject.ext.releaseVersionName
        }
    }
}