#!/usr/bin/tclsh

#    Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
# 
# This file is part of GNU ghostscript
#
# GNU ghostscript is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This software is provided AS-IS with no warranty, either express or
# implied. That is, 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA, 02110-1301.


# $Id: gssubst,v 1.5 2007/08/01 14:26:56 jemarch 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
}
