/**
 * Invoke this build file with Gradle's -b switch for POM- or
 * Maven-publishing related tasks.
 *
 * Unfortunately, it is impractical to encapsulate all of the needed processing
 * here, so there is some tight coupling between this file and "build.gradle".
 * (This file loads/applies "build.gradle").
 *
 * The tasks pulled in by this file require several properties to be set,
 * for populating a POM file and for specifying (and optionally authenticating
 * to a Maven repository server).
 *
 *****************************************************************************
 * Instructions for doing a full, public release:
 *
 * Right after doing the build of the zip distributable with JDK 6 and
 * javac.debug of false, publish the same exact jar files that are in the zip.
 * Use -x switches to prevent rebuilds of the jars which have been checksummed
 * by 'build-dist' not by 'mvn-pub'.
 *
 * <PRE><CODE>
 *     gradlew -x sqltool -x osgi -b mvn-pub.gradle
 * </CODE></PRE>
 *
 * Now, for each alternative, adjust environment and build properties, like
 * by setting env var JAVA_HOME or build property 'build.debug', and run
 *
 * <PRE><CODE>
 *   gradlew -Pclassifier=NEW_CLASSIFIER -b mvn-pub.gradle clean-all uploadArchives
 * </CODE></PRE>
 * The clean-all will wipe .../doc, but the .../doc and everything else from
 * your original dist build will be retained in .../dist/*.zip.
 *****************************************************************************
 * For snapshot builds, just publish jar-pairs individually, exactly as
 * described in the 2nd part of "Instructions for doing a full, public
 * release:" above, beginning  with "Now, for each alternative".
 * (In this case, clean-all really will blow away your only docs, so either
 * back them up or regenerate them afterwards).
 *
 * @author Blaine Simpson  (blaine dot simpson at admc dot com)
 */

buildscript { dependencies {
    // This 'buildscript' only here because with current version of Gradle,
    // 'buildscript' is only honored from the top-level build file executed.
    // When we "apply" 'build.gradle, the 'buildscript' there will be ignored.
    classpath fileTree(dir: '.', include: 'gradle-javaPropFile-plugin-*.jar')
    classpath fileTree(dir: '.', include: 'gradle-ivyxml-plugin-*.jar')
} }

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

signing {
    required { project.hasProperty('signing.keyId') }
    sign configurations.archives
}

project.ext.mvnBuild = true
apply from: 'build.gradle'
defaultTasks 'uploadArchives'

configurations {
    deployJars
}

if (project.hasProperty('mavenRepository.dest.url')) {
    def url = project.property('mavenRepository.dest.url')
    assert url != null:
        '''Property 'mavenRepository.dest.url' may not be null
'''
    assert url instanceof URL || url instanceof String:
        ('''Property 'mavenRepository.dest.url' must be of type URL.  Is a '''
                + url.class.name + '\n')
    if (url instanceof String && url.startsWith('scp')) {
        repositories { mavenCentral() }
        dependencies {
            deployJars 'org.apache.maven.wagon:wagon-ssh:latest.milestone'
        }
    }
}

// description attribute of project is always set, but may be null
if (!project.hasProperty('description') ||
        project.property('description') == null)
    project.description = 'HSQLDB - Lightweight 100% Java SQL Database Engine'
if (!project.hasProperty('title')) project.ext.set('title', 'HyperSQL Database')

uploadArchives {
    def authMap = [:]
    if (project.hasProperty('mavenRepository.dest.username'))
        authMap['userName'] = project.property('mavenRepository.dest.username')
    if (project.hasProperty('mavenRepository.dest.password'))
        authMap['password'] = project.property('mavenRepository.dest.password')
    if (project.hasProperty('mavenRepository.dest.privateKey'))
        authMap['privateKey'] =
                project.property('mavenRepository.dest.privateKey')

    repositories.mavenDeployer {
        configuration = configurations.deployJars
        if (project.hasProperty('signing.keyId')) beforeDeployment {
            MavenDeployment deployment -> signing.signPom(deployment)
        }
        if (project.hasProperty('mavenRepository.dest.url'))
            repository(url: project.property('mavenRepository.dest.url')) {
                authentication(authMap)
            }
        [
            addFilter('hsqldb') {artifact, file -> artifact.name == 'hsqldb'},
            addFilter('sqltool') {artifact, file -> artifact.name == 'sqltool'}
        ].each { pom -> pom.project {
            // The hasProperty tests are necessary even for required variables
            // because this code is evaluated even if the uploadArchives task
            // will never be executed.
            if (project.hasProperty('title')) name project.property('title')
            if (project.hasProperty('description'))
                description project.property('description')
            if (project.hasProperty('url')) url project.property('url')
            if (project.hasProperty('inceptionYear'))
                inceptionYear project.property('inceptionYear')
            scm {
                if (project.hasProperty('scm.url'))
                    url project.property('scm.url')
                if (project.hasProperty('scm.connection'))
                    connection project.property('scm.connection')
                if (project.hasProperty('scm.tag'))
                    tag project.property('scm.tag')
            }
            if (project.hasProperty('org.name')
                    || project.hasProperty('org.url')) organization {
                if (project.hasProperty('org.name'))
                    name project.property('org.name')
                if (project.hasProperty('org.url'))
                    url project.property('org.url')
            }
            licenses {
                license {
                    name 'HSQLDB License, a BSD open source license'
                    url 'http://hsqldb.org/web/hsqlLicense.html'
                    distribution 'repo'
                }
            }
            if (project.hasProperty('developer.name')
                    || project.hasProperty('developer.email')) developers {
                developer {
                    if (project.hasProperty('developer.id'))
                        id project.property('developer.id')
                    if (project.hasProperty('developer.name'))
                        name project.property('developer.name')
                    if (project.hasProperty('developer.email'))
                        email project.property('developer.email')
                }
            }
        } }
    }
}

uploadArchives { doFirst {
    ['title', 'group', 'mavenRepository.dest.url', 'scm.connection',
            'scm.url', 'url', 'inceptionYear', 'description'].each {
        assert (project.hasProperty(it) && project.property(it) != null
                && project.property(it) != '') :
            """Property '$it' is required by 'uploadArchives' task.
"""
    }
    ['scm.url', 'url', 'org.url'].each {
        assert project.property(it) != null:
            """Property '$it' may not be null
"""
        assert (!project.hasProperty(it)
                || project.property(it) instanceof URL):
            ("""Property '$it' must be of type URL.  Is a """
                    + project.property(it).class.name + '\n')
    }
    ['mavenRepository.dest.url'].each {
        assert project.property(it) != null:
            """Property '$it' may not be null
"""
        assert (!project.hasProperty(it)
                || project.property(it) instanceof URL
                || project.property(it) instanceof String):
            ("""Property '$it' must be of type URL or String.  Is a """
                    + project.property(it).class.name + '\n')
    }
} }

// This is because jar.inputs is read-only
// Funny name is work-around for Gradle sequencing bug
task zJarDep << {
    if (jar.archivePath.exists() && jar.archivePath.lastModified()
            < file('../lib/hsqldb-osgi.jar').lastModified()) {
        println "Deleting $jar.archivePath.absolutePath"
        jar.archivePath.delete()
    }
}

jar << {
    File srcFile = file('../lib/hsqldb-osgi.jar')
    jar.archivePath.setBytes(srcFile.bytes)
}

jar.dependsOn = [osgi, zJarDep]
if (project.hasProperty('classifier')) jar.classifier = project.classifier

clean.dependsOn = [prohibitionMap['clean']]

// This is because jar.inputs is read-only
// Funny name is work-around for Gradle sequencing bug
task zSqltoolJarDep << {
    if (sqltoolJar.archivePath.exists() && sqltoolJar.archivePath.lastModified()
            < file('../lib/sqltool.jar').lastModified()) {
        println "Deleting $sqltoolJar.archivePath.absolutePath"
        sqltoolJar.archivePath.delete()
    }
}

task sqltoolJar(dependsOn: [sqltool, zSqltoolJarDep], type: Jar) {
    baseName = 'sqltool'
    if (project.hasProperty('classifier')) classifier = project.classifier
}

sqltoolJar << {
    File srcFile = file('../lib/sqltool.jar')
    sqltoolJar.archivePath.setBytes(srcFile.bytes)
}

task sourcesJar(type: Jar, dependsOn:classes) {
     classifier = 'sources'
     //from sourceSets.main.allSource
     from fileTree(dir: '../src')
}
sourcesJar.description = 'Build sources jar file'

task javadocJar(type: Jar, dependsOn:javadoc) {
     classifier = 'javadoc'
     //from javadoc.destinationDir
     from fileTree(dir: '../doc/apidocs')
}
javadocJar.description = 'Build javadoc jar file'

task sqltoolSourcesJar(type: Jar, dependsOn:classes) {
    baseName = 'sqltool'
    classifier = 'sources'
    //from sourceSets.main.allSource
    from fileTree(dir: '../src')
}
sqltoolSourcesJar.description = 'Build Sqltool sources jar file'

task sqltoolJavadocJar(type: Jar, dependsOn:javadoc) {
    baseName = 'sqltool'
    classifier = 'javadoc'
    //from javadoc.destinationDir
    from fileTree(dir: '../doc/apidocs')
}
sqltoolJavadocJar.description = 'Build Sqltool javadoc jar file'
 
artifacts { archives sqltoolJar }

// add javadoc/source jar tasks as artifacts, only if no classifier specified
if (!project.hasProperty('classifier')) artifacts {
    archives sourcesJar
    archives javadocJar
    archives sqltoolSourcesJar
    archives sqltoolJavadocJar
}
