/*
 * (C) Copyright IBM Deutschland GmbH 2021
 * (C) Copyright IBM Corp. 2021
 */

class Config {
    def input
    def output = null
    def fileName = "generated_strings.xml"
}

ext.twineStrings = { args ->
    def config = new Config(args)
    if (config.output == null) {
        config.output = "${project.projectDir}/src/main/res"
    }

    task generateStrings {
        inputs.file(config.input)

        doFirst {
            // https://github.com/scelis/twine/issues/239
            exec {
                commandLine("twine", 'generate-localization-file',
                    config.input,
                    "${config.output}/values/${config.fileName}",
                    '--format', "android",
                    '--lang', "en",
                    '--developer-language', "en"
                )
            }
            exec {
                commandLine("twine", 'generate-localization-file',
                    config.input,
                    "${config.output}/values-de/${config.fileName}",
                    '--format', "android",
                    '--lang', "de",
                    '--developer-language', "en"
                )
            }
        }
        doLast {
            fileTree(dir: "${config.output}").matching {
                include "values*/${config.fileName}"
            }.each { file ->
                ant.replace(
                    file: file,
                    token: '<resources>',
                    value: '<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="UnusedResources">'
                )
            }
        }
    }
}
