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

#  This Tcl/Tk Motif GUI wrapper for program `disci' created by version 2.1
#  of generate_tk_program.
#
#  Stephen O. Lidie, Lehigh University.  Wed May  4 11:16:33 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 output "stdout"
set output0 "$output"
set display_option "full"
set display_option0 "$display_option"
set do_page "TRUE"
set do_page0 "$do_page"
set files ""
set files0 "$files"



proc Reset_Parameters {} {

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

	global output output0
	set output "stdout"
	set output0 "$output"
	global display_option display_option0
	set display_option "full"
	set display_option0 "$display_option"
	global do_page do_page0
	set do_page "TRUE"
	set do_page0 "$do_page"
	global files files0
	set files ""
	set files0 "$files"

}

Reset_Parameters

set gentkp_command "disci"
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.


	# Handle trailing file_name(s).

	upvar #0 files file_list
	set disci_files $file_list
	if { $file_list == "stdin" } {
		set disci_files ""
	}

	# Perform application specific command line argument processing here.

		# Maybe something here?

	set gentkp_command "disci"

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

	foreach parameter { "output" "display_option" "do_page" } {
		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 $disci_files"
	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 "xdisci.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 `disci' created by version 2.1 of generate_tk_program.\n\nStephen O. Lidie, Lehigh University.  Wed May  4 11:16:33 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
        global files
	if { $files == "stdin" } {
		set execute [ tk_dialog .stdin "Alert" "Is standard input really the file you want to process?" \
			warning 0 Cancel OK ]
	} else {
		foreach file "$files" {
			if { ! [ file exists $file ] } {
###				tk_dialog .required "Alert" "File $file doesn't exist." warning 0 Cancel
###				Reset_OK_Button
###				return
			}
		}
	}
	if { $execute == 0 } {
		Reset_OK_Button
		return
	}
	if { $execute == 1 } {
		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 . "xdisci"
wm iconname . "xdisci"
#wm iconbitmap . @/usr/local/lib/tcl+tk_lucc/sqtp.xbm
wm geometry . +400+50

set realize "pack $c $c.menu $c.w_disci_command $c.w_output $c.w_display_option $c.w_do_page $c.w_files $c.see -side top -fill x"
set tabs "set tabList \" $c.w_output.entry $c.w_files.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  -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_disci_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_disci_command
text $c.w_disci_command.t -relief raised -bd 1 -yscrollcommand "$c.w_disci_command.s set" -setgrid true -height 10 -font fixed
scrollbar $c.w_disci_command.s -relief flat -command "$c.w_disci_command.t yview"
pack $c.w_disci_command.s -side right -fill y
pack $c.w_disci_command.t -expand yes -fill both
$c.w_disci_command.t insert 0.0 \
{Command Source:  /usr/local/bin/disci

Message Module Name:  disci.mm

display_command_information, disci

	Displays information about a command.

	  Example:
	
	    disci -do f genpdt

        This example uses display_option full to see further information.
        Currently this additional data includes where the command was
        found and the name of the evaluate_parameters message module,
	text describing the command, as well as parameter help, if
	available.

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.

-output, o: file = stdout

	Specifies the path name of the file to receive the Display
	Command Information output.

-display_option, do: key brief, b, full, f, usage, u, keyend = D_DISCI_DO, brief

	Specifies the level of help desired.  

-do_page, dp: boolean = D_DISCI_DO_PAGE, TRUE

	Specifies whether `display_command_information' pages help
	output.  If paging is enabled the environment variable
	MANPAGER (or PAGER) is used to control the display of the
	generated help information.  If defined and non-null, the
	value of the environment variable is taken as the name of
	the program to pipe the help output through.  If no paging
	program is defined then the program `more' is used.

	The boolean environment variable D_DISCI_DO_PAGE can be
	set to FALSE/NO/OFF/0, any case, to disable this automatic
	paging feature (or you can set your paging program to
	`cat').

A Program_Name is required by this command.

}
$c.w_disci_command.t configure -state disabled

$c.w_disci_command.t mark set mark_disci_output 28.0
$c.w_disci_command.t mark set mark_disci_display_option 33.0
$c.w_disci_command.t mark set mark_disci_do_page 37.0
$c.w_disci_command.t mark set mark_disci_trailing_files 52.0

# -output, o: file = stdout

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

# -display_option, do: key brief, b, full, f, usage, u, keyend = D_DISCI_DO, brief

frame $c.w_display_option -bd 1 -relief sunken
button $c.w_display_option.label -text "display_option                         (k ) " -bd 0 -font fixed -command "$c.w_disci_command.t yview -pickplace mark_disci_display_option; See_View \"-display_option\""
frame $c.w_display_option.subframe
pack $c.w_display_option.label -in $c.w_display_option -side left -anchor nw
pack $c.w_display_option.subframe -in $c.w_display_option -side left
radiobutton $c.w_display_option.brief -text brief -variable display_option -relief flat -value brief -command {set gentkp_command [ Update ]}
pack $c.w_display_option.brief -in $c.w_display_option.subframe -side left
radiobutton $c.w_display_option.b -text b -variable display_option -relief flat -value b -command {set gentkp_command [ Update ]}
pack $c.w_display_option.b -in $c.w_display_option.subframe -side left
radiobutton $c.w_display_option.full -text full -variable display_option -relief flat -value full -command {set gentkp_command [ Update ]}
pack $c.w_display_option.full -in $c.w_display_option.subframe -side left
radiobutton $c.w_display_option.f -text f -variable display_option -relief flat -value f -command {set gentkp_command [ Update ]}
pack $c.w_display_option.f -in $c.w_display_option.subframe -side left
radiobutton $c.w_display_option.usage -text usage -variable display_option -relief flat -value usage -command {set gentkp_command [ Update ]}
pack $c.w_display_option.usage -in $c.w_display_option.subframe -side left
radiobutton $c.w_display_option.u -text u -variable display_option -relief flat -value u -command {set gentkp_command [ Update ]}
pack $c.w_display_option.u -in $c.w_display_option.subframe -side left

# -do_page, dp: boolean = D_DISCI_DO_PAGE, TRUE

frame $c.w_do_page -bd 1 -relief sunken
button $c.w_do_page.label -text "do_page                                (b ) " -bd 0 -font fixed -command "$c.w_disci_command.t yview mark_disci_do_page; See_View \"-do_page\""
frame $c.w_do_page.subframe
pack $c.w_do_page.label -in $c.w_do_page -side left -anchor nw
pack $c.w_do_page.subframe -in $c.w_do_page -side left
radiobutton $c.w_do_page.true -text yes -variable do_page -relief flat -value TRUE -command {set gentkp_command [ Update ]}
radiobutton $c.w_do_page.false -text no -variable do_page -relief flat -value FALSE -command {set gentkp_command [ Update ]}
pack $c.w_do_page.true $c.w_do_page.false -side left

# A Program_Name is required by this command.

frame $c.w_files -bd 1 -relief sunken
entry $c.w_files.entry -relief sunken -width 40 -textvariable files -bg $gentkp_highlight
button $c.w_files.label -text "Required File Name(s)                       " -bd 0 -font fixed -command "$c.w_disci_command.t yview -pickplace mark_disci_trailing_files; See_View end_of_MM"
pack $c.w_files.entry -side right
pack $c.w_files.label -side left
bind $c.w_files.entry <KeyPress-Tab> {Tab $tabList; set gentkp_command [ Update ]}
bind $c.w_files.entry <KeyPress-Return> {Tab $tabList; set gentkp_command [ Update ]}

set gentkp_command [ Update ]

eval $realize
eval $tabs

focus [ lindex $tabList 0 ]

