# Agent Tcl
# Bob Gray
# 7 April 1997
# 
# customize.tcl
#
# This file customizes the Tcl scripts for the local machine's configuration.

proc customize code {

    global tclExecutable
    global tkExecutable
    global javaExecutable
    global schemeExecutable
    global tclShortExecutable
    global tkShortExecutable
    global javaShortExecutable
    global schemeShortExecutable
    global pgpKeys
    global pgpExecutable
    global tclLibrary
    global tkLibrary
    global libraryInstall
    global serverExecutable
    global serverInstall
    global tcpipSocket 
    global unixSocket 
    global logDirectory 
    global username 
    global groupname 
    global serverHome 
    global localMachine 
    global dataDirectory
    global adminKeyname
    global javaClasspath
    global javaZipClasspath
    global tempDirectory;
   
    regsub -all \
	@JAVA_ZIP_CLASSPATH@ \
	$code \
	$javaZipClasspath \
	code

    regsub -all \
	@JAVA_CLASSPATH@ \
	$code \
	$javaClasspath \
	code

    regsub -all \
	@TK_STARTUP@ \
	 $code \
	"#!/bin/sh\n# restart with the Agent Tk interpreter \\\nexec $tkExecutable \"\$0\" \"\$@\"" \
	code

    regsub -all \
	@TCL_STARTUP@ \
	$code \
	"#!/bin/sh\n# restart with the Agent Tcl interpreter \\\nexec $tclExecutable \"\$0\" \"\$@\"" \
	code

    regsub -all \
	@TK_EXECUTABLE@ \
	$code \
	$tkExecutable \
	code

    regsub -all \
	@TCL_EXECUTABLE@ \
	$code \
	$tclExecutable \
	code

    regsub -all \
	@JAVA_EXECUTABLE@ \
	$code \
	$javaExecutable \
	code

    regsub -all \
	@SCHEME_EXECUTABLE@ \
	$code \
	$schemeExecutable \
	code

    regsub -all \
	@TK_SHORT_EXECUTABLE@ \
	$code \
	$tkShortExecutable \
	code

    regsub -all \
	@TCL_SHORT_EXECUTABLE@ \
	$code \
	$tclShortExecutable \
	code

    regsub -all \
	@JAVA_SHORT_EXECUTABLE@ \
	$code \
	$javaShortExecutable \
	code

    regsub -all \
	@SCHEME_SHORT_EXECUTABLE@ \
	$code \
	$schemeShortExecutable \
	code

    regsub -all \
	@PGP_KEYS@ \
	$code \
	$pgpKeys \
	code

    regsub -all \
	@PGP_EXECUTABLE@ \
	$code \
	$pgpExecutable \
	code

    regsub -all \
	@TCL_LIBRARY@ \
	$code \
	$tclLibrary \
	code

    regsub -all \
	@TK_LIBRARY@ \
	$code \
	$tkLibrary \
	code

    regsub -all \
	@LIBRARY_INSTALL@ \
	$code \
	$libraryInstall \
	code

    regsub -all \
	@SERVER_EXECUTABLE@ \
	$code \
	$serverExecutable \
	code

    regsub -all \
	@SERVER_INSTALL@ \
	$code \
	$serverInstall \
	code

    regsub -all \
	@TCPIP_SOCKET@ \
	$code \
	$tcpipSocket \
	code

    regsub -all \
	@UNIX_SOCKET@ \
	$code \
	$unixSocket \
	code

    regsub -all \
	@LOG_DIRECTORY@ \
	$code \
	$logDirectory \
	code

    regsub -all \
	@USERNAME@ \
	$code \
	$username \
	code

    regsub -all \
	@GROUPNAME@ \
	$code \
	$groupname \
	code

    regsub -all \
	@SERVER_HOME@ \
	$code \
	$serverHome \
	code

    regsub -all \
	@LOCAL_MACHINE@ \
	$code \
	$localMachine \
	code

    regsub -all \
	@DATA_DIRECTORY@ \
	$code \
	$dataDirectory \
	code

    regsub -all \
	@ADMIN_KEYNAME@ \
	$code \
	\"$adminKeyname\" \
	code

    regsub -all \
	@TEMP_DIRECTORY@ \
	$code \
	\"$tempDirectory\" \
	code

    return $code
}

    # get the command-line arguments

set buildDirectory [lindex $argv 0]
set sourceDirectory [lindex $argv 1]
set tclExecutable [lindex $argv 2]
set tkExecutable [lindex $argv 3]
set pgpExecutable [lindex $argv 4]
set pgpKeys [lindex $argv 5]
set tclLibrary [lindex $argv 6]
set tkLibrary [lindex $argv 7]
set libraryInstall [lindex $argv 8]
set serverExecutable [lindex $argv 9]
set serverInstall [lindex $argv 10]
set tcpipSocket [lindex $argv 11]
set unixSocket [lindex $argv 12]
set logDirectory [lindex $argv 13]
set username [lindex $argv 14]
set groupname [lindex $argv 15]
set serverHome [lindex $argv 16]
set localMachine [lindex $argv 17]
set dataDirectory [lindex $argv 18]
set adminKeyname [lindex $argv 19]
set javaExecutable [lindex $argv 20]
set javaZipClasspath [lindex $argv 21]
set javaClasspath [lindex $argv 22]
set tempDirectory [lindex $argv 23]
set schemeExecutable [lindex $argv 24]

    # get the short names of the Tcl and Tk executables

set tclShortExecutable [lindex [split $tclExecutable /] end]
set tkShortExecutable [lindex [split $tkExecutable /] end]
set javaShortExecutable [lindex [split $javaExecutable /] end]
set schemeShortExecutable [lindex [split $schemeExecutable /] end]

    # summarize the command-line arguments

puts ""
puts "Customizing the scripts:"
puts ""
puts "Build directory              : $buildDirectory"
puts "Source directory             : $sourceDirectory"
puts "Tcl executable (installed)   : $tclExecutable"
puts "Tcl executable (name only)   : $tclShortExecutable"
puts "Tk executable (installed)    : $tkExecutable"
puts "Tk executable (name only)    : $tkShortExecutable"
puts "Java executable (installed)  : $javaExecutable"
puts "Java executable (name only)  : $javaShortExecutable"
puts "Scheme executable (installed): $schemeExecutable"
puts "Scheme executable (name only): $schemeShortExecutable"
puts "PGP executable (installed)   : $pgpExecutable"
puts "PGP key directory            : $pgpKeys"
puts "Tcl library (installed)      : $tclLibrary"
puts "Tk library (installed)       : $tkLibrary"
puts "Java ZIP classpath           : $javaZipClasspath"
puts "Java classpath               : $javaClasspath"
puts "Library directory (installed): $libraryInstall"
puts "Server executable (installed): $serverExecutable"
puts "Server directory (installed) : $serverInstall"
puts "TCP/IP port                  : $tcpipSocket"
puts "UNIX socket filename         : $unixSocket"
puts "Log directory                : $logDirectory"
puts "Server username              : $username"
puts "Server groupname             : $groupname"
puts "Server home directory        : $serverHome"
puts "Server's machine             : $localMachine"
puts "Data directory               : $dataDirectory"
puts "Administrator keyname        : $adminKeyname"
puts "Agent temp directory         : $tempDirectory"
puts ""

    # customize the scripts

foreach source \
 	{keys/template/keys.tcl \
	 scripts/managers/template/manager.tcl \
	 scripts/managers/template/manager.conf \
	 scripts/start/template/start_agentd \
	 scripts/start/template/start_man \
	 scripts/start/template/agent.languages \
	 scripts/start/template/agentd.conf \
	 scripts/start/template/kill.tcl \
	 scripts/start/template/machine.tcl \
	 scripts/tk/demos/template/browse \
	 scripts/tk/demos/template/hello \
	 scripts/tk/demos/template/ixset \
	 scripts/tk/demos/template/rmt \
	 scripts/tk/demos/template/rolodex \
	 scripts/tk/demos/template/square \
	 scripts/tk/demos/template/tcolor \
	 scripts/tk/demos/template/timer \
	 scripts/tk/demos/template/widget \
	 examples/template/fac.tcl \
	 examples/template/fac.remote.tcl \
	 examples/template/hello.tk \
	 examples/template/rhello.tk \
	 examples/template/hello.bad.tk \
	 examples/template/driver.tcl \
	 examples/template/handler.tcl \
	 examples/template/client.tcl \
	 examples/template/server.tcl \
	 examples/template/disk.tcl \
	 examples/template/meet_one.tcl \
	 examples/template/meet_two.tcl \
	 examples/template/force.tcl \
	 examples/template/who.tcl \
	 examples/template/who.jump.tcl \
	 examples/template/who.jump.tk \
	} \
	dest \
	{keys/keys.tcl \
	 managers/manager.tcl \
	 managers/manager.conf \
	 start/start_agentd \
	 start/start_man \
	 start/agent.languages \
	 start/agentd.conf \
	 start/kill.tcl \
	 start/machine.tcl \
	 tk/demos/browse \
	 tk/demos/hello \
	 tk/demos/ixset \
	 tk/demos/rmt \
	 tk/demos/rolodex \
	 tk/demos/square \
	 tk/demos/tcolor \
	 tk/demos/timer \
	 tk/demos/widget \
	 examples/fac.tcl \
	 examples/fac.remote.tcl \
	 examples/hello.tk \
	 examples/rhello.tk \
	 examples/hello.bad.tk \
	 examples/driver.tcl \
	 examples/handler.tcl \
	 examples/client.tcl \
	 examples/server.tcl \
	 examples/disk.tcl \
	 examples/meet_one.tcl \
	 examples/meet_two.tcl \
	 examples/force.tcl \
	 examples/who.tcl \
	 examples/who.jump.tcl \
	 examples/who.jump.tk \
	} \
{
    puts "Customizing $source ..."

    set fd [open $sourceDirectory/$source]
    set code [read $fd]
    close $fd

    set code [customize $code]

    set fd [open $buildDirectory/scripts/$dest w 0644]
    puts $fd $code
    close $fd
}


