#!/usr/bin/X11/wish -f
# The above path must be the one to wish

# The Following Globals setup the program for your system
#
# PATH TO THIS PROGRAM AND ALL ITS COMPONENTS
set MAINPATH .

# FILE NAME OF THE DATABASE
set recordfile records.rfd

# THE Following indicates the system sort that is used by you system
# Comment out the one that doesn't apply.
# Note; SystemV works on Suns and GNU on Linux.
set SORTER  	GNU 
#set SORTER 	SystemV

# THANK YOU; LEAVE THE REST TO ME!



##############
# GLOBALS

global Current

set current(comp) ""
set current(titl) ""
set current(per1) ""
set current(per2) ""
set current(per3) ""
set current(locn) ""
set current(medm) ""
set current(othr) ""
global CardBox
global CurrentField
global fields
global RbasePipe
set CurrentField [list comp titl per1 per2 per3 locn medm othr]
set fields $CurrentField

# Filename for DataBase
global recordfile
set recordfile $MAINPATH/$recordfile
# Filename for AwkScript
global awkfile
set awkfile "$MAINPATH/search.awk"
#Status Variables
global SearchStatus
set SearchStatus "search"
global SearchSelect
set SearchSelect "start"
global AddStatus 
set AddStatus 0

##############
# Set the rbase task running
global RbasePipe
set RbasePipe [open "|rbase -m $recordfile" "w+"]

##############
#Load Modules
source $MAINPATH/browser.tk
source $MAINPATH/search.tk
source $MAINPATH/history.tk
source $MAINPATH/recproc.tk
source $MAINPATH/addproc.tk
source $MAINPATH/delproc.tk
source $MAINPATH/askproc.tk

############
# Callback to the search menu
proc ToggleSearchMode {} {
	global SearchStatus
	global SearchSelect

   # Make sure that there mode is to be toggled 	
   if { $SearchStatus != $SearchSelect } {
	if { $SearchSelect == "start" } {
		cardBox show
		ShowEntry show
		set SearchStatus search
		set SearchSelect search
	
	} \
	elseif { $SearchSelect == "browse" } {
		# Check if returning to search mode
		if { $SearchStatus == "none" } {
			 ModeChange
			 cardBox show
			 ShowBrowser show
		} else {
			ShowEntry noShow
			ShowBrowser show
		}
		set SearchStatus browser
		 
	} \
	elseif { $SearchSelect == "search" } {
		# Check if returning to search mode
		if { $SearchStatus == "none" } { 
			 ModeChange
			 cardBox show
			 ShowEntry show
		} else {
			ShowBrowser noShow
			ShowEntry show
			}
		set SearchStatus search
	} else { puts "error in ToggleSearchMode: invalid arg" }
   }
}

############
# Change of Mode; wipe out existing windows
proc ModeChange {} {
	global SearchStatus
	global AddStatus
	# Use catch to supress the error if the window isn't packed
	catch {destroy .log}
	catch {cardBox noShow }
	catch {ShowBrowser noShow }
	catch {ShowEntry noShow }
	catch {cardBox noShow }
        catch { destroy .io }
	set SearchStatus none
	if { $AddStatus==1 } { 
			   #Refresh lists
			   # Commented out because cause delays
			   # The refresh button can be used if necessary
			   # BrowserRefresh 
			   set AddStatus 0 }\
	elseif  {$AddStatus==2} {
			   #Refresh child list only
			   # on Modification
			    BrowserRefresh 0
			    MakeCurrSelec
			   set AddStatus 0 }

}
	


############
# CREATE THE MENU BAR
proc menuBar {} {
	global AddStatus
	# The background frame
	frame .mBar -width 500 -borderwidth 3 -relief ridge
	 
	menubutton .mBar.search -text Search -menu .mBar.search.m -relief raised
	menubutton .mBar.log -text "History" -menu .mBar.log.m -relief raised
 	menubutton .mBar.mod -text "Add/Modify" -menu .mBar.mod.m -relief raised
	button .mBar.quit -text Exit -command {
	    puts $RbasePipe q
	    flush $RbasePipe
	    destroy .
	}

	menu .mBar.search.m 
		.mBar.search.m add radiobutton -label "Browse" \
			-variable SearchSelect -command { ToggleSearchMode } \
			-value browse
		.mBar.search.m add radiobutton -label "Enter Criteria"\
			-variable SearchSelect -command { ToggleSearchMode } \
			-value search
	menu .mBar.mod.m
	   	.mBar.mod.m add command -label "Modify Record" \
					-command { 
						   ModeChange
						   RecordIn MOD
						   set AddStatus 1 }
		.mBar.mod.m add command -label "Add New Record" \
					-command {  
						   ModeChange
						   RecordIn ADD 
						   set AddStatus 1 }
		.mBar.mod.m add command -label "Delete Record" \
					-command {  
						   ModeChange
						   RecordIn DEL 
						   set AddStatus 1 }
	menu .mBar.log.m
		.mBar.log.m add command -label "Add to History" \
			-command { LogAdd }
		.mBar.log.m add command -label "View History" \
			-command { ModeChange
				   LogView }
		.mBar.log.m add command -label "Clear History" \
			-command { LogClear }
	pack .mBar.search .mBar.log .mBar.mod .mBar.quit \
		-fill both -expand 1 -side left

	#Place at top of the window
	pack .mBar -fill x 
}

###########
# MAIN BIT
wm title . "Music Library Catalogue"
wm geometry . 500x470
wm minsize . 500 1
wm maxsize . 500 430
ShowBrowser init
ShowEntry init
cardBox init
menuBar
ToggleSearchMode

#######
