/*
 * AndroWish gradle.build file modeled after custom_rules.xml for
 * setup/cleanup of the source tree and to run the NDK build tool
 * on the native components.
 *
 *    ./gradlew help                   some info
 *    ./gradlew assembleDebug          build debug APK
 *    ./gradlew assembleRelease        build release APK
 *    ./gradlew clean                  clean tree
 *
 * Output goes into build/outputs/apk e.g. as AndroWish-debug.apk
 */

/*
 * These are needed since we deal with ndk-build ourselves.
 */
project.gradle.startParameter.excludedTaskNames.add('compileDebugNdk')
project.gradle.startParameter.excludedTaskNames.add('compileReleaseNdk')

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
    }
}

apply plugin: 'com.android.application'

android {
    lintOptions {
        abortOnError false
    }

    compileSdkVersion 16
    buildToolsVersion "25.0.3"

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 15
        versionCode 1
        versionName "1.0"
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
            jni.srcDirs = ['']
        }

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

    if (new File("ant.properties").exists()) {
        Properties props = new Properties()
        props.load(new FileInputStream(file("ant.properties")))
        if (props['key.store'] != null &&
            new File(props['key.store']).exists()) {
            signingConfigs {
                release {
                    storeFile file(props['key.store'])
                    storePassword props['key.store.password']
                    keyAlias props['key.alias']
                    keyPassword props['key.alias.password']
                }
            }
        }
    }

    buildTypes {
        release {
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            if (signingConfigs.hasProperty('release')) {
                signingConfig signingConfigs.release
            }
        }
        debug {
            debuggable true
        }
    }

    /*
     * Prepare source tree for ndk-build:
     *  - copy files for Windows platforms
     *  - symlink directories for POSIX platforms
     */
    preBuild.doLast {
        if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
            println "Copy selected jni sources"
            ant.copy(todir: "${projectDir}/jni/tcl-pkgs") {
                fileset(dir: "${projectDir}/jni/tcl/pkgs")
            }
            ant.copy(todir: "${projectDir}/assets/tcl8.6") {
                fileset(dir: "${projectDir}/jni/tcl/library")
            }
            ant.copy(todir: "${projectDir}/assets/sdl2tk8.6") {
                fileset(dir: "${projectDir}/jni/sdl2tk/library")
            }
        } else {
            println "(Re)create symlinks"
            file("jni/tcl-pkgs").delete()
            file("jni/tcl/tcl").delete()
            file("assets/tcl8.6").delete()
            file("assets/sdl2tk8.6").delete()
            ant.symlink(resource: "${projectDir}/jni/tcl/pkgs", link: "jni/tcl-pkgs", overwrite: true)
            ant.symlink(resource: ".", link: "jni/tcl/tcl", overwrite: true)
            ant.symlink(resource: "${projectDir}/jni/tcl/library", link: "assets/tcl8.6", overwrite: true)
            ant.symlink(resource: "${projectDir}/jni/sdl2tk/library", link: "assets/sdl2tk8.6", overwrite: true)
        }
    }

    /*
     * Cleanup ${projectDir}/jni
     */
    clean.doLast {
        println "Cleaning up (runs 'ndk-build clean' etc.)"
        if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
            exec {
                workingDir "${projectDir}/jni"
                commandLine "ndk-build.cmd"
                args "-k", "clean"
            }
            file("jni/tcl-pkgs").deleteDir()
            file("assets/tcl8.6").deleteDir()
            file("assets/sdl2tk8.6").deleteDir()
        } else {
            exec {
                workingDir "${projectDir}/jni"
                commandLine "ndk-build"
                args "-k", "clean"
            }
            file("jni/tcl-pkgs").delete()
            file("jni/tcl/tcl").delete()
            file("assets/tcl8.6").delete()
            file("assets/sdl2tk8.6").delete()
        }
    }

    /*
     * Invoke ndk-build in ${projectDir}/jni
     */
    preBuild.doLast {
        println "Perform ndk-build"
        if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
            exec {
                workingDir "${projectDir}/jni"
                commandLine "ndk-build.cmd"
            }
        } else {
            exec {
                workingDir "${projectDir}/jni"
                commandLine "ndk-build"
            }
        }
        ant.move(failonerror: false, file: "libs/armeabi/run", tofile: "libs/armeabi/librun.so")
        ant.move(failonerror: false, file: "libs/x86/run", tofile: "libs/x86/librun.so")
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

