buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.2.2'
    }
}
plugins {
    id 'org.gradle.crypto.checksum' version '1.4.0'
}

import org.gradle.crypto.checksum.Checksum
apply plugin: 'com.android.application'

repositories {
    google()
    mavenCentral()
}


dependencies {
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'org.hamcrest:hamcrest-all:1.3'
}

android {
    compileSdk 34
    buildToolsVersion "34.0.0"
    ndkVersion "26.1.10909125"
    namespace "com.reecedunn.espeak"

    buildFeatures {
        buildConfig = true
    }

    defaultConfig {
        minSdk 21
        targetSdk 33
        versionCode 22
        versionName "1.52.0"

        externalNativeBuild {
            cmake {
                arguments "-DUSE_ASYNC:BOOL=OFF", "-DUSE_MBROLA:BOOL=OFF"
                targets "ttsespeak", "espeak-data"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "jni/CMakeLists.txt"
            version "3.22.1"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
        }
        androidTest.setRoot('eSpeakTests')
        androidTest {
            java.srcDirs = ['eSpeakTests/src']
        }
    }

    lint {
        abortOnError false
    }
}

tasks.register('checkData') {
    doFirst {
        assert file("../espeak-ng-data/en_dict").exists()
        assert file("../espeak-ng-data/intonations").exists()
        assert file("../espeak-ng-data/phondata").exists()
        assert file("../espeak-ng-data/phondata-manifest").exists()
        assert file("../espeak-ng-data/phonindex").exists()
        assert file("../espeak-ng-data/phontab").exists()
    }
}

tasks.register('createDataArchive', Zip) {
    preserveFileTimestamps = false
    reproducibleFileOrder = true
    archiveFileName = "espeakdata.zip"
    destinationDirectory = file("res/raw")

    from("build/generated/espeak-ng-data/") {
        into "espeak-ng-data"
    }
}

tasks.register('createDataHash', Checksum) {
    dependsOn tasks.createDataArchive
    checksumAlgorithm.set(Checksum.Algorithm.SHA256)
    inputFiles.setFrom(file("./res/raw/espeakdata.zip"))
    outputDirectory.set(layout.buildDirectory.dir('intermediates/datahash'))
}

tasks.register('createDataVersion', Copy) {
    dependsOn tasks.createDataHash
    from layout.buildDirectory.file('intermediates/datahash/espeakdata.zip.sha256')
    rename { return 'espeakdata_version' }
    into file("./res/raw")
}

tasks.whenTaskAdded { task ->
    if (task.name == 'assembleDebug' || task.name == 'assembleRelease') {
        task.dependsOn createDataVersion
    }
}

createDataVersion.mustRunAfter 'externalNativeBuildDebug'
createDataVersion.mustRunAfter 'externalNativeBuildRelease'

project.afterEvaluate {
    tasks.checkData.dependsOn(externalNativeBuildDebug)
    tasks.createDataArchive.dependsOn(externalNativeBuildDebug)
    tasks.named('mapDebugSourceSetPaths') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
    tasks.named('mergeDebugResources') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
    tasks.named('packageDebugResources') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
    tasks.named('mapReleaseSourceSetPaths') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
    tasks.named('mergeReleaseResources') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
    tasks.named('packageReleaseResources') {
        dependsOn ':createDataVersion', ':createDataArchive'
    }
}
