#!/icem3.1/bin/wish-3.3 -f
#
# Usage: remove_domains confname geoname
#
# Copyright (c) 1993 - 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com

option add *font variable

set confname [lindex $argv 0]
set geoname [lindex $argv 1]

set domains {}
foreach d [glob -nocomplain mulcad/$confname/$geoname/domains/*] {
    set d [file tail $d]
    lappend domains $d
    set remove($d) 0
}

proc dcmp {d1 d2} {
    if {[regexp {.*\.([0-9]+)$} $d1 dummy n1] &&
	[regexp {.*\.([0-9]+)$} $d2 dummy n2]} {
	    return [expr {$n1 - $n2}]
	} else {
	    if {$d1 == $d2} {
		return 0
	    } elseif {$d1 > $d2} {
		return 1
	    } else {
		return  -1
	    }
	}
}

set domains [lsort -command dcmp $domains]

message .m -aspect 300 -text "Select the domains to be deleted by clicking \
on them in the left list.  The right list shows the domains that will be removed.  \
To take a domain out of the set to be removed, click on its name in the right list.  \
The \"All\" button puts all existing domains in the list to be removed, \
and the \"None\" button takes them all out.  Click on \"Remove domains\" to \
remove the listed domains."

frame .f1 -relief raised -borderwidth 2
frame .f2 -relief raised -borderwidth 2
listbox .f1.l -width 20 -height 16 -relief sunken
scrollbar .f1.s -command ".f1.l yview" -orient v -relief sunken
label .f1.lab -text "Existing domains" -relief flat
listbox .f2.l -width 20 -height 16 -relief sunken
scrollbar .f2.s -command ".f2.l yview" -orient v -relief sunken
label .f2.lab -text "To be removed" -relief flat
frame .b
button .b.all -text "All" -command "put_all 1"
button .b.none -text "None" -command "put_all 0"
button .b.okay -text "Remove domains" -command "remove_domains; exit 0"
button .b.cancel -text "Cancel" -command "exit 0"

pack .m -side top
pack .b -side bottom -fill both -padx 5 -pady 5
pack .f1 .f2 -side left -expand 1 -padx 5 -pady 5
pack .f1.lab -side top
pack .f1.l .f1.s -side left -fill both -expand 1
pack .f2.lab -side top
pack .f2.l .f2.s -side left -fill both -expand 1
pack .b.all .b.none .b.okay .b.cancel -side left -fill both -expand 1

wm geometry . +200+200
wm title . "Remove domains"

foreach d $domains {
    .f1.l insert end $d
}

bind .f1.l <Button-1> "put_one %y 1"
bind .f1.l <Button1-Motion> ""
bind .f1.l <ButtonRelease-1> ""
bind .f2.l <Button-1> "put_one %y 2"
bind .f2.l <Button1-Motion> ""
bind .f2.l <ButtonRelease-1> ""

proc put_all {in} {
    global remove
    
    if $in {
	set nn [.f1.l size]
	for {set i 0} {$i < $nn} {incr i} {
	    set d [.f1.l get $i]
	    if {$d == ""} break
	    if !$remove($d) {
		set remove($d) 1
		.f2.l insert end $d
	    }
	}
    } else {
	while 1 {
	    set d [.f2.l get 0]
	    if {$d == ""} break
	    set remove($d) 0
	    .f2.l delete 0
	}
    }
}

proc put_one {pos which} {
    global remove confname geoname
    
    set num [.f$which.l nearest $pos]
    set d [.f$which.l get $num]
    if {$d == ""} return
    .f$which.l select set $num
    case $which in  {
	1 {
	    if !$remove($d) {
		set remove($d) 1
		.f2.l insert end $d
	    }
	}
	2 {
	    if $remove($d) {
		set remove($d) 0
		.f2.l delete $num
	    }
	}
    }
}

proc remove_domains {} {
    global remove confname geoname
    
    foreach d [array names remove] {
	if $remove($d) {
	    exec rm -f mulcad/$confname/$geoname/domains/$d
	}
    }
}
