#!/usr/local/bin/wish -f
# edit.tk - simple Tk-based text editor

catch {tk colormodel . color}           ;# colour even on a 2-bit display

option add {*background} grey80
option add {*selectBackground} grey60
option add {*selectBorderWidth} 0
option add {*Scrollbar*foreground}  grey80

######################################################################

global NAME                     ;# user's login name
global HOME                     ;# user's home directory

global PREFS MSGSPREFS		;# user preferences

set NAME $env(USER)
set HOME $env(HOME)

# check for $HOME/.tk/jlibrary.tcl and read it in if it exists.
# this contains library procedures.  it would normally be in $tk_library,
# but we check for its presence here for people who can't put things in
# $tk_library.
#
if {[file isfile "$HOME/.tk/jlibrary.tcl"]} then {
  source "$HOME/.tk/jlibrary.tcl"
}
# now check for some other library files which might be there (again,
# these would normally be in $tk_library):
j:source_config jrichtext.tcl
j:source_config jabout.tcl
j:source_config jbindings.tcl

j:read_standard_prefs

set MSGSPREFS(dir) "/home/it2/js/msgs"
set MSGSPREFS(lbside) left

######################################################################
# msgs_mklistbox w - make a listbox to hold the messages
######################################################################

proc msgs_mklistbox { w } {
  global MSGSPREFS
  global PREFS
  if {[lsearch [array names PREFS] {scrollbarside}] == -1} {
    set PREFS(scrollbarside) right ;# make sure it's defined
  }
  toplevel $w
  wm title $w "System Messages"
  
  scrollbar $w.sb -command "$w.lb yview"
  listbox $w.lb -yscrollcommand "$w.sb set" \
    -geometry 80x10 -setgrid true -exportselection false
  j:buttonbar $w.b -default view -buttons [format {
    {
      view View { msgs_view %s }
    }
    {
      quit Quit { exit 0 }
    }
  } $w.lb]
  
  pack $w.b [j:rule $w] -in $w -side bottom -fill x
  pack $w.sb [j:rule $w] -in $w -side $PREFS(scrollbarside) -fill y
  pack $w.lb -in $w -side top -fill both
  
  msgs_filllistbox $w.lb
}

######################################################################
# msgs_filllistbox lb - fill listbox with summary of messages
######################################################################

proc msgs_filllistbox { lb } {
  global MSGSPREFS

  cd $MSGSPREFS(dir)
  
  foreach file [glob {[0-9]*}] {
    set subject [exec sed -n {s/^Subject://p} $file]
    $lb insert 0 "$file:$subject"
  }
}

######################################################################
# msgs_view lb - view file selected (before colon) in lb
######################################################################

proc msgs_view { lb } {
  set cursel [$lb curselection]
  if {"x$cursel" == "x"} then {return 1}

  set selection [$lb get $cursel]
  set file [lindex [split $selection ":"] 0]
  
  set from [exec sed -n {1,/^$/s/^From:/From:/p} $file]
  set date [exec sed -n {1,/^$/s/^Date:/Date:/p} $file]
  set subj [exec sed -n {1,/^$/s/^Subject:/Subject:/p} $file]
  set body [exec sed {1,/^$/d} $file]
  
  j:more -title "Message $file" -text "$subj\n$date\n$from\n\n$body"
}


wm withdraw .
msgs_mklistbox .msgs
