#!/afs/ece/usr/tcl/bin/wish -f
# The next line is executed by most shells, but not Tcl \
wish $0 $*

#
# Mail browsing program. (single-folder)
# Takes single arg of folder/msg, so can be used as -showproc for
# show, next, or prev


proc filter_headers {w} {
  global body_start
  set important_headers [$w tag ranges important]
  set length [llength $important_headers]
  incr length -1
  if {($length < 0)} {$w delete 1.0 $body_start ; return}
  $w delete [lindex $important_headers $length] $body_start

  for {incr length -2} {$length >= 0} {incr length -2} {
    $w delete [lindex $important_headers $length] \
      [lindex $important_headers [expr "$length+1"]]
  }

  $w delete 1.0 [lindex $important_headers 0]
}

proc check_buttons {w} {
  set f [th_frame $w]
  global TH
  if {($TH(File,Browse,Index) == 0)} {$f.prev configure -state disabled
  } else {$f.prev configure -state normal}
  if {($TH(File,Browse,Index) == [expr $TH(File,Browse,Length) -1])} {
    foreach b "$f.next $f.last" {$b configure -state disabled}
  } else {foreach b "$f.next $f.last" {$b configure -state normal}}
}

proc show_another {w d} {
  $w configure -state normal
  th_file_move $w $d
  check_buttons $w
  add_section_tags $w
  filter_headers $w
  wm title [winfo toplevel $w] "Mail Read"
  wm iconname [winfo toplevel $w] "MR"
  $w configure -state disabled
}

proc do_refile {w new_folder} {
  global TH
  set old $TH(File,$w)
  if {$TH(File,Browse,Index) == [expr $TH(File,Browse,Length) - 1]} {
    bell
    set d -1
  } else {set d 1}
  show_another $w $d
  set TH(File,Browse,Dir) ""
  exec refile -src +[file tail [file dirname $old]] [file tail $old] +$new_folder
}

proc make_move_menu {w m} {
  global mail_dir folder
  set folders [exec folders -fast]
  menu $m
  foreach fldr $folders {$m add command -label $fldr \
       -command "do_refile $w $fldr"
}}

proc show_window {w} {
  set f [th_frame $w]
  wm title .syme1 "Mail: Read"
  wm iconname .syme1 "MR"
  pack propagate $f 1
  button $f.prev -text Previous -command {show_another .syme1.t -1}
  button $f.next -text Next     -command {show_another .syme1.t 1}
  button $f.last -text Last     -command {show_another .syme1.t [expr $TH(File,Browse,Length) - $TH(File,Browse,Index) -1]}
  button $f.repl -text Reply    -command {exec repl +$folder [file tail $TH(File,.syme1.t)] &}
  button $f.forw -text Forward  -command {exec forw +$folder [file tail $TH(File,.syme1.t)] &}
  menubutton $f.move -text Move -menu $f.move.m
  make_move_menu $w $f.move.m

  foreach window {last next prev move repl forw} {
    pack $f.$window -side right -expand yes -fill x
  }
  pack propagate $f 0
}


if {([string match /* [info script]])} {
  set bethmail_dir [file dirname [info script]]
} else {set bethmail_dir [file dirname [pwd]/[info script]]}
cd $bethmail_dir

if {$argv != ""} {
  set folder [file dirname $argv]
} else {set folder inbox
}

set w .syme1.t
set f .syme1.t_fm
source ../aux/mailth.tcl

if {$argv != ""} {
  set this [file tail $argv]
} else {
  set list [exec ls $mail_dir/$folder]
  set this [lindex $list [expr [llength $list] -1]]
}

set argv "-i -c \"th_add_module paragrath\" -a $mail_dir/$folder/$this"
set argc [llength $argv]
source elsbeth

show_window $w
show_another $w 0
