#!/usr/bin/tclsh

#    Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA, 02111-1307.

# $Id: gssubst,v 1.1.4.1.2.1 2003/04/12 14:02:39 giles Exp $

# Replace one word by another in a set of files.  This is just a front end
# to a simple Perl invocation.

if {$argc < 2} {
    puts stderr "Usage: $argv0 (-t type | -u word | fromword toword) file ..."
    puts stderr "  -t word = word word_t"
    puts stderr "  -u word = word WORD"
    exit 1
}
set a0 [lindex $argv 0]
set a1 [lindex $argv 1]
switch -- $a0 {
    -t {set from $a1; set to ${from}_t}
    -u {set from $a1; set to [string toupper ${from}]}
    default {set from $a0; set to $a1}
}
puts "$from => $to"
flush stdout
set tmp /tmp/[pid]
foreach f [lreplace $argv 0 1] {
    if {![file exists $f~]} {exec cp -p $f $f~}
    exec perl -pe "s\{\\b${from}\\b\}\{${to}\}g" < $f > $tmp
    exec mv $tmp $f
}
