#!/usr/bin/env wish8.3
# jmsgs - msgs viewer
#
###############################################################################
#  Copyright 1992-2001 by Jay Sekora.  This file may be freely distributed,   #
#  modified or unmodified, for any purpose, provided that this copyright      #
#  notice is retained verbatim in all copies and no attempt is made to        #
#  obscure the authorship of this file.  If you distribute any modified       #
#  versions, I ask, but do not require, that you clearly mark any changes     #
#  you make as such and that you provide your users with instructions for     #
#  getting the original sources.                                              #
###############################################################################
## begin boiler_header

if {[info exists env(AQTOOLS_LIB)]} {
  set aq_library $env(AQTOOLS_LIB)
  set aq_pkg [file join $env(AQTOOLS_LIB) pkg]
} else {
  set aq_library /usr/lib/aq
  set aq_pkg [file join $aq_library pkg]
}

# add the aq library to the library search path:

set auto_path  [concat  [list $aq_pkg]  [list $aq_library]  $auto_path]

# check for ~/.tk and prepend it to the auto_path if it exists.
# that way the user can override and customise the aq libraries.

if {[file isdirectory ~/.tk]} then {
  set auto_path [concat [list [glob ~/.tk]] $auto_path]
}

## end boiler_header

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 J_PREFS JMSGS_PREFS	;# user preferences

::aq::aq_init aqmsgs		;# prefs, libraries, bindings...

#set JMSGS_PREFS(dir) "/usr/spool/msgs"
set JMSGS_PREFS(dir) "/home/js/mh/humour"
set JMSGS_PREFS(lbside) left

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

proc msgs_mklistbox { w } {
  global JMSGS_PREFS
  global J_PREFS
  if {[lsearch [array names J_PREFS] {scrollbarside}] == -1} {
    set J_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" \
    -setgrid true -exportselection false
  $w.lb configure -width 80 -height 10
  ::aq::buttonbar $w.b -buttons [format {
    {
      view View { msgs_view %s }
    }
    {
      quit Quit { exit 0 }
    }
  } $w.lb]
  
  pack $w.b -in $w -side bottom -fill x
  pack $w.sb -in $w -side $J_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 JMSGS_PREFS

  cd $JMSGS_PREFS(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]
  
  ::aq::more -title "Message $file" -text "$subj\n$date\n$from\n\n$body"
}


wm withdraw .
msgs_mklistbox .msgs

