buildscript {
    ext {
        asmAsmVersion = "9.6" // https://gitlab.ow2.org/asm/asm/-/tags
    }
    configurations {
        classpath {
            resolutionStrategy {
                //in order to handle jackson's higher release version in shadow, this needs to be upgraded to latest.
                force(group: "org.ow2.asm", name: "asm", version: asmAsmVersion)
                force(group: "org.ow2.asm", name: "asm-commons", version: asmAsmVersion)
            }
        }
    }
}
plugins {
    id 'com.github.johnrengelman.shadow' version '8.1.1'
    id "com.jfrog.artifactory" version "5.2.2"
}

println """
*******************************************
 You are building CraftBook! 
 If you encounter trouble:
 1) Read CONTRIBUTING.md if you haven't yet
 2) Try running 'build' in a separate Gradle run
 3) Use gradlew and not gradle
 4) If you still need help, ask on IRC! irc.esper.net #sk89q
 Output files will be in [subproject]/build/libs
*******************************************
"""

if (!project.hasProperty("artifactory_contextUrl")) ext.artifactory_contextUrl = ""
if (!project.hasProperty("artifactory_user")) ext.artifactory_user = ""
if (!project.hasProperty("artifactory_password")) ext.artifactory_password = ""

if (!project.hasProperty("gitCommitHash")) {
    try {
        def repo = org.ajoberstar.grgit.Grgit.open(project.file('.'))
        ext.gitCommitHash = repo.head().abbreviatedId
    } catch (Exception e) {
        ext.gitCommitHash = "no_git_id"
    }
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'maven-publish'

    group = pluginGroup
    version = pluginVersion
    ext.internalVersion = version + ";" + gitCommitHash

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(21)
        }
        withJavadocJar()
        withSourcesJar()
    }

    tasks.test {
        // FIXME: Obviously, this isn't what we want
        enabled = false
    }

    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            name = 'spigotmc-repo'
            url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
        }
        maven {
            name = 'enginehub-repo'
            url = 'https://maven.enginehub.org/repo/'
        }
        maven {
            name = 'md_5-rep'
            url = 'https://repo.md-5.net/content/repositories/releases/'
        }
        maven {
            name = 'Vault Repo'
            url = 'https://jitpack.io'
        }
        maven {
			name = 'bstats repo'
            url = 'https://repo.codemc.org/repository/maven-public'
		}
        maven {
            name "papermc"
            url "https://repo.papermc.io/repository/maven-public/"
        }
    }

    dependencies {
        compileOnly 'org.spigotmc:spigot-api:1.21.5-R0.1-SNAPSHOT'
        compileOnly 'com.sk89q.worldedit:worldedit-core:7.3.12-SNAPSHOT'
        compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.3.12-SNAPSHOT'
        compileOnly 'com.sk89q.worldguard:worldguard-core:7.0.14-SNAPSHOT'
        compileOnly 'com.sk89q.worldguard:worldguard-bukkit:7.0.14-SNAPSHOT'
        compileOnly 'com.github.MilkBowl:VaultAPI:1.7'
        compileOnly 'net.dmulloy2:ProtocolLib:5.4.0'
        compileOnly 'fr.neatmonster:nocheatplus:3.15.1-RC'

        implementation 'com.sk89q:squirrelid:0.1.0'
        implementation "io.papermc:paperlib:1.0.7"
        implementation 'org.bstats:bstats-bukkit:2.2.1'

        testImplementation 'org.spigotmc:spigot-api:1.21.3-R0.1-SNAPSHOT'
        testImplementation 'com.sk89q.worldedit:worldedit-core:7.3.8-SNAPSHOT'
        testImplementation group: 'junit', name: 'junit', version: '4.12'
        testImplementation group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.4'
        testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.4'
    }

    if (JavaVersion.current().isJava8Compatible()) {
        // Java 8 turns on doclint which we fail
        tasks.withType(Javadoc) {
            options.addStringOption('Xdoclint:none', '-quiet')
        }
    }

    jar {
        manifest {
            attributes("CraftBook-Version": pluginVersion)
        }
    }

    shadowJar {
        archiveClassifier.set('dist')
        dependencies {
            include(dependency('io.papermc:paperlib'))
            include(dependency('com.sk89q:squirrelid'))
            include(dependency("org.bstats:"))
        }
        relocate 'org.bstats', 'com.sk89q.craftbook.bukkit.bstats'
        relocate 'com.sk89q.squirrelid', 'com.sk89q.craftbook.util.profile'
        relocate 'io.papermc.lib', 'com.sk89q.craftbook.bukkit.paperlib'
        exclude 'GradleStart**'
        exclude '.cache'
        exclude 'LICENSE*'
    }

    build.dependsOn(shadowJar)

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

    artifactory {
        contextUrl = "${artifactory_contextUrl}"
        publish {
            repository {
                repoKey = project.version.contains("SNAPSHOT") ? 'libs-snapshot-local' : 'libs-release-local'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
            }

            defaults {
                publications("mavenJava")
            }
        }
    }

    processResources {
        duplicatesStrategy DuplicatesStrategy.INCLUDE

        from (sourceSets.main.resources.srcDirs) {
            expand 'internalVersion': project.internalVersion
            include 'plugin.yml'
        }
    }
}
