#!/usr/local/bin/wish8.0
#
set epath "/usr/local/bin/ezmlm"
#
# $Source: /home/nlfm/Working/Layz/RCS/layz,v $
# $Date: 1998/06/11 11:10:20 $
# $Revision: 1.1 $
#
# ----------------------------------------------------------------------
#   AUTHOR:  Lindsay Marshall <lindsay.marshall@newcastle.ac.uk>
# ----------------------------------------------------------------------
# Copyright 1998 The University of Newcastle upon Tyne (see COPYRIGHT)
# ======================================================================
#
proc comp {a1 a2} {
    regexp {(.*)@(.*)} [string tolower $a1] m u1 h1
    regexp {(.*)@(.*)} [string tolower $a2] m u2 h2
    if {![set x [string compare $h1 $h2]]} {
        return [string compare $u1 $u2]
    }
    return $x
}
#
proc dosub {ld} {
    global SUBN epath
    switch {} $SUBN return
    catch {exec [file join $epath ezmlm-sub] $ld $SUBN}
    refresh $ld
}
#
proc refresh {ld} {
    global epath Filter
    .lz.lb delete 0 end
    foreach x [lsort -command comp [exec [file join $epath ezmlm-list] $ld]] {
    	if {[regexp "^$Filter\$" $x]} {
            .lz.lb insert end $x
	}
    }
    .lz.filt.c configure -text "[.lz.lb index end] Entries"
}
#
proc sub {ld} {
    toplevel .sub -clas Layz
    wm title .sub "New subscription"
    grid columnconfigure .sub 1 -weight 1
    grid rowconfigure .sub 0 -weight 1
    #
    grid [label .sub.l -text "E-mail address:"]
    grid [entry .sub.e -textvariable SUBN -width 40 -relief sunken] \
      -row 0 -column 1 -sticky ew -padx 10 -pady 10
    grid [frame .sub.b] - -sticky ew
    grid [button .sub.b.ok -text OK -command "dosub [list $ld] ; destroy .sub" -width 10] \
      -row 0 -column 0
    grid [button .sub.b.can -text Cancel -command "destroy .sub" -width 10] \
      -row 0 -column 1
}
#
proc doed {ld idx nm} {
    global epath
    upvar #0 Ed$idx evar
    switch -- $nm $evar return
    catch {exec [file join $epath ezmlm-unsub] $ld $nm}
    switch {} $evar {} default {
	catch {exec [file join $epath ezmlm-sub] $ld $nm}
    }
    refresh $ld
    unset evar
}
#
proc edit {ld} {
    foreach y [.lz.lb curselection] {
        set nm [.lz.lb get $y]
	global Ed$y
	set Ed$y $nm
	toplevel [set w .ed$y] -clas Layz
	wm title $w "Edit $nm"
	grid columnconfigure $w 1 -weight 1
	grid rowconfigure $w 0 -weight 1
    #
	grid [label $w.l -text "E-mail address:"]
	grid [entry $w.e -textvariable Ed$y -width 40 -relief sunken] \
	  -row 0 -column 1 -sticky ew -padx 10 -pady 10
	grid [frame $w.b] - -sticky ew
	grid [button $w.b.ok -text OK -command "doed [list $ld] $y [list $nm]; destroy $w" -width 10] \
	  -row 0 -column 0
	grid [button $w.b.can -text Cancel -command "destroy $w ; unset Ed$y" -width 10] \
	  -row 0 -column 1
    }
}
#
proc unsub {ld} {
    global epath
    set bp {}
    foreach x [.lz.lb curselection] {
        if {[catch {exec [file join $epath ezmlm-unsub] $ld [.lz.lb get $x]} msg]} {
	    puts stderr "Error : $msg"
	}
	set bp [linsert $bp 0 0 $x]
    }
    foreach x $bp {.lz.lb delete $x}
}
#
proc public {ld} {
    global publ
    if {$publ} {
        catch {close [open [file join $ld public] w]}
    } {
        catch {file delete [file join $ld public]}
    }
}
#
proc archv {ld} {
    global publ
    if {$publ} {
        catch {close [open [file join $ld archived] w]}
    } {
        catch {file delete [file join $ld archived]}
    }
}
#
proc isList {dir} {
    if {[file exists $dir]} {
        if {[file isdirectory $dir]} {
	    if {[file exists [file join $dir bouncer]]} { return 1 }
	}
    }
    return 0
}
#
set version 1
set patchlevel 0
#
wm withdraw .
#
switch -- $argv {} - -h - -? {
    puts stderr "usage: $argv0 <list name>"
    exit 1
} -v {
    puts stdout "layz Version $version, Patchlevel $patchlevel"
    exit 0
}
#
set who [glob ~]
#
set lname [lindex $argv 0]
#
if {[regexp {^([^-]+)-(.*)} $lname m owner lrest]} {
    if {![catch {set who [glob ~$owner]}]} {
        set lname $lrest
    }
}

if {![isList [set ld [file join $who $lname]]]} {
    if {![isList [set ld [file join ~alias $lname]]]} {
        puts stderr "Cannot find list called \"$lname\"."
        exit 1
    }
}
set ld [glob $ld]
set archv [file exists [file join $ld archived]]
set publ [file exists [file join $ld public]]
#
toplevel .lz -class Layz
wm title .lz "Mailing List $lname"
grid [frame .lz.flags] - -sticky ew
grid [checkbutton .lz.flags.arc -text Archived \
  -variable archv -command "archv $ld" -width 12] -row 0 -column 0
grid [checkbutton .lz.flags.pub -text Public \
  -variable publ -command "public $ld"  -width 12] -row 0 -column 1
#
grid [frame .lz.filt] - -sticky ew
grid columnconfigure .lz.filt 1 -weight 1
grid [label .lz.filt.l -text Filter:] 
set Filter .*
grid [entry .lz.filt.e -textvariable Filter -relief sunken -width 40]\
   -sticky ew -row 0 -column 1
grid [label .lz.filt.c -text {} -width 16] -row 0 -column 2
bind .lz.filt.e <Return> "refresh $ld"
#
set lrow 2
scrollbar .lz.vs -command ".lz.lb yview"
scrollbar .lz.hs -command ".lz.lb xview" -orient horizontal
listbox .lz.lb -xscrollcommand ".lz.hs set" -yscrollcommand ".lz.vs set" \
  -width 80 -height 24 -selectmode extended
bind .lz.lb <Double-1> "edit $ld"
#
grid columnconfigure .lz 0 -weight 1
grid rowconfigure .lz $lrow -weight 1
#
grid .lz.lb -sticky nsew
grid .lz.vs -row $lrow -column 1 -sticky ns
grid .lz.hs -column 0 -sticky ew
grid [frame .lz.btn] - -sticky ew
button .lz.btn.del -text Unsubscribe -command "unsub $ld" -width 12
button .lz.btn.add -text Subscribe -command "sub $ld" -width 12
button .lz.btn.ref -text Refresh -command "refresh $ld" -width 12
button .lz.btn.quit -text Quit -command "exit 0" -width 12
grid .lz.btn.del .lz.btn.add .lz.btn.ref .lz.btn.quit

#
refresh $ld
