# Aufräumen
#
# Copyright (C) 2000  G. Lamprecht, W. Lotz, R. Weibezahn; IWD, Bremen University

proc clear {f} {

proc akanzeigen {s1} {
  global vv Wa aufmax aufsuff auftoggle
  writescr0 $Wa.d.tt "$s1\n\n $vv(ak2) $aufmax\n $vv(ak3) $aufsuff\n $vv(ak4) $auftoggle\n\n"
  $Wa.d.tt yview 0
}

proc akEnd {} {
  global Wa auftoggle nmax ak_v
  set auftoggle ""; for {set n 0} {$n<$nmax} {incr n} {lappend auftoggle $ak_v($n)}
  destros $Wa; unlock_list
}

proc akChkBut {wn} {# checkbutton wn is clicked
 global Wa ak_h delsuff deldat dd dmaxmax aufmax daus df1 df2
 if {$ak_h($wn)==1} {set ak_h($wn) 0} else {set ak_h($wn) 1}
 for {set dm 0} {$dm<$dmaxmax} {incr dm} {
   if [string match "$delsuff($wn)" $deldat($dm)] \
	{if {($dd($dm)==0)||($dd($dm)==1)} {set dd($dm) $ak_h($wn)}}
 }
 $Wa.c.2.fr.f.f.li delete 0 end 
 set daus 0; set df1 -1; set df2 -1
 for {set dm 0} {$dm<$dmaxmax} {incr dm} {
   if {$dd($dm)==1} {
     if {$df1==-1} {set df1 $dm}
     if {$daus<$aufmax} {$Wa.c.2.fr.f.f.li insert end $deldat($dm); incr daus; set df2 $dm}
   }
 }
}

proc akExclFile {f} {# exclude file from delete list box
 global Wa deldat dd df1 df2
 set w $Wa.c.2.fr.f.f.li
 if {$f!=""} {
   set d $df1; while {$d<=$df2} {if {[string match "$deldat($d)" $f]} {set dd($d) -1}; incr d}
   $w delete 0 end
   set d $df1; while {$d<=$df2} {if {$dd($d)==1} {$w insert end $deldat($d)}; incr d}
 }
}

proc akDelFiles {w} {# deletes displayd files, messages -> w
 global Wa vv dd df1 df2 deldat dmaxmax daus aufmax
 if {$df1<0} {
   writescr $w "$vv(ak5)\n"
 } else {
   writescr0 $w "$vv(ak6)"
   for {set d $df1} {$d<=$df2} {incr d} {
     if {$dd($d)==1} {
	set f $deldat($d)
	writescr $w "\n $f"
	 if {[file writable $f]} {FileDelete $f} \
	 elseif {[file exists $f]} {writescr $w "  $vv(ak7)"}
	set dd($d) -2
     }
   }
   writescr $w "\n"
   $Wa.c.2.fr.f.f.li delete 0 end
   set dm $df2; set daus 0; set df1 -1; set df2 -1
   while {$dm<$dmaxmax} {
     if {$dd($dm)==1} {
	if {$df1==-1} {set df1 $dm}
	if {$daus<$aufmax} {$Wa.c.2.fr.f.f.li insert end $deldat($dm); incr daus; set df2 $dm}
     }
     incr dm
   }
 }
}


global vv Wa auftoggle aufsuff geom ak_h delsuff nmax deldat dd dmaxmax daus aufmax df1 df2
global quickfile

toplevel $Wa;  wm title $Wa "$vv(akvor)";  wm minsize $Wa 0 0
if [info exists geom($Wa)] {wm geometry $Wa $geom($Wa)}

set tyh 18
set dir [pwd]


# Bereich a fuer erste Buttons

insertTopButtons $Wa rm_h akEnd
Bind3HF $Wa.a.e rm_quit $Wa.d.tt
button $Wa.a.g -text "$vv(ag)" -command \
	{vstr_cleanup; destros $Wa; clear .d.tt; akanzeigen "$vv(ausg1)"}
Bind3HF $Wa.a.g rm_grund $Wa.d.tt
pack configure $Wa.a.e $Wa.a.h $Wa.a.l $Wa.a.g -side left -padx 3 -pady 3


# Bereich b fuer Anzeigen

frame $Wa.b; pack configure $Wa.b -pady 13 -anchor w
Bind3HF $Wa.b rm_anzeig $Wa.d.tt
label $Wa.b.dv -text "$vv(bdv)  $dir" -width 90 -anchor w; pack configure $Wa.b.dv
Bind3HF $Wa.b.dv rm_anzeig $Wa.d.tt


# Bereich c fuer Suffix- und Datei-Listen

frame $Wa.c; pack configure $Wa.c -anchor w -fill x -expand yes
frame $Wa.c.1; frame $Wa.c.2
pack configure $Wa.c.1 $Wa.c.2 -side left -ipadx 40 -fill x -expand yes

# Bereich c1 fuer Suffix-Liste
label $Wa.c.1.g -text "$vv(akc1g)"; pack configure $Wa.c.1.g -anchor n
Bind3HF $Wa.c.1.g rm_suffix $Wa.d.tt
frame $Wa.c.1.fr; pack configure $Wa.c.1.fr -anchor n

if {[string first "$quickfile.*" "$aufsuff"]<0} \
	{set aufsuff "$quickfile.* $aufsuff"; set auftoggle "1 $auftoggle"}
set nmax [llength $aufsuff]
for {set n 0} {$n<$nmax} {incr n} {
  checkbutton $Wa.c.1.fr.cb$n -relief raised -borderwidth 1 -variable ak_v($n) \
      -text "  [lindex $aufsuff $n] " -width 16 -anchor w -command "akChkBut $Wa.c.1.fr.cb$n"
  pack configure  $Wa.c.1.fr.cb$n
  Bind3HF $Wa.c.1.fr.cb$n rm_suffix $Wa.d.tt
  set delsuff($Wa.c.1.fr.cb$n) [lindex $aufsuff $n]
  set ak_h($Wa.c.1.fr.cb$n) [lindex $auftoggle $n]
  if {$ak_h($Wa.c.1.fr.cb$n)==1} {$Wa.c.1.fr.cb$n select} else {$Wa.c.1.fr.cb$n deselect}
}

# Bereich c2 fuer Datei-Anzeige
button $Wa.c.2.ad -text "$vv(akc2ad)" -command "akDelFiles $Wa.d.tt"
Bind3HTF $Wa.c.2.ad "$vv(btclk1):\n" rm_files $Wa.d.tt
frame $Wa.c.2.fr -borderwidth 2
pack configure $Wa.c.2.ad $Wa.c.2.fr -fill x -expand yes
CreateLSBox $Wa.c.2.fr "" "" 35 $aufmax 2 rm_files $Wa.d.tt "" "" "akExclFile" selnoysb ""

# Erstellen Dateiliste für Löschen (Dateien mit allen gesetzten Suffixen der Liste und core)
set dmaxmax 0
foreach i [DelWinNTFiles [lsort [glob -nocomplain -- *]]] {
  if [file isfile $i] {
    if {($i=="core")} {set deldat($dmaxmax) $i; set dd($dmaxmax) 1; incr dmaxmax}
    for {set n 0} {$n<$nmax} {incr n} {
      if [string match "$delsuff($Wa.c.1.fr.cb$n)" $i] {
        set deldat($dmaxmax) $i
        if {$ak_h($Wa.c.1.fr.cb$n)==1} {set dd($dmaxmax) 1} else {set dd($dmaxmax) 0}
        incr dmaxmax
      }
    }
  }
}
# Ausgabe der zum Loeschen vorgesehenen Dateien
$Wa.c.2.fr.f.f.li delete 0 end
set daus 0; set df1 -1; set df2 -1
for {set dm 0} {$dm<$dmaxmax} {incr dm} {
  if {$dd($dm)==1} {
    if {$df1==-1} {set df1 $dm}
    if {$daus<$aufmax} {$Wa.c.2.fr.f.f.li insert end $deldat($dm); incr daus; set df2 $dm}
  }
}


InsertTextFrame $Wa $tyh "" "" ""
}
