apply plugin: "com.badlogicgames.gdx.gdx-jnigen"

file("jni").mkdir()

jnigen{
    def fdir = "freetype-2.10.4"

    sharedLibName = "arc-freetype"
    temporaryDir = file("build/target/native").absolutePath
    libsDir = file("libs").absolutePath

    all{
        headerDirs +=  (String[])["$fdir/include"]
        cIncludes = [
            //BASE
            "$fdir/src/base/ftsystem.c", "$fdir/src/base/ftinit.c", "$fdir/src/base/ftdebug.c",
            "$fdir/src/base/ftbase.c", "$fdir/src/base/ftbbox.c", "$fdir/src/base/ftglyph.c",
            "$fdir/src/base/ftbitmap.c", "$fdir/src/base/ftstroke.c",

            //DRIVERS
            "$fdir/src/truetype/truetype.c", "$fdir/src/sfnt/sfnt.c",

            //RASTERIZERS
            "$fdir/src/smooth/smooth.c",

            //AUX
            "$fdir/src/autofit/autofit.c", "$fdir/src/gzip/ftgzip.c",
            "$fdir/src/psnames/psnames.c"
        ]

        cFlags += " -DFT2_BUILD_LIBRARY"
        cppFlags += " -DFT2_BUILD_LIBRARY"
    }
    add(Windows, x32)
    add(Windows, x64)
    add(Linux, x64)
    add(MacOsX, x64)
    add(MacOsX, x64, ARM){
        cFlags = cppFlags = cppFlags.replace("x86_64", "arm64")
        linkerFlags = linkerFlags.replace("x86_64", "arm64")
    }
    add(Android)
    add(IOS)
}

task preJni{

    doFirst{
        copy{
            from "$rootDir/arc-core/build/classes/java/main"
            into "$rootDir/extensions/freetype/build/classes/java/main"
            include "**"
        }

        if(!file("$project.rootDir/extensions/freetype/jni/freetype-2.10.4").exists()){
            println "Fetching freetype source..."

            def root = "$project.rootDir/extensions/freetype"
            new File("$root/jni").mkdir()
            def script = "wget -c https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz -O $root/jni/freetype.tar.gz\n" +
                    "tar -xf $root/jni/freetype.tar.gz -C $root/jni"
            def scriptFile = new File("$buildDir/getfreetype")
            scriptFile.parentFile.mkdirs()
            scriptFile.text = script
            "bash $scriptFile.absolutePath".execute().waitFor()

            //overwrite config to use only certain modules.
            file("$root/jni/freetype-2.10.4/include/freetype/config/ftmodule.h").text =
            """
            FT_USE_MODULE( FT_Module_Class, autofit_module_class )
            FT_USE_MODULE( FT_Driver_ClassRec, tt_driver_class )
            FT_USE_MODULE( FT_Module_Class, psnames_module_class )
            FT_USE_MODULE( FT_Module_Class, sfnt_module_class )
            FT_USE_MODULE( FT_Renderer_Class, ft_smooth_renderer_class )
            """
        }
    }
}

//copies files into the right locations
task postJni{
    doLast{
        copy{
            from "libs/linux64", "libs/windows32", "libs/windows64", "libs/macosx64"
            into "../../natives/natives-freetype-desktop/libs"
            include "**"
        }

        ["arm64-v8a", "x86", "x86_64", "armeabi-v7a"].each{ p ->
            copy{
                from "libs/$p"
                into "../../natives/natives-freetype-android/libs/$p/"
                include "**"
            }
        }

        copy{
            from "libs/ios32/libarc-freetype.a"
            into "../../natives/natives-freetype-ios/libs/"
            include "**"
        }
    }
}

preJni.dependsOn "compileJava"
jnigenBuild.finalizedBy postJni

getTasksByName("jnigen", true).each{
    it.dependsOn preJni
}