buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}


allprojects {
    repositories {
        mavenCentral()
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
}


apply plugin: "kotlin"

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    compile 'org.reflections:reflections:0.9.9-RC1'
}

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}


subprojects {
    buildscript {
        repositories {
            mavenCentral()
            maven {
                url 'http://oss.sonatype.org/content/repositories/snapshots'
            }
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        }
    }

    apply plugin: 'kotlin'

    repositories {
        mavenCentral()
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    }
    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    }
}


// Code generation

task(genkalium, dependsOn: jar, type: JavaExec) {
    main = 'genkalium.GenkaliumPackage'
    classpath = sourceSets.main.runtimeClasspath
}

task(genproperties, dependsOn: jar, type: JavaExec) {
    main = 'genproperties.GenpropertiesPackage'
    classpath = sourceSets.main.runtimeClasspath
}

task(genpropabbrs, dependsOn: jar, type: JavaExec) {
    main = 'genpropabbrs.GenpropabbrsPackage'
    classpath = sourceSets.main.runtimeClasspath
}

task wrapper(type: Wrapper) {
    gradleVersion = 1.11
    doLast() {
        def gradleOpts = "-Xmx1024m"
        def gradlew_sh = file("gradlew")
        def gradlew_bat = file("gradlew.bat")
        gradlew_sh.text = gradlew_sh.text.replace("DEFAULT_JVM_OPTS=",
                "GRADLE_OPTS=\"$gradleOpts \$GRADLE_OPTS\"\nDEFAULT_JVM_OPTS=")
        gradlew_bat.text = gradlew_bat.text.replace("set DEFAULT_JVM_OPTS=",
                "set GRADLE_OPTS=$gradleOpts %GRADLE_OPTS%\nset DEFAULT_JVM_OPTS=")

    }
}


// Distribution

// More information about the following code can be found in the wiki.
// TODO: I bet this could be simplified so much...

task distJar(type: Jar) {
    archiveName = "kotlinfx-${rootProject.version}.jar"
    from project(':kotlinfx-core').sourceSets.main.output.classesDir
}

// It is assumed that the KotlinFX-pages dir that is a clone of the gh-pages branch
// is a sibling of the KotlinFX project dir.
def ghpagesPath = "${projectDir.getParentFile()}/KotlinFX-pages"
def localRepoPath = ghpagesPath+"/repository"

project(":kotlinfx-core") {
    apply plugin: 'maven'
    group = rootProject.group
    version = rootProject.version
    archivesBaseName = 'kotlinfx'
    uploadArchives {
        repositories.mavenDeployer {
            repository(url: "file:///" + localRepoPath)
            pom.version = "${rootProject.version}"
            pom.artifactId = 'kotlinfx'
            pom.groupId = "${rootProject.group}"
        }
    }

// I just want to do it like this >:(!
//task publishgh {
//    workingDir ghpagesPath
//    uploadArchives
//    commandLine './update-directory-index.sh'
//    commandLine 'git add . && git commit --amend -m "Upload binaries" && git push -f'
//}

    task updateIndex(type:Exec, dependsOn: uploadArchives) {
        workingDir ghpagesPath
        commandLine './update-directory-index.sh'
    }

    task publishgh0(type:Exec, dependsOn: updateIndex) {
        workingDir ghpagesPath
        commandLine 'git', 'add', '--all'
    }

    task publishgh1(type:Exec, dependsOn: publishgh0) {
        workingDir ghpagesPath
        commandLine 'git', 'commit', '--amend', '-m', '"Upload binaries"'
    }

    task publishgh(type:Exec, dependsOn: publishgh1) {
        workingDir ghpagesPath
        commandLine 'git', 'push', '-f'
    }
}


