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


group 'com.backendless'
version '7.0.0-beta'
archivesBaseName='backendless'



sourceCompatibility = 1.8

def getMavenSettingsCredentials = {
    String userHome = System.getProperty( "user.home" );
    File mavenSettings = new File(userHome, ".m2/settings.xml")
    def xmlSlurper = new XmlSlurper()
    def output = xmlSlurper.parse(mavenSettings)
    return output.servers."server"
}

[compileJava, compileTestJava]*.options*.compilerArgs = ['-g', '-parameters']

if (JavaVersion.current().isJava8Compatible())
{
    allprojects {
        tasks.withType( Javadoc ) {
            options.addStringOption( 'Xdoclint:none', '-quiet' )
        }
    }
}

task logInfo {
    doFirst {
        println getMavenSettingsCredentials()
    }
}

task javadocJar(type: Jar) {
    classifier = 'javadoc'
    from javadoc
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives javadocJar, sourcesJar
}

//signing {
//    sign configurations.archives
//}

uploadArchives {
    repositories {
        mavenDeployer {

            def ossrhUsername=''
            def ossrhPassword=''

//            Properties props = new Properties()
//            props.load(new FileInputStream("./ossrh.properties"))
//
//            ossrhUsername=props.getProperty( "user.name" )
//            ossrhPassword=props.getProperty( "user.password" )

            pom.whenConfigured {
                p -> p.dependencies = p.dependencies.findAll {
                    dep -> ( dep.artifactId != "commons" && dep.artifactId != "weborbclient" )
                }
            }

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

            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                name 'Backendless android sdk'
                packaging 'jar'
                // optionally artifactId can be defined here
                description 'Android SDK used by developers to provide Backendless API in apps.'
                url 'http://backendless.com'

                scm {
                    connection 'scm:git:https://github.com/Backendless/Android-SDK.git'
                    developerConnection 'scm:git:https://github.com/Backendless/Android-SDK.git'
                    url 'https://github.com/Backendless/Android-SDK.git'
                }

                licenses {
                    license {
                        name 'The MIT License (MIT)'
                        url 'https://opensource.org/licenses/MIT'
                    }
                }

                developers {
                    developer {
                        id 'backendless'
                        name 'backendless'
                        email 'info@backednless.com'
                    }
                }
            }
        }
    }
}

def getCredentials = {
    def entries = getMavenSettingsCredentials()
    for( entry in entries )
    {
        if( entry."id".text() == "backendless_public" )
        {
            return [username: entry.username.text(), password: entry.password.text()]
        }
    }

    return [username: "dummy", password: "dummy"]
}

def creds = getCredentials()

sourceSets {
    main {
        java {
            srcDir './src'
        }
    }
}

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        credentials {
            username creds.username
            password creds.password
        }
        url "https://maven.backendless.com/repository/backendless_public"
    }
    flatDir {
        dirs 'libs'
    }
    jcenter()
    google()
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            credentials {
                username creds.username
                password creds.password
            }
            url "https://maven.backendless.com/repository/backendless_public"
        }
    }
}

configurations {
    provided
    compile.extendsFrom provided
}

def libsToPackage() {

        def artifactsToPackage =
                [
                    ["com.backendless", "commons"],
                    ["weborb", "weborbclient"]
                ]

        def libsToPackage = ["mediaLib.jar"]

        return configurations.compileOnly.files { pkg ->
            def count = artifactsToPackage.stream()
                .filter( { ar ->
                    if (pkg.group.equals( ar[ 0 ] ) && pkg.name.equals( ar[ 1 ] ) )
                        return true

                    if (pkg.group == null && pkg.version == null)
                    {
                        def fileName = pkg.resolve().iterator().next().name
                        return libsToPackage.contains( fileName )
                    }
                    return false
                } )
                .count()
            return count > 0
        }
}

jar {
    from {
        libsToPackage().collect {
            it.isDirectory() ? it : zipTree( it )
        }
    }
}

task copySampleJars {
    doLast {
        copyT( jar.archivePath, projectDir.path + '/samples/DataService/tododemo/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/EndlessMatchMaker/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/EndlessTagging/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/MessagingService/pubsubdemo/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/MessagingService/pushdemo/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/UserService/FacebookSDKSamples/login/libs', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/UserService/rolesdemo/libs/', jar.getArchiveName() )
        copyT( jar.archivePath, projectDir.path + '/samples/UserService/userservicedemo/libs', jar.getArchiveName() )
    }
}

task copyJarToOut {
    doLast {
        copyT( jar.archivePath, projectDir.path + '/out', jar.getArchiveName() )
    }
}

def copyT(fromPath, toPath, archName)
{
   copy{
      from fromPath into toPath
      rename(archName, 'backendless.jar')
   }
}

configurations.all {
    // check for updates every build
    resolutionStrategy.cacheChangingModulesFor 1, 'seconds'
}

dependencies {
    provided fileTree(dir: 'libs', include: 'android-26.jar')
    provided fileTree(dir: 'libs', include: 'support-compat-26.1.0.jar')
    provided fileTree(dir: 'libs', include: 'support-fragment-26.1.0.jar')
    provided fileTree(dir: 'libs', include: 'support-annotations-26.1.0.jar')
    provided fileTree(dir: 'libs', include: 'firebase-iid-17.0.1.jar')
    provided fileTree(dir: 'libs', include: 'firebase-messaging-17.3.1.jar')
    provided fileTree(dir: 'libs', include: 'play-services-basement-15.0.1.jar')
    provided fileTree(dir: 'libs', include: 'play-services-tasks-15.0.1.jar')
    provided fileTree(dir: 'libs', include: 'FacebookSDK.jar')
    provided fileTree(dir: 'libs', include: 'maps.jar')
    compileOnly fileTree(dir: 'libs', include: 'mediaLib.jar')
    compileOnly( group: "com.backendless", name: "commons", version: "6.3.0", changing: true, ) {
        exclude group: 'com.fasterxml.jackson.core'
    }
    compileOnly( group: "weborb", name: "weborbclient", version: "5.2.0.4", changing: true)

    compileOnly ('io.socket:socket.io-client:1.0.0') {
//       excluding org.json which is provided by Android
      exclude group: 'org.json', module: 'json'
    }

    // https://projectlombok.org
    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok:1.18.22'
}
