import org.apache.tools.ant.filters.*
import org.apache.tools.ant.taskdefs.condition.Os

configurations{
	mergedApiDependency
	lib.extendsFrom(mergedApiDependency)
}

dependencies {
	mergedApiDependency project(':freeplane_api')
}

task freeplanelauncherJar(type: Jar) {
     archiveName = 'freeplanelauncher.jar'
     destinationDir = new File(globalBin)

     dependsOn classes
     from (sourceSets.main.output) {
          include("**")
     }
     includeEmptyDirs = false

     manifest {
          attributes("Manifest-Version": "1.0",
                     "Main-Class": "org.freeplane.launcher.Launcher",
                     "Class-Path": "framework.jar")
     }
	 
	 configurations.mergedApiDependency.files.each {
    	from(zipTree(it.path)) {
    		exclude('META-INF/MANIFEST.MF')
    	}
    }
}

jar.dependsOn freeplanelauncherJar

task copyFreeplaneShellScript(type: Copy) {
     from('script/') {
         include('freeplane.sh')
         fileMode = 0755
     }
	 into(globalBin)
	 filter(FixCrLfFilter.class, eol:FixCrLfFilter.CrLf.newInstance("lf"))
}	 

ext {launch4jDir = 'C:/Program Files (x86)/Launch4j'}

task createExeFiles {
	dependsOn 'makeKnopflerfischPermissionDirs'
	onlyIf { Os.isFamily(Os.FAMILY_WINDOWS) && file(launch4jDir).exists()}
	inputs.dir('launch4j')
	outputs.files("${globalBin}/freeplane.exe", "${globalBin}/freeplaneConsole.exe")
	doLast {
        ant.taskdef(name: 'launch4j',
                    classname: 'net.sf.launch4j.ant.Launch4jTask',
                    classpath: "${launch4jDir}/launch4j.jar:${launch4jDir}/lib/xstream.jar")
        ant.launch4j (
        	configFile: 'launch4j/freeplaneGui.lj4.xml',
        	outfile: "${globalBin}/freeplane.exe"
        )
        ant.launch4j (
        	configFile: 'launch4j/freeplaneConsole.lj4.xml',
        	outfile: "${globalBin}/freeplaneConsole.exe"
        )
    }
}

task copyLauncherStuff(type: Copy) {
	 dependsOn 'copyFreeplaneShellScript', 'createExeFiles'
     from('script/') {
         include('*')
         exclude('freeplane.sh')
     }

     def knopflerfishJarFile = project.configurations.runtime.files.find {
         isKnopflerfishJar(it.name)
     }
     from(knopflerfishJarFile)
     // rename [knopflerfish-]framework-*.jar to framework.jar
     rename { String fileName ->
         if (isKnopflerfishJar(fileName))
            return "framework.jar"
         else
            return fileName
     }

     from('windows-icons/') {
         include('freeplaneIcons.dll')
     }
     from('launch4j/') {
         include('*.exe')
         include('*.ini')
     }
     into(globalBin)
     dependsOn jar
}

task makeKnopflerfischPermissionDirs {
    doLast {
        new File(globalBin, 'fwdir/perms').mkdirs()
        new File(globalBin, 'fwdir/condperm').mkdirs()
    }
}

eclipseJdt {
    doLast {
        File f = file('.settings/org.eclipse.core.resources.prefs')
        f.append('encoding/windows-installer=ISO-8859-1\n')
    }
}

build.dependsOn (copyLauncherStuff, makeKnopflerfischPermissionDirs)


