//tag::exportContributors[]
import static groovy.io.FileType.*
import java.security.MessageDigest

task exportContributors(
    description: 'exports all contributors for all asciidoc files',
    group: 'docToolchain'
) {
    doLast {
        def wordsPerMinute = 189
        def md5 = { input ->
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(input.getBytes());
            BigInteger hash = new BigInteger(1, md5.digest());
             String.format("%032x", hash)
        }
        File sourceFolder = new File("${docDir}/${inputPath}")
        println "sourceFolder: " + sourceFolder.canonicalPath
        println "targetFolder: " + targetDir + "/contributors"
        sourceFolder.traverse(type: FILES) { file ->
            if (file.name ==~ '^.*[.](ad|adoc|asciidoc)$') {
                def currentFileName = file.canonicalPath.replace("\\", "\\\\")
                def readingtime = getReadingtime(currentFileName, wordsPerMinute)
                def contributors = ["git", "log", "--no-merges", "--follow",  "HEAD", currentFileName].execute(null,  sourceFolder)
                def sout = new StringBuilder(), serr = new StringBuilder()
                contributors.waitForOrKill(1000)
                contributors = contributors.text
                contributors = contributors
                //.text
                    .split("\n")
                    .collect { line ->
                        if (line.startsWith('Author')) {
                            def matcher = line =~ /[\t ]*Author: ([^<]+) <([^>]+)>/
                            if (matcher) {
                                [matcher[0][1], matcher[0][2]]
                            } else {
                                ["", ""]
                            }
                        } else {
                            ["", ""]
                        }
                    }.unique { a, b -> a[1] <=> b[1] }
                def avatars = contributors.collect {
                    if (it[1]) {
                        "image:https://www.gravatar.com/avatar/" +
                            md5(it[1].toLowerCase().trim()) +
                            "?d=identicon" +
                            "[height=32,width=32,role='gravatar',alt='${it[0]}',title='${it[0]}']"
                    } else { ""}
                }.minus("").join(" ")
                def tag = (file.canonicalPath - sourceFolder.canonicalPath).replace("\\", "/").replaceAll("^/", "")
                File targetFile = new File(targetDir.toString(), "/contributors/" + tag)
                safeMkDirs(targetFile)
                targetFile.write(avatars + "\n")
//              println("\u023F1 $readingtime minutes to read")
                def minutesTerm = readingtime == 1 ? "minute" : "minutes"
                targetFile.append("\n $readingtime $minutesTerm to read \n")

                //file attributes
                targetFile = new File(targetDir.toString(), "/fileattribs/" + tag)
                safeMkDirs(targetFile)

                def attributes = ["git", "log", "-1", "--no-merges", "--pretty=format:%x7c%x20%at%n%x7c%x20%ad%x20%n%x7c%x20%aE%n%x7c%x20%an%x20%n%x7c%x20%s%x20%n", "--date=short", "HEAD", currentFileName].execute(null,  sourceFolder)
                attributes.waitForOrKill(1000)
                attributes = attributes.text.split("[|]").collect{it.trim()}
                if (attributes.size()>1) {
                    attributes = attributes[1..-1]
                    def avatar = "http://www.gravatar.com/avatar/" +
                        md5(attributes[2].toLowerCase().trim()) +
                        "?d=identicon" +
                        "[32,32,role='gravatar',alt='${attributes[3]}',title='${attributes[3]}']"
                    targetFile.write("""\
:lastUpdated: ${new Date((attributes[0] as Long)*1000).format('dd.MM.yyyy hh:mm')}
:lastAuthorName: ${attributes[3]}
:lastAuthorEmail: ${attributes[2]}
:lastAuthorAvatar: ${avatar}
:lastMessage: ${attributes[4]}
""")
                }
                print "."
            }
        }
        println ""
    }
}

def getReadingtime(def filename, def wpm) {
    assert wpm != 0
    def file = new File(filename)

    String[] words =
        file
            .text
            .trim()
            .split("\\s+");

    return (words.length / wpm).toInteger() +1
}

void safeMkDirs(File file) {
    File fileDir = file.getParentFile();
    if (!fileDir.exists()) {
        if (!fileDir.mkdirs()) {
            throw new RuntimeException("unable to create folder '" + fileDir + '')
        }
    }
}

//end::exportContributors[]

