#!/usr/bin/env tclsh8.5
#
# Renumber references and sort appendix
#
# (c) 2008 Michael Schlenker
#
# Handle the problem in multiple passes.
# 1. Find numbered footnotes and create renumbered mapping
# 2. Apply mapping to all numbers
# 3. Sort the footnote appendix and memorize indices
# 4. Output the head part, than iterate over the appendix
#
# we use Tcl 8.5 features
package require Tcl 8.5
# import +/-
namespace path ::tcl::mathop
proc main {file} {
set fd [open $file]
fconfigure $fd -encoding iso8859-15
fconfigure stdout -encoding iso8859-15
set data [renumber [read $fd]]
close $fd
lassign [sort_appendix $data] first parts
# dump the head part
puts [string range $data 0 $first]
# output the footnote appendix
foreach item $parts {
lassign $item no start end
puts [format "\[%d\] %s" $no [string range $data $start $end]]
}
}