#!/usr/bin/tcl

# Script to change macros in fvwm2-ade.  The first argument is the name
# of the file in which the macro to be set resides.  The second argument
# is the name of the macro.  All arguments that follow constitute the
# definition of the macro.  Double quotes must be escaped with the \
# character.

# Side effects:  if you ask for the macro BLAH, calling this script like
# this:
#
#    changemacro filename BLAH Exec xterm
#
# then ALL LINES in the file "filename" containing the string "<<BLAH>>"
# (without the quotes) will be replaced by the line
#
# define(<<BLAH>>, <<Exec xterm>>)dnl

# If the indicated file is not among the configuration files in
# fvwm2-ade, then nothing happens.

# If the indicated file does not include the indicated macro, then
# nothing happens.

# Gregory Fall
# November 1997

set userdir $env(HOME)/.fvwm2-ade

# Extract the filename from the command line.

set filename [lindex $argv 0]

# Extract the macroname from the command line.

set macroname [lindex $argv 1]

# Extract the macro definition from the command line.

set macrocmd ""
set words 0
foreach word $argv {
    if {[expr [expr [string compare $word $filename] != 0] && \
	    [expr [string compare $word $macroname] != 0]] == 1} {

	# This script uses the sed utility to make the requested
	# changes.  sed reserves the following characters:
	# ^ $ . * [ ] \ /
	# Of those, most can still be included on the command line
	# without any trouble (\ is, however, problematic).  If / is
	# included (this occurs frequently), it must be escaped with a
	# \:

        regsub -all \/ $word \\/ word        

	if {$words == 0} {
	    set macrocmd $macrocmd$word
	    incr words
	} else {
	    set macrocmd $macrocmd\ $word
	    incr words
	}
    }
}

puts stdout $word

# Make sure the requested file is there.

if {![file exists $userdir/$filename]} {
    set basedir /usr/local/etc/X11/fvwm2-ade
    exec $basedir/bin/makeuserdir $basedir
}

if {[file exists $userdir/$filename]} {
#    if {[string length $macrocmd] > 0} {

    # Use sed to change the macro definition.
#    exec echo <<$macrocmd>> > $userdir/foo
    exec sed -e "s/.*<<$macroname>>.*/define(<<$macroname>>, <<$macrocmd>>)dnl/g" $userdir/$filename >> $userdir/$filename.tmp
    exec mv -f $userdir/$filename.tmp $userdir/$filename

#    }
}
