#!/usr/local/bin/wish -f

#  This Tcl/Tk Motif GUI wrapper for program `genmp' created by version 2.1
#  of generate_tk_program.
#
#  Stephen O. Lidie, Lehigh University.  Wed May  4 11:16:44 EDT 1994
#
#  lusol@Lehigh.EDU

source /usr/local/lib/tcl+tk_lucc/FSBox.tcl

set gentkp_highlight [ option get . highlight Highlight ]
if { $gentkp_highlight == "" } {
	if { [ tk colormodel . ] == "monochrome" } {
		set gentkp_highlight "white"
	} else {
		set gentkp_highlight "azure"
	}
}

# Initialize global variables.

set commands "\$required"
set commands0 "$commands"
set author "\$required"
set author0 "$author"
set see_also "\"\""
set see_also0 "$see_also"
set output_path "man"
set output_path0 "$output_path"



proc Reset_Parameters {} {

	# Restore all command line parameter values to their default values.

	global commands commands0
	set commands "\$required"
	set commands0 "$commands"
	global author author0
	set author "\$required"
	set author0 "$author"
	global see_also see_also0
	set see_also "\"\""
	set see_also0 "$see_also"
	global output_path output_path0
	set output_path "man"
	set output_path0 "$output_path"

}

Reset_Parameters

set gentkp_command "genmp"
set c ".main"
set gentkp_fini 0
set gentkp_ok_background "white"

# Application command line defaults.

	# Maybe something here?




proc Update {} {

	# Create the command to execute.


	# Perform application specific command line argument processing here.

		# Maybe something here?

	set gentkp_command "genmp"

	# Build all non-switch parameters that have been specified.

	foreach parameter { "commands" "author" "see_also" "output_path" } {
		upvar #0 $parameter p
		upvar #0 ${parameter}0 p0
		for { set i 0 } { $i < [ llength $p ] } { incr i } {
			if { [ lrange $p 0 end ] != [ lrange $p0 0 end ] } {
				set value [ lindex $p $i ]
				set gentkp_command "$gentkp_command -$parameter \"$value\""
			}
		}
	}

	# Build all switch parameters that have been specified.

	foreach parameter { } {
		upvar #0 $parameter p
		upvar #0 ${parameter}0 p0
		if { [ lrange $p 0 end ] != [ lrange $p0 0 end ] } {
			if { $p != "-not_${parameter}" } {
				set gentkp_command "$gentkp_command -${parameter}"
			}
		}
	}
	set gentkp_command "$gentkp_command "
	return $gentkp_command
}




proc Update_Parameter { p v } {

	# Insert 'v' into list 'p' unless it's already there, in which case remove it!

        upvar #0 $p list		# pass by name

	set cofp  [ lrange $list 0 end ]
	set is_it_there [ lsearch -exact $list $v ]
	if { $is_it_there >= 0 } {
		set cofp [ lreplace $list $is_it_there $is_it_there ]
	} else {
		set cofp [ lappend list $v ]
	}

	return $cofp
}



proc Tab {list} {

	# Move the focus to the next window in the tab list.

	set i [ lsearch $list [ focus ] ]

	if {$i < 0} {
		set i 0
	} else {
		incr i
		if {$i >= [ llength $list ]} {
		    set i 0
		}
	}
	focus [ lindex $list $i ]
}




proc Pipe_Window {which} {

	# Create a modal dialog entry toplevel window divided into an upper message widget, a middle entry widget and a lower
	# frame with OK and Cancel button widgets.  Make OK the default button.  Center the window, make a local grab, wait
	# for the pipeline string to be entered, destroy the window and perform the exec.

	global gentkp_pipeline

	set pipe ""	
	set gentkp_pipeline ""

	catch { destroy .pipe }
        toplevel .pipe -class dialog
	wm title .pipe "Pipe"
	wm iconname .pipe "Pipe"
	frame .pipe.f1 -bd 1 -relief raised
	frame .pipe.f2 -bd 1 -relief raised
	frame .pipe.f3 -bd 1 -relief raised
	pack .pipe.f1 .pipe.f2 .pipe.f3 -side top -fill both
	message .pipe.msg -aspect 200 -text \
	  "Enter command pipeline.  Separate all tokens by whitespace, even I/O redirection symbols, or else the exec will fail."
	pack .pipe.msg -in .pipe.f1 -side top -expand yes -fill both -padx 5m -pady 5m
	entry .pipe.entry -relief sunken -width 40 -textvariable pipe
	focus .pipe.entry
	bind .pipe.entry <KeyPress-Return> "set gentkp_pipeline \$pipe"
	pack .pipe.entry -in .pipe.f2 -side top -expand yes -fill both -padx 5m -pady 5m
	button .pipe.ok -text OK -command "set gentkp_pipeline \$pipe"
	frame .pipe.default -relief sunken -bd 1
	raise .pipe.ok .pipe.default
	pack .pipe.default -in .pipe.f3 -side left -expand yes -padx 3m -pady 2m
	pack .pipe.ok -in .pipe.default -padx 2m -pady 2m -ipadx 2m -ipady 2m
	button .pipe.cancel -text Cancel -command "set gentkp_pipeline \"\""
	pack .pipe.cancel -in .pipe.f3 -side left -expand yes -padx 3m -pady 3m -ipadx 2m -ipady 1m

	wm withdraw .pipe
	update idletasks
	set x [expr [winfo screenwidth .pipe]/2 - [winfo reqwidth .pipe]/2 - [winfo vrootx [winfo parent .pipe]]]
	set y [expr [winfo screenheight .pipe]/2 - [winfo reqheight .pipe]/2 - [winfo vrooty [winfo parent .pipe]]]
	wm geom .pipe +$x+$y
	wm deiconify .pipe

	grab .pipe
	tkwait variable gentkp_pipeline
	destroy .pipe

	if { $gentkp_pipeline != "" } {
		set text_window_contents [$which get 1.0 end]
		set p [ open "| $gentkp_pipeline" w ]
		puts $p $text_window_contents
		close $p
	}

}




proc Save_Window {which} {

        # Open a file selection window.

	global fsBox
	set o "xgenmp.output"
	set o [ FSBox "Select file:" $o ];
	if { $o != "" } {
		set replace 1
		if [ file exists $o ] {
			set replace [ tk_dialog .replace "Alert" "Replace existing \"$fsBox(name)\"?" \
				warning 0 Cancel Replace ]
		}
		if { $replace == 1 } {
			set text_window_contents [ $which get 1.0 end ]
			set p [ open "| cat > $o" w ]
			puts $p $text_window_contents
			close $p
		}
	}

}




proc See_View { parameter } {

        # Position view of the command Entry widget to this command line parameter.

        global c gentkp_command
	if { $parameter == "end_of_MM" } {
	        set index [ $c.see.e index end ]
	} else {
        	set index [ string first $parameter $gentkp_command ] 
	}
        $c.see.e view [ expr $index - 10 ]

}




proc Display_About {} {

	tk_dialog .help_version About "This Tcl/Tk Motif GUI wrapper for program `genmp' created by version 2.1 of generate_tk_program.\n\nStephen O. Lidie, Lehigh University.  Wed May  4 11:16:44 EDT 1994\n\nlusol@Lehigh.EDU" "@/usr/local/lib/tcl+tk_lucc/SOL.xbm" 0 OK

}
source /usr/local/lib/tcl+tk_lucc/DisU.tcl




proc Execute_Command { } {

	# Open a Toplevel Output window, exec the Unix command and capture stdout/stderr.
	# If AddInput is available then use it, else just do stupid blocking reads.

	global gentkp_command runme_num c gentkp_fini gentkp_highlight

	set gentkp_command [Update]
	set execute 1
	if { $execute == 1 } {

		# Special case $required parameters.

		global commands
		if { $commands == "\$required" || $commands == "" } {
			tk_dialog .required "Alert" "Parameter \"commands\" requires a value." warning 0 Cancel
			Reset_OK_Button
			return
		}
		global author
		if { $author == "\$required" || $author == "" } {
			tk_dialog .required "Alert" "Parameter \"author\" requires a value." warning 0 Cancel
			Reset_OK_Button
			return
		}
		catch { destroy .runme$runme_num }    
		toplevel .runme$runme_num
		wm title .runme$runme_num "xoq output $runme_num"
		wm iconname .runme$runme_num "xoq$runme_num"
		#wm iconbitmap .runme$runme_num @/usr/local/lib/tcl+tk_lucc/sqtp_out.xbm
		frame .runme$runme_num.menu -bd 1 -relief raised
		menubutton .runme$runme_num.menu.file -text File -menu .runme$runme_num.menu.file.m -underline 0
		menu .runme$runme_num.menu.file.m
		.runme$runme_num.menu.file.m add command -label "Save As ..." -command "Save_Window .runme$runme_num.text" \
			-underline 0
		.runme$runme_num.menu.file.m add command -label "Pipe To ..." -command "Pipe_Window .runme$runme_num.text" \
			-underline 0
		.runme$runme_num.menu.file.m add separator
		.runme$runme_num.menu.file.m add command -label Close -command "destroy .runme$runme_num" -underline 0
		pack .runme$runme_num.menu.file -side left
		text .runme$runme_num.text -relief raised -bd 2 -yscrollcommand ".runme$runme_num.s set" -setgrid true -font fixed
		scrollbar .runme$runme_num.s -relief flat -command ".runme$runme_num.text yview"
		pack .runme$runme_num.menu -side top -fill x -expand yes
		pack .runme$runme_num.s -side right -fill y
		pack .runme$runme_num.text -expand yes -fill both
		.runme$runme_num.text mark set insert 0.0
		bind .runme$runme_num <Any-Enter> "focus .runme$runme_num.text"
		update

		# Open the pipe.  The OK button has been disabled until now to prevent a race condition.

		set f [ open "| $gentkp_command |& cat"  r ]
		set gentkp_have_addinput [ catch { addinput $f "Read_STDOUT %% %E %F" } ]
		if { $gentkp_have_addinput == 0 } {
	
			$c.menu.ok configure -text "Cancel" -relief raised -command "Kill_STDOUT $f" -state normal
			Flash_Button $c.menu.ok -background $gentkp_highlight [lindex [ $c.menu.ok configure -background ] 4] 500

		} else {

			$c.menu.ok configure -state normal
			while { [ gets $f line ] >= 0 } {
				if { $line == "" } {
					.runme$runme_num.text insert end \n
				} else {
					set lines [ split $line "\r" ]
					foreach line $lines {
						if { $line == "" } {
							continue
						}
						.runme$runme_num.text insert end $line\n
						}
				}
			}
			catch { close $f }
			set runme_num [ expr $runme_num + 1 ]
                        Reset_OK_Button

		}
	}
}




proc Flash_Button { w option val1 val2 interval } {

	# Flash a window by alternating its foreground and background colors.

	global gentkp_fini

	if { $gentkp_fini == 0 } {
	        $w configure $option $val1
		after $interval [ list Flash_Button $w $option $val2 $val1 $interval ]
	}

}




proc Kill_STDOUT { fileid } {

	# With AddInput, a click on the blinking Cancel Button resumes normal operations.
	#
	# Bug Note:  can't close the pipe without first killing all its processes since
	# it too hangs on, say, TCP/IP operations without a timeout.  This leaves stray
	# processes around (at least on AIX).

	global gentkp_fini c

	set gentkp_fini 1
	removeinput $fileid
	exec kill [ pid $fileid ]
	catch { close $fileid }
	Reset_OK_Button

}




proc Read_STDOUT {token events fileid } {

	# With AddInput, called when input is available for the Output window.  Also checks
	# the global gentkp_fini to see if the user has clicked the Cancel Button.

	global gentkp_fini runme_num c

	if { $gentkp_fini } {
		Kill_STDOUT $fileid
	} else {
		if { [ gets $fileid line ] >= 0 } {
			if { $line == "" } {
				.runme$runme_num.text insert end \n
			} else {
				set lines [ split $line "\r" ]
				foreach line $lines {
					if { $line == "" } {
						continue
					}
					.runme$runme_num.text insert end $line\n
				}
			}
		} else {
			set gentkp_fini 1
			removeinput $fileid
			catch { close $fileid }
			set runme_num [ expr $runme_num + 1 ]	
			Reset_OK_Button
		}
	}
	
}




proc Reset_OK_Button {} {

	# Establish normal OK Button parameters.

	global gentkp_fini c gentkp_ok_background

	$c.menu.ok configure -text "Do It" -relief raised -background $gentkp_ok_background -state normal -command \
		{ set gentkp_fini 0; $c.menu.ok configure -text "Working ..." -relief sunken -state disabled; Execute_Command }

}




set runme_num 1

wm title . "xgenmp"
wm iconname . "xgenmp"
#wm iconbitmap . @/usr/local/lib/tcl+tk_lucc/sqtp.xbm
wm geometry . +400+50

set realize "pack $c $c.menu $c.w_genmp_command $c.w_commands $c.w_author $c.w_see_also $c.w_output_path $c.see -side top -fill x"
set tabs "set tabList \" $c.w_commands.entry $c.w_author.entry $c.w_see_also.entry $c.w_output_path.entry\""


# Toplevel frame.

catch { destroy $c }
frame $c -bd 1
pack $c -side top -fill both -expand yes

# Command to execute.

frame $c.see
entry $c.see.e -relief ridge -scroll "$c.see.s set" -textvariable gentkp_command
scrollbar $c.see.s -relief sunken -orient horiz -command "$c.see.e view"
pack $c.see.e -pady 1m -padx 1m -side top -fill x
pack $c.see.s -side top -fill x

# Menu selections.

frame $c.menu -bd 1

menubutton $c.menu.file -text File -menu $c.menu.file.m -underline 0
menu $c.menu.file.m
$c.menu.file.m add command -label "Open ..." -underline 0 -state disabled -command {
	set tmp_files [FSBox]
	if { $tmp_files != "" } {
		set files $tmp_files
		set gentkp_command [ Update ]    
	}
}
$c.menu.file.m add separator
$c.menu.file.m add command -label "Quit" -underline 0 -command  "destroy ."

menubutton $c.menu.edit -text Edit -menu $c.menu.edit.m -underline 0
menu $c.menu.edit.m
$c.menu.edit.m add command -label "Undo All" -underline 0 \
    -command { $c.w_genmp_command.t yview 0.0; Reset_Parameters; set gentkp_command [ Update ] }

menubutton $c.menu.filler -text "          " -state disabled

menubutton $c.menu.help -text Help -menu $c.menu.help.m -underline 0
menu $c.menu.help.m
$c.menu.help.m add command -label "About" -underline 0 -command "Display_About"
$c.menu.help.m add command -label "Usage" -underline 0 -command "Display_Usage 2.1"

button $c.menu.ok
set gentkp_ok_background [ lindex [ $c.menu.ok configure -background ] 4 ]
Reset_OK_Button

pack $c.menu.file $c.menu.edit -side left
pack $c.menu.ok -side left -expand yes
pack $c.menu.help $c.menu.filler -side right

# Full command help from evaluate_parameters Message Module.

frame $c.w_genmp_command
text $c.w_genmp_command.t -relief raised -bd 1 -yscrollcommand "$c.w_genmp_command.s set" -setgrid true -height 10 -font fixed
scrollbar $c.w_genmp_command.s -relief flat -command "$c.w_genmp_command.t yview"
pack $c.w_genmp_command.s -side right -fill y
pack $c.w_genmp_command.t -expand yes -fill both
$c.w_genmp_command.t insert 0.0 \
{Command Source:  /usr/local/bin/genmp



generate_man_page, genmp

        Use the output from evaluate_parameters to create
        a man page.  Output is written to a file name
        created by concatenating:

          `output_path/' . `name of the command' . `1'

          Example:

            genmp -c genmp -a 'S. O. Lidie'

              This example generates genmp's man page.
              Output is written to file `man/genmp.1'.

Parameters:

-help, ?, usage_help, full_help: Display Command Information

	Display information about this command, which includes
	a command description with examples, plus a synopsis of
	the command line parameters.  If you specify -full_help
	rather than -help complete parameter help is displayed
	if it's available.

-commands, c: list of file = $required

	Specifies the name of the command(s), each of
	which MUST use evaluate_parameters as its user
	interface.

-author, a: list of string = $required

	Specifies the author of the software.

-see_also, sa: list of string

	Optionally specifies a list of `See Also'
	references.

-output_path, op: file = man

	Specifies the path name of the directory to hold
	the generated man pages.  Default is `man'.      

}
$c.w_genmp_command.t configure -state disabled

$c.w_genmp_command.t mark set mark_genmp_commands 29.0
$c.w_genmp_command.t mark set mark_genmp_author 35.0
$c.w_genmp_command.t mark set mark_genmp_see_also 39.0
$c.w_genmp_command.t mark set mark_genmp_output_path 44.0

# -commands, c: list of file = $required

frame $c.w_commands -bd 1 -relief sunken
entry $c.w_commands.entry -relief sunken -width 40 -textvariable commands -bg $gentkp_highlight
button $c.w_commands.label -text "commands                               (fl) " -bd 0 -font fixed -command "$c.w_genmp_command.t yview mark_genmp_commands; See_View \"-commands\""
pack $c.w_commands.entry -side right
pack $c.w_commands.label -side left
bind $c.w_commands.entry <KeyPress-Tab> {Tab $tabList; set gentkp_command [ Update ]}
bind $c.w_commands.entry <KeyPress-Return> {Tab $tabList; set gentkp_command [ Update ]}

# -author, a: list of string = $required

frame $c.w_author -bd 1 -relief sunken
entry $c.w_author.entry -relief sunken -width 40 -textvariable author -bg $gentkp_highlight
button $c.w_author.label -text "author                                 (sl) " -bd 0 -font fixed -command "$c.w_genmp_command.t yview mark_genmp_author; See_View \"-author\""
pack $c.w_author.entry -side right
pack $c.w_author.label -side left
bind $c.w_author.entry <KeyPress-Tab> {Tab $tabList; set gentkp_command [ Update ]}
bind $c.w_author.entry <KeyPress-Return> {Tab $tabList; set gentkp_command [ Update ]}

# -see_also, sa: list of string

frame $c.w_see_also -bd 1 -relief sunken
entry $c.w_see_also.entry -relief sunken -width 40 -textvariable see_also
button $c.w_see_also.label -text "see_also                               (sl) " -bd 0 -font fixed -command "$c.w_genmp_command.t yview mark_genmp_see_also; See_View \"-see_also\""
pack $c.w_see_also.entry -side right
pack $c.w_see_also.label -side left
bind $c.w_see_also.entry <KeyPress-Tab> {Tab $tabList; set gentkp_command [ Update ]}
bind $c.w_see_also.entry <KeyPress-Return> {Tab $tabList; set gentkp_command [ Update ]}

# -output_path, op: file = man

frame $c.w_output_path -bd 1 -relief sunken
entry $c.w_output_path.entry -relief sunken -width 40 -textvariable output_path
button $c.w_output_path.label -text "output_path                            (f ) " -bd 0 -font fixed -command "$c.w_genmp_command.t yview mark_genmp_output_path; See_View \"-output_path\""
pack $c.w_output_path.entry -side right
pack $c.w_output_path.label -side left
bind $c.w_output_path.entry <KeyPress-Tab> {Tab $tabList; set gentkp_command [ Update ]}
bind $c.w_output_path.entry <KeyPress-Return> {Tab $tabList; set gentkp_command [ Update ]}

set gentkp_command [ Update ]

eval $realize
eval $tabs

focus [ lindex $tabList [ lsearch $tabList "$c.w_commands.entry" ] ]
