#!/usr/coda/etc/wish -f

#
# A menu to pass user-initiated events up to the advice monitor
#
# History :
#  95/4/15 : mre@cs.cmu.edu : code
#

#
#
#

proc Quit {} {
	destroy .
	exit 0
}


#
# Routines for interacting with the advice monitor...  
# (These are common to all of the various options.)
#

proc CreateCommandFile {line} {
	global CommandFileName

	set outFile [open $CommandFileName w]
	puts $outFile [format "%s" $line]
	close $outFile
}

proc SignalAdviceMonitor {} {
	global argv

	# Set a USR1 signal to the advice monitor
	exec kill -USR1 [lindex $argv 0]
}

proc WaitForCommandFileRemoval {} {
	global CommandFileName

	set test [file exists $CommandFileName]
	while { $test == 1 } {
		# Command File still exists
		after 1000
		set test [file exists $CommandFileName]
	}
	# Command file has been removed
}


# 
# Routines for implementing the various options...
#

proc UserPatienceParameters {} {

}

proc RequestMissList {} {
	global RequestMissList 

	CreateCommandFile $RequestMissList
	SignalAdviceMonitor
	WaitForCommandFileRemoval
}

proc VenusStoppedHoardAdvice {} {
	global HoardWalkAdvice

	set HoardWalkAdvice 0
}

proc RequestHoardAdvice {} {
	global SolicitHoardAdvice
	global UnsolicitHoardAdvice
	global HoardWalkAdvice

	if {$HoardWalkAdvice} {
		puts stdout [format "Hoard Walk Advice --> %s" $SolicitHoardAdvice]
		CreateCommandFile $SolicitHoardAdvice
	} else {
		puts stdout [format "Hoard Walk Advice --> %s" $UnsolicitHoardAdvice]
		CreateCommandFile $UnsolicitHoardAdvice
	}

	SignalAdviceMonitor
	WaitForCommandFileRemoval
}

proc CreateMenuBar {} {

;	frame .frame -relief flat 
;	pack append . .frame {top filly frame center}
;	frame .mbar -relief raised -borderwidth 2
;	pack before .frame .mbar {top fillx}

	pack append . .mbar {top}

	menubutton .mbar.variables -text Variables -underline 0 -menu .mbar.variables.menu
	menu .mbar.variables.menu
	.mbar.variables.menu add command \
		-label "User Patience Parameters" \
		-command "UserPatienceParameters"


	menubutton .mbar.weakmiss -text WeakMiss -underline 0 -menu .mbar.weakmiss.menu
	menu .mbar.weakmiss.menu
	.mbar.weakmiss.menu add checkbutton \
		-label "Hoard Walk Advice" \
		-variable HoardWalkAdvice \
		-command RequestHoardAdvice
	.mbar.weakmiss.menu add checkbutton \
		-label "Long Fetch Queries" \
		-variable LongFetchQueries
	.mbar.weakmiss.menu add separator
	.mbar.weakmiss.menu add command \
		-label "Request list of cache misses" \
		-command "RequestMissList"

	pack append .mbar \
		.mbar.variables {left} \
		.mbar.weakmiss {right}


	tk_menuBar .mbar .mbar.variables .mbar.weakmiss
	focus .mbar

}


#
# Check that we have precisely 1 arguments.
#

if { $argc != 1 } then {
	puts "Usage: user_initiaited pid"
	puts "  where pid is the process ID of the advice monitor"
	exit 1
}

# N.B. The following global variables MUST be consistent with those of 
# the same names found in advice_srv.h
set CommandFileName /tmp/advice_srv_command_file
set SolicitHoardAdvice a
set UnsolicitHoardAdvice b
set RequestMissList c
set RequestLongFetchQuery d
# End of global variables which MUST be consistent with those in advice_srv.h

#
# Create all windows
#
CreateMenuBar

wm geometry . +0+0
wm title . "Advice Monitor"

#
# Now, wait for user actions...
#

