apply plugin: "org.springframework.boot"
apply from: "https://dl.bintray.com/scalding/generic/waroverlay.gradle"

def currentBranch = "master"
def currentVersion = "${project.'cas.version'}"
if (!currentVersion.contains("RC")) {
    def matcher = currentVersion =~ /(\d+\.\d+\.).+/
    if (matcher.find()) {
        currentBranch = matcher.group(1) + "x"
    }
}
logger.info "Using CAS branch [${currentBranch}] to handle overrides"
apply from: "https://raw.githubusercontent.com/apereo/cas/${currentBranch}/gradle/overrides.gradle"

apply plugin: 'eclipse'
apply plugin: 'idea'

eclipse {
    classpath {
       downloadSources = true
       downloadJavadoc = true
    }
}

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

springBoot {
    mainClass = "org.springframework.boot.loader.WarLauncher"
}

bootRepackage {
    mainClass = "org.apereo.cas.web.CasWebApplication"
    executable = false
    excludeDevtools = false
}

bootRun {
    addResources = true
    classpath = sourceSets.main.compileClasspath
}

repositories {
    mavenLocal()
    jcenter()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'https://build.shibboleth.net/nexus/content/repositories/releases/' }
}

dependencies {
    compile "org.apereo.cas:cas-server-webapp-tomcat:${project.'cas.version'}@war"
    if (!project.hasProperty('bootiful')) {
        // Other dependencies may be listed here...
    } else {
        println "Running CAS in Bootiful mode; all dependencies except the CAS web application are ignored."
    }
}

task copyConfig(type: Copy, description: "Copy CAS configuration over to /etc/cas/config") {
    doLast {
        from "${project.rootDir}/etc/cas/config"
        into '/etc/cas/config'

        println "Copied configuration files into /etc/cas/config"
    }
}

war {
    dependsOn copyConfig
    baseName 'cas'
    includeWarJars = true
    entryCompression = ZipEntryCompression.STORED
}

task explodeWar(type: Copy, group: "build", description: "Explodes the cas.war") {
    doLast {
        from zipTree(project.war.outputs.files.singleFile)
        into "${buildDir}/cas"
        println "CAS web application artifact exploded into [cas/build/cas]"
    }
}

task run(group: "build", description: "Run the CAS web application in embedded container mode") {
    dependsOn build
    doLast {
        def casRunArgs = Arrays.asList(project.'cas.run.jvmArgs'.split(" "))
        javaexec {
            main = "-jar";
            jvmArgs = casRunArgs
            args = ["build/libs/cas.war"]

            logger.info "Started ${commandLine}"
        }
    }
}

task debug(group: "build", description: "Debug the CAS web application in embedded mode on port 5005") {
    dependsOn build
    doLast {
        def casDebugArgs = Arrays.asList(project.'cas.debug.jvmArgs'.split(" "))

        javaexec {
            main = "-jar";
            jvmArgs = casDebugArgs
            args = ["build/libs/cas.war"]

            logger.info "Started ${commandLine}"
        }
    }
}