configurations {
    ant_tasks
}

dependencies {
    ant_tasks project(':freeplane_ant')
}

task format_translation(dependsOn: configurations.ant_tasks) {
    doLast {
        ant.taskdef(
            name: "format_translation",
            classname: "org.freeplane.ant.FormatTranslation",
            classpath: configurations.ant_tasks.asPath,
            onerror: "report"
        )

        ant.format_translation(
            dir: file("src/editor/resources/translations"),
            includes: "*.properties",
            eolstyle: "unix"
        )
        ant.format_translation(
            dir: file("src/viewer/resources/translations"),
            includes: "*.properties",
            eolstyle: "unix"
        )
    }
}

task check_translation(dependsOn: configurations.ant_tasks) {
    doLast {
        ant.taskdef(
            name: "check_translation",
            classname: "org.freeplane.ant.FormatTranslationCheck",
            classpath: configurations.ant_tasks.asPath,
            onerror: "report"
        )

        ant.check_translation(
            dir: file('src/editor/resources/translations'),
            includes: '*.properties',
            eolstyle: 'unix'
        )
        ant.check_translation(
            dir: file('src/viewer/resources/translations'),
            includes: '*.properties',
            eolstyle: 'unix'
        )
    }
}

void update(File resourceFile){
   def userFile = file('../User' + resourceFile.name)
   if (userFile.exists()) {
	  Properties properties = new Properties()
	  resourceFile.withInputStream {
		  properties.load(it)
	  }
	  
	  userFile.withInputStream {
		  properties.load(it)
	  }
	  
	  properties.store(resourceFile.newOutputStream(), null)
   }
}

task update_translation {
    doLast {
        fileTree(dir: './viewer-resources/translations', include: 'Resources_*.properties').each{
            File resourceFile -> update(resourceFile)
        }
        fileTree(dir: './resources/translations', include: 'Resources_*.properties').each{
            File resourceFile -> update(resourceFile)
        }
    }
}

update_translation.finalizedBy format_translation

build.dependsOn(check_translation)
