String fileName = ""
Boolean sortByTextAppearance = false, argsOk=false
String errorText="Usage groovy OrderFootnotes.groovy [option] filename\noption: -t: order footnotes by text appearance"
if( args ){
   if( args.size() == 2 ){
       argsOk=true
       args.each{ String arg ->
           if( arg=="-t" ){
               sortByTextAppearance = true
           } else {
               fileName = arg
           }
       }
   } else if( args.size() == 1 ){
       argsOk=true
       fileName = args[0]
   }
}
if( ! argsOk ){
   println errorText
   return
}
Map<String,String> mapOldNewNumber = [:]
FileReader fr = new FileReader(fileName)
Map<String,Long> footnoteNumbers = [:]
Set footnoteSet = []
if( sortByTextAppearance ){
       if(fr.ready()){
               Boolean isFootnotes = false
               long newCount = 0
               fr.eachLine{ String line ->
                       String parsed = ""
                       Long startIndex = 0
                       line.eachMatch(/\[[0-9]+\]/){ number ->
                               long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,""))
                               if( isFootnotes ){
                                       footnoteNumbers[mapOldNewNumber[tmpNumber]] = line - number[0]
                               } else {
                                       long tmpCount = mapOldNewNumber[tmpNumber]
                                       if( ! tmpCount ){
                                               tmpCount = mapOldNewNumber[tmpNumber] = ++newCount
                                       }
                                       parsed += line[startIndex..<line.indexOf("[${tmpNumber}]")]
                                       parsed += "[$tmpCount]"
                                       startIndex = (line.indexOf("[${tmpNumber}]") + "[${tmpNumber}]".size() )
                               }
                       }
                       if ( line ==~ /@footnote:/ && ! isFootnotes ) {
                               isFootnotes = true
                               println line
                       }
                       if( ! isFootnotes )
                               println "${parsed}"
               }
               fr.close()
       } else {
               println errorText
               return
       }
} else {
       if(fr.ready()){
               Boolean isFootnotes = false
               long newCount = 0
               fr.eachLine{ String line ->
                       if( isFootnotes ){
                               line.eachMatch(/\[[0-9]+\]/){ number ->
                                       long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,""))
                                       footnoteNumbers["${++newCount}"] = line - number[0]
                                       mapOldNewNumber["${tmpNumber}"] = newCount
                               }
                       }
                       if ( line ==~ /@footnote:/ && ! isFootnotes )
                               isFootnotes = true
               }
               fr.close()
               fr = new FileReader(fileName)
               isFootnotes = false
               fr.eachLine{ String line ->
                       if ( line ==~ /@footnote:/ && ! isFootnotes ) {
                               isFootnotes = true
                               println line
                       }
                       if( ! isFootnotes ){
                               Long startIndex = 0
                               String parsed = ""
                               line.eachMatch(/\[[0-9]+\]/){ number ->
                                       long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,""))
                                       long replaceNum = mapOldNewNumber["$tmpNumber"]
                                       parsed += line[startIndex..<line.indexOf("[$tmpNumber]")]
                                       parsed += "[$replaceNum]"
                                       startIndex = (line.indexOf("[$tmpNumber]") + "[$tmpNumber]".size())
                               }
                               println parsed
                       }
               }
               fr.close()
       } else {
               println errorText
               return
       }
}
println footnoteNumbers.entrySet().sort{Long.valueOf(it.key)}.collect{"[${it.key}] ${it.value}"}.join("\n")