plugins {
    // dependency versions (task: dependencyUpdates)
    //	https://github.com/ben-manes/gradle-versions-plugin
    id 'com.github.ben-manes.versions' version "$versionsPluginVersion"

    // maven publish plugin
    id 'maven-publish'

    // nexus publish plugin
    id 'io.github.gradle-nexus.publish-plugin' version "$nexusPublishPluginVersion"
}

allprojects {
    version = project.properties['releaseVersion'] ?: 'SNAPSHOT'
    group = 'org.libelektra'
    ext {
        isReleaseVersion = !project.version.contains("SNAPSHOT")
    }
}

nexusPublishing {
    repositories {
        sonatype {
            nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
            snapshotRepositoryUrl = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
            username = project.properties['sonatypeUsername'] ?: 'SONATYPE_USER_NAME_NOT_CONFIGURED'
            password = project.properties['sonatypePassword'] ?: 'SONATYPE_USER_PASSWORD_NOT_CONFIGURED'
        }
    }
}

// apply optional version information override
File versionSettings = new File(rootProject.projectDir, 'version-settings.gradle')
if (versionSettings.exists()) {
    apply from: versionSettings
}

// log task graph
gradle.taskGraph.whenReady { taskGraph ->
    logger.lifecycle("Building Elektra Java bindings version: ${rootProject.version}")
    logger.lifecycle("Is release version: ${rootProject.isReleaseVersion}")
    logger.lifecycle("Found task graph: $taskGraph")
    logger.lifecycle("Found ${taskGraph.allTasks.size()} tasks.")
    taskGraph.allTasks.forEach { task ->
        logger.lifecycle("$task")
        task.dependsOn.forEach { dep ->
            logger.lifecycle("  - $dep")
        }
        // task.inputs.files.each { input ->
        //     logger.lifecycle("  IF $input")
        // }

        // task.inputs.properties.each { input ->
        //     logger.lifecycle("  IP $input")
        // }

        // task.outputs.files.each { output ->
        //     logger.lifecycle("  OF $output")
        // }
    }
    if (rootProject.version == 'undefined' || rootProject.version == '0.9.4-1') {
        throw new RuntimeException("Version '${rootProject.version}' is blacklisted. see https://issues.sonatype.org/browse/OSSRH-66930")
    }
}

task printSourceFiles {
    doLast {
        allprojects.forEach { project ->
            if (project.hasProperty("sourceSets")) {
                project.sourceSets.each {
                    it.allSource.each {
                        println(it)
                    }
                }
            }
        }
    }
}
