#! /bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

# interface generated by SpecTcl version 1.1 from /puccini2/users2/ramsan/instalations/SpecTcl1.1b2/demo/eraseoldfiles.ui
#   root     is the parent window for this user interface

proc DeleteOldFilesWin { filelist } {

    set root .gid.__deloldfiles

    toplevel $root
    wm title $root [_ "Erase old files"]


	# this treats "." as a special case

	if {$root == "."} {
	    set base ""
	} else {
	    set base $root
	}
    
	frame $base.frame#1

	frame $base.frame#2 \
		-borderwidth 2 \
		-relief sunken

	label $base.label#1 \
		-padx 5 \
		-relief ridge \
         	-text [_ "Current files in temporal directory seem to be old. Erase them?"] \
		-wraplength 200

	listbox $base.listbox#2 \
		-height 4 \
	        -width 0 \
	        -xscroll "$base.scrollbar#2 set" -yscroll "$base.scrollbar#1 set"

	scrollbar $base.scrollbar#1 \
		-orient v -comm "$base.listbox#2 yview"

	scrollbar $base.scrollbar#2 \
		-orient h -comm "$base.listbox#2 xview"

	label $base.label#2 \
	        -text [_ "Total size: 200Kb"]

	button $base.button#1 \
	        -text [_ "OK"] \
	        -command "DeleteOldFilesErase $filelist ; destroy $root"

	button $base.button#3 \
	        -text [_ "Cancel"] \
	        -command "destroy $root"


	# Geometry management

	grid $base.frame#1 -in $root	-row 5 -column 1  \
		-columnspan 2 -pady 5
	grid $base.frame#2 -in $base.frame#1	-row 1 -column 2 -padx 3
	grid $base.label#1 -in $root	-row 1 -column 1  \
		-columnspan 2 \
		-padx 5 \
		-pady 5
	grid $base.listbox#2 -in $root	-row 2 -column 1  \
		-sticky nesw
	grid $base.scrollbar#1 -in $root	-row 2 -column 2  \
		-sticky ns
	grid $base.scrollbar#2 -in $root	-row 3 -column 1  \
		-sticky ew
	grid $base.label#2 -in $root	-row 4 -column 1 
	grid $base.button#1 -in $base.frame#1	-row 1 -column 1 -padx 3
	grid $base.button#3 -in $base.frame#2	-row 1 -column 1 -padx 3 -pady 3

	# Resize behavior management

	grid rowconfigure $base.frame#2 1 -weight 0 -minsize 30
	grid columnconfigure $base.frame#2 1 -weight 0 -minsize 30

	grid rowconfigure $root 1 -weight 0 -minsize 16
	grid rowconfigure $root 2 -weight 1 -minsize 30
	grid rowconfigure $root 3 -weight 0 -minsize 2
	grid rowconfigure $root 4 -weight 0 -minsize 30
	grid rowconfigure $root 5 -weight 0 -minsize 30
	grid columnconfigure $root 1 -weight 1 -minsize 2
	grid columnconfigure $root 2 -weight 0 -minsize 2

	grid rowconfigure $base.frame#1 1 -weight 0 -minsize 30
	grid columnconfigure $base.frame#1 1 -weight 0 -minsize 30
	grid columnconfigure $base.frame#1 2 -weight 0 -minsize 30




    wm withdraw $root
    update idletasks

    set pare [winfo parent $root]
    set x [expr [winfo x $pare]+[winfo width $pare ]/2- [winfo reqwidth $root]/2]
    set y [expr [winfo y $pare]+[winfo height $pare ]/2- [winfo reqheight $root]]
    WmGidGeom $root +$x+$y
    wm deiconify $root

    set size 0
    foreach i $filelist {
	$base.listbox#2 ins end $i
	foreach j [glob -nocomplain [file join $i *]] {
	    set size [expr $size+[file size $j]]
	}
    }
    set size [expr $size/1024+1]
    $base.label#2 conf -text [_ "Total size: %dKb" ${size}]

    focus $base.button#3

}

proc DeleteOldFilesErase { args } {
    foreach i $args {
	if { [catch { file delete -force $i } msg] } {
	    WarnWinText [_ "Error deleting file: %s" $msg]
	}
    }
}

#argument is one of the files inside tmp directory
proc DeleteOldFiles { tmpdir } {

    set tmpdir [file dirname $tmpdir]

    set NumOfDays 2
    
    set oldtime [expr [clock seconds]-$NumOfDays*86400.0]

    set filelist ""
    foreach i [glob -nocomplain [file join $tmpdir gid*]] {
	if { [file isdirectory $i] && \
	     [file owned $i] && \
	     [file exists [file join $i tmp-gidOutput]] && \
	     [file mtime $i]<$oldtime } {
	    lappend filelist $i
	}
    }

    if { $filelist == "" } { return }

    if { 0 } {
	DeleteOldFilesWin $filelist
    } else {
	#delete old temporary gid directories without ask to user
	DeleteOldFilesErase $filelist
    }
}


# Allow interface to be run "stand-alone" for testing

catch {
    if [info exists embed_args] {
	# we are running in the plugin
	eraseoldfiles_ui .
    } else {
	# we are running in stand-alone mode
	if {$argv0 == [info script]} {
	    wm title . [_ "Testing eraseoldfiles_ui"]
	    eraseoldfiles_ui .
	}
    }
}
