import org.apache.tools.ant.filters.FixCrLfFilter

// some shorthands for often-used include(...)/exclude(...) specs:
def textFilesIncludeSpec(dir) {
    if (dir == null)
        return ['**/*.java',
                '**/*.xml',
                '**/*.xsl',
                '**/*.mm',
                '**/*.properties',
                '**/*.gradle']
    else
        return [dir + '/**/*.java',
                dir + '/**/*.xml',
                dir + '/**/*.xsl',
                dir + '/**/*.mm',
                dir + '/**/*.properties',
                dir + '/**/*.gradle']
}

def buildFiles() {
    return ['*/build/**', '**/bin/**', '*/apt_generated/**', '*/out/**']
}

def miscExcludedFilesSpec() {
    return [
			/* dot files */                  '**/.*/**',
            /* might include credentials! */ '**/signjar*.properties'
            ]
}

// NOTE: if you change excludes here, also adapt task 'srcpureTarGz' below
// (unfortunately there is some redundancy)!
task srcTarGz(type: Tar) {
    destinationDir = new File(globalDist)
    archiveName = 'freeplane_src-' + distVersion + '.tar.gz'
    compression = 'GZIP'
    includeEmptyDirs = false

    // text files (convert to LF)
    from(rootDir) {
        include('license.txt')
        include('*.gradle')
        include(textFilesIncludeSpec('freeplane*'))
        include(textFilesIncludeSpec('JOrtho_0.4_freeplane'))

        // exclude gradle/eclipse build files for all projects:
        exclude(buildFiles())
        // exclude misc files:
        exclude(miscExcludedFilesSpec())

        filter(FixCrLfFilter.class,
            eol:FixCrLfFilter.CrLf.newInstance("lf"), fixlast:false)
    }

    // all other (source) files
    from(rootDir) {
        include('freeplane*/**')
        include('JOrtho_0.4_freeplane/**')
        exclude(textFilesIncludeSpec(null))

        // exclude gradle/eclipse build files for all projects:
        exclude(buildFiles())
        // exclude misc files:
        exclude(miscExcludedFilesSpec())
    }

    // these two files must be included because they are generated from git info
    // and git info is not available int src/srcpure distribs!
    from(globalBin) {
        include('gitinfo.txt')
    }
    from(globalBin + '/resources') {
        include('gitinfo.properties')
    }

    into('freeplane-' + distVersion)
}

def srcpureAdditionalExcludeSpec() {
    // built from latex.flex using jflex!
    return ['freeplane_plugin_jsyntaxpane/src/main/java/jsyntaxpane/lexers/LaTeXLexer.java',

    // the flash files don't have a source in the freeplane-distrib,
    // which is a problem for Debian,
    // see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=736106 
    // (the source is here: https://github.com/freeplane/misc/tree/master/flash-browser)
    // => there is an (inactive) Debian package here:
    //    https://github.com/fnatter/freeplane-flash-browser-debian/
          'freeplane/resources/flash/visorFreeplane.swf', 'freeplane/resources/flash/flashobject.js',

    // windows stuff
            '**/*.exe', '**/*.res', '**/*.jar', '**/*.dll',
    // MAC stuff
            '**/*JavaApplicationStub*']

}

task srcpureTarGz(type: Tar) {
    destinationDir = new File(globalDist)
    archiveName = 'freeplane_srcpure-' + distVersion + '.tar.gz'
    compression = 'GZIP'
    includeEmptyDirs = false

    // text files (convert to LF)
    from(rootDir) {
        include('license.txt')
        include('*.gradle')
        include(textFilesIncludeSpec('freeplane*'))
        include(textFilesIncludeSpec('JOrtho_0.4_freeplane'))

        // exclude gradle/eclipse build files for all projects:
        exclude(buildFiles())
        // exclude misc files:
        exclude(miscExcludedFilesSpec())
        // exclude additional srcpure stuff
        exclude(srcpureAdditionalExcludeSpec())

        filter(FixCrLfFilter.class,
            eol:FixCrLfFilter.CrLf.newInstance("lf"), fixlast:false)
    }

    // all other (source) files
    from(rootDir) {
        include('freeplane*/**')
        include('JOrtho_0.4_freeplane/**')
        exclude(textFilesIncludeSpec(null))

        // exclude gradle/eclipse build files for all projects:
        exclude(buildFiles())
        // exclude misc files:
        exclude(miscExcludedFilesSpec())
        // exclude additional srcpure stuff
        exclude(srcpureAdditionalExcludeSpec())
    }

    // these two files must be included because they are generated from git info
    // and git info is not available int src/srcpure distribs!
    from(globalBin) {
        include('gitinfo.txt')
    }
    from(globalBin + '/resources') {
        include('gitinfo.properties')
    }

    into('freeplane-' + distVersion)
}
