#!/usr/bin/tclsh
########################################################################
#
# I really don't know why I am doing this. It is only to support
# brain-dead platforms which don't know basic things like sed
#
########################################################################

lappend auto_path .
package require clig
namespace import ::clig::\[A-Za-z\]*

setSpec ::main
String -i infile {input file} -m
String -o outfile {output file} -m
Rest reps {pattern-replacement pairs} -c 2 oo
# proc catch {script args} {
#   uplevel 1 $script
#   set [lindex $args 0] bla
# }

set Program [file tail $argv0]

if {[catch {parseCmdline main $Program $argc $argv} err]} {
  puts stderr $err
  exit 1
}
if {[llength $reps]%2!=0} {
  puts stderr "$Program: there must be an odd number of arguments"
  exit 1
}

set in [open $infile r]
set out [open $outfile w]

while {-1!=[gets $in line]} {
  foreach {pat rep} $reps {
    regsub -all $pat $line $rep line
  }
  puts $out $line
}
close $in
close $out
