/**
 * Copyright 2016 Dmytro Zaitsev
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.text.SimpleDateFormat

apply plugin: 'com.jfrog.bintray'

version = POM_LIBRARY_VERSION
final Date buildTimeAndDate = new Date()
ext {
  buildTime = new SimpleDateFormat('yyyy-MM-dd').format(buildTimeAndDate)
  buildDate = new SimpleDateFormat('HH:mm:ss.SSSZ').format(buildTimeAndDate)
}

def manifestAttributes = [
    'Built-By'              : System.properties['user.name'],
    'Created-By'            : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.getProperty("java.vm.version")})",
    'Build-Date'            : project.buildTime,
    'Build-Time'            : project.buildDate,
    'Specification-Title'   : POM_ARTIFACT,
    'Specification-Version' : project.version,
    'Specification-Vendor'  : POM_SITE_URL,
    'Implementation-Title'  : POM_ARTIFACT,
    'Implementation-Version': POM_LIBRARY_VERSION,
    'Implementation-Vendor' : POM_SITE_URL
]

task sourcesJar(type: Jar) {
  manifest {
    attributes(manifestAttributes)
  }
  from android.sourceSets.main.java.srcDirs
  classifier = 'sources'
}

task javadoc(type: Javadoc) {
  failOnError false
  source = android.sourceSets.main.java.srcDirs
  classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}

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

artifacts {
  archives javadocJar
  archives sourcesJar
}

bintray {
  dryRun = false // Whether to run this as dry-run, without deploying
  publish = false // If version should be auto published after an upload
  user = project.hasProperty('bintrayUser') ? project.property('bintrayUser') : System.getenv('BINTRAY_USER')
  key = project.hasProperty('bintrayApiKey') ? project.property('bintrayApiKey') : System.getenv('BINTRAY_API_KEY')

  configurations = ['archives']
  pkg {
    repo = BINTRAY_REPO
    name = BINTRAY_NAME
    desc = POM_LIBRARY_DESCRIPTION
    websiteUrl = POM_SITE_URL
    vcsUrl = POM_GIT_URL
    licenses = readArray(POM_ALL_LICENSES)
    labels = readArray(POM_ALL_LABELS)
    publish = true
    publicDownloadNumbers = true
    version {
      desc = POM_LIBRARY_DESCRIPTION
      gpg {
        sign = true // Determines whether to GPG sign the files. The default is false
        passphrase = project.hasProperty('bintrayGpgPassword') ? project.property('bintrayGpgPassword') :
            System.getenv('BINTRAY_GPG_PASSWORD')
        //Optional. The passphrase for GPG signing'
      }
    }
  }
}

String[] readArray(final String property) {
  final String[] props = property.split(',')
  props.each { defaultTasks.add(it.trim()) }
  return props;
}
