import org.codehaus.groovy.ant.Groovy


def executeBenchmark(String filepath) {
    execute("benchy-execute -p ${processorCount} -cp multiverse-gamma/build/classes/main:multiverse-gamma/build/classes/test ${filepath}")
}

def executeDisplay(String filepath) {
    execute("benchy-display ${filepath}")
}

def execute(String s) {
    Process proc = s.execute()
    proc.consumeProcessErrorStream(System.err)
    proc.consumeProcessOutputStream(System.out)
    if (proc.waitFor() != 0) {
        throw new RuntimeException('exec failed')
    }
}

allprojects {
//  apply plugin: 'base'
    defaultTasks 'clean', 'install'
}

project(':multiverse-core') {
    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'project-reports'

    sourceCompatibility = 1.6
    version = '0.7.0'
    group = 'org.multiverse'

    //install.dependsOn ':build'

    def localMavenRepo = 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath

    configurations {
        testFixtures {
            extendsFrom testRuntime
        }
        deployerJars
    }

    javadoc {
        title = "Multiverse Core $version"
    }

    repositories {
        mavenRepo(url: localMavenRepo)
        mavenCentral()
        mavenRepo(url: 'http://download.java.net/maven/2/')
        mavenRepo(url: 'https://maven.atlassian.com/content/groups/public')
        mavenRepo(url: 'http://snapshots.repository.codehaus.org')
    }

    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.10'
        testCompile group: 'org.mockito', name: 'mockito-all', version: '1.9.0'
        testCompile group: 'org.benchy', name: 'benchy-driver', version: '0.1-SNAPSHOT'
        deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
        deployerJars group: 'org.apache.maven.wagon', name: 'wagon-webdav-jackrabbit', version: '2.2'
    }

    task benchmarkUncontendedMonoUpdate << {
        executeBenchmark("benchmarks/uncontended_mono_update_benchmark.groovy")
        executeDisplay("benchmarks/uncontended_mono_update_display.groovy")
    }

    task benchmarkCounter << {
        executeBenchmark("benchmarks/contended_counter_benchmark.groovy")
        executeDisplay("benchmarks/contended_counter_display.groovy")
    }

    task benchmarkMonoRead << {
        executeBenchmark("benchmarks/mono_read_benchmark.groovy")
        executeDisplay("benchmarks/mono_read_display.groovy")
    }

    task benchmarkAtomicGet << {
        executeBenchmark("benchmarks/atomic/atomic_get_benchmark.groovy")
        executeDisplay("benchmarks/atomic/atomic_get_display.groovy")
    }

    task benchmarkAtomicWeakGet << {
        executeBenchmark("benchmarks/atomic/atomic_weak_get_benchmark.groovy")
        executeDisplay("benchmarks/atomic/atomic_weak_get_display.groovy")
    }

    task benchmarkAtomicIncrement << {
        executeBenchmark("benchmarks/atomic/atomic_increment_benchmark.groovy")
        executeDisplay("benchmarks/atomic/atomic_increment_display.groovy")
    }

    task benchmarkAtomicLongIncrement << {
        executeBenchmark("benchmarks/atomic/atomic_long_increment_benchmark.groovy")
        executeDisplay("benchmarks/atomic/atomic_long_increment_display.groovy")
    }

    task benchmarkSimpleStack << {
        executeBenchmark("benchmarks/blocking/simple_stack_benchmark.groovy")
    }

    task benchmarkIntrinsicLockStack << {
        executeBenchmark("benchmarks/blocking/intrinsic_lock_stack_benchmark.groovy")
        //execute("benchy-display benchmarks/atomic_weak_get_display.groovy")
    }

    task benchmarkJucLockStack << {
        executeBenchmark("benchmarks/blocking/juc_lock_stack_benchmark.groovy")
        //execute("benchy-display benchmarks/atomic_weak_get_display.groovy")
    }

    task benchmarkOrecUpdate << {
        executeBenchmark("benchmarks/orec/update_benchmark.groovy")
        //execute("benchy-display benchmarks/atomic_weak_get_display.groovy")
    }

    task benchmarkOrecRead << {
        executeBenchmark("benchmarks/orec/update_benchmark.groovy")
        //execute("benchy-display benchmarks/atomic_weak_get_display.groovy")
    }

    task benchmarkBoxingOverhead << {
        executeBenchmark("benchmarks/overhead/boxing_overhead_benchmark.groovy")
        executeDisplay("benchmarks/overhead/boxing_overhead_display.groovy")
        executeDisplay("benchmarks/overhead/boxing_overhead_total_display.groovy")
    }

    task benchmark(dependsOn: [
            benchmarkUncontendedMonoUpdate,
            benchmarkCounter,
            benchmarkMonoRead,
            benchmarkAtomicGet,
            benchmarkAtomicWeakGet,
            benchmarkSimpleStack,
            benchmarkIntrinsicLockStack,
            benchmarkJucLockStack,
            benchmarkAtomicIncrement,
            benchmarkAtomicLongIncrement,
            benchmarkOrecUpdate,
            benchmarkOrecRead,
            benchmarkBoxingOverhead]) << {}

    test {
        exclude("**/*AbstractTest*")
        exclude("**/*Benchmark*")
        exclude("**/*StressTest*")
        exclude("**/*stressTest*")
        exclude("**/*LongTest*")
        exclude("**/*PerformanceTest*")
        exclude("**/*performanceTest*")
        exclude("**/*Driver*")
    }

    task shortintegrationtest(type: Test) {
        include("**/*StressTest*")
        include("**/*stressTest*")
        include("**/*LongTest*")
        include("**/*longTest*")
    }

    task integrationtest(type: Test) {
        include("**/*StressTest*")
        include("**/*stressTest*")
        include("**/*LongTest*")
        include("**/*longTest*")
    }

    shortintegrationtest {
        jvmArgs = ["-Dorg.multiverse.integrationtest.durationMs=10000", "-Dorg.multiverse.bugshaker.enabled=true"]
    }

    task deploy(dependsOn: ['clean', 'javadoc', 'install', 'uploadArchives']) << {
    }

    // custom tasks for creating source/javadoc jars
    task sourcesJar(type: Jar, dependsOn: classes) {
        classifier = 'sources'
        from sourceSets.main.allSource
    }

    task javadocJar(type: Jar, dependsOn: javadoc) {
        classifier = 'javadoc'
        from javadoc.destinationDir
    }

    // add javadoc/source jar tasks as artifacts
    artifacts {
        archives sourcesJar
        archives javadocJar
    }

    uploadArchives() {
        repositories.mavenDeployer {
            name = 'sshDeployer'
            uniqueVersion = false
            configuration = configurations.deployerJars
            repository(
                url: 'dav.codehaus.org/repository/multiverse/') {
                authentication(userName: "$codehaus_loginname", password: "$codehaus_password")
            }
            snapshotRepository(
                    url: 'dav.codehaus.org/snapshots.repository/multiverse/') {
                authentication(userName: "$codehaus_loginname", password: "$codehaus_password")
            }
        }
    }
}

project(':multiverse-site') {

    task clean << {
        ant.delete(dir: "${projectDir}/build")
    }

    task install(dependsOn: clean) << {
        def binding = new Binding()
        def parent = Groovy.class.getClassLoader()
        def loader = new GroovyClassLoader(parent)
        def shell = new GroovyShell(loader, binding)
        shell.evaluate(new java.io.File("multiverse-site/menu.groovy"))

        println "Copying charts"
        ant.copy(todir: "${projectDir}/build/site") {
            fileset(dir: "../charts")
        }

        println "Finished copying charts"
    }
}
