#!/usr/bin/tclsh

#    Copyright (C) 1996, 2000 Aladdin Enterprises.  All rights reserved.
# 
# This software is provided AS-IS with no warranty, either express or
# implied.
# 
# This software is distributed under license and may not be copied,
# modified or distributed except as expressly authorized under the terms
# of the license contained in the file LICENSE in this distribution.
# 
# For more information about licensing, please refer to
# http://www.ghostscript.com/licensing/. For information on
# commercial licensing, go to http://www.artifex.com/licensing/ or
# contact Artifex Software, Inc., 101 Lucas Valley Road #110,
# San Rafael, CA  94903, U.S.A., +1(415)492-9861.

# $Id: gssubst 6300 2005-12-28 19:56:24Z giles $

# 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
}
