#! /set/misc/local/bin/wish -f

#
# Script to display the comments for a given workspace in it's own window
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.

set cmt_file "./Codemgr_wsdata/Comments"

text .cmt -relief raised -bd 2 -yscrollcommand ".scroll set"
scrollbar .scroll -command ".cmt yview"
pack .scroll -side right -fill y
pack .cmt -side top

#
# Create edittable text field for new comments
#
label .new_entry_label -text "New comment:"
entry .new_entry -width 60 -relief sunken -bd 2 -textvariable new_cmt
pack .new_entry_label .new_entry -side left -padx 1m -pady 2m -fill x

#
# Create and pack buttons
#
button .exit_btn -command exit -text "Exit"
# button .date_btn -command put_date -text "Date"
button .add_btn -command add_new_comment -text "Add"
pack .add_btn .exit_btn -side left -expand 1

#
# Allow this script to be run from anywhere within the workspace
#
set not_found 1
 
while { $not_found == 1  } {
    set ws_name [ pwd ]
    if { [ file exists $ws_name/Codemgr_wsdata ] != 0 } {
        set not_found 0
    } else {
        cd "$ws_name/.."
        set ws_name [ pwd ]
    }    
}
 
proc put_date {} {
    .new_entry insert 0 [ date ]
}
 
proc loadFile file {
        global ws_name
        .cmt delete 1.0 end
        .cmt insert end "$ws_name: \n\n"
        set f [ open $file a+ ]
        seek $f 0
        while { ![eof $f]} {
                .cmt insert end [ read $f 1000]
        }
        close $f
}
 
loadFile $cmt_file

#
# Append the contents of the new_cmt textfield to the comment file
#
proc add_new_comment {} {
    global cmt_file new_cmt .new_entry
 
    #
    # Append $new_cmt to comments file
    #
    set f [ open $cmt_file a ]
    puts $f "$new_cmt\n"
    close $f
    
    # reload the new comment file
    loadFile $cmt_file
 
    #
    # Clear the entry field
    #
    .new_entry delete 0 end
}

