#!/usr/misc/.tcl/bin/wish -f

proc mkbuttons {frame} {
   radiobutton $frame.remove -text "Remove Inconsistent file" -variable action \
	-value remove 
   radiobutton $frame.explode -text "Explode Inc. file into directory" \
	-variable action -value explode 
   radiobutton $frame.latest -text "Use one of the replicas as the new version" \
	-variable action -value latest 
   radiobutton $frame.nothing -text "Take no action" -variable action \
	-value nothing

   #frame for the button and entry for specifying a new file
   frame $frame.newfile
   frame $frame.newfile.fname
   radiobutton $frame.newfile.but -text "Use another file" \
	-variable action -value name 
   entry $frame.newfile.fname.ent -width 30 -relief sunken;
   bind $frame.newfile.fname.ent <Return> "set action name; repairwithnamedfile $frame.newfile.fname.ent"
   scrollbar $frame.newfile.fname.sb -relief flat -orient horiz \
	-command "$frame.newfile.fname.ent view";
   pack append $frame.newfile.fname $frame.newfile.fname.sb {bottom fill}\
	$frame.newfile.fname.ent top

   pack append $frame.newfile $frame.newfile.fname {top right} $frame.newfile.but {top left padx 10}

   frame $frame.replicas
   getreplicas $frame.replicas
   pack append $frame \
	$frame.remove {top pady 10 }\
	$frame.explode {top pady 10 }\
	$frame.newfile {top pady 10}\
	$frame.latest {top pady 10 }\
	$frame.replicas {top pady 5}\
	$frame.nothing {top pady 10 } 
   
}

proc GetIncResp {{w .f}} {
   global action
   global oname

   set sl [string length $oname]
   if {$sl > 0} {set width $sl} {set width 15}

   #frame for announcing what has happened
   message $w.announce -font -Adobe-times-medium-r-normal--*-180* \
	-relief raised -width [expr $width]c \
	-text "$oname \nis inconsistent... \n\nWhat do you want to do?"

   # frame for making the buttons
   frame $w.actionbuttons 
   mkbuttons $w.actionbuttons

   #frame for placing the ok/cancel buttons
   frame $w.finalize 
   button $w.finalize.ok -text "OK" -command {\
	case $action in {
		nothing {exit 0} 
		remove {exit 2} 
		explode {exit 3}
		latest {
			oklistboxcommand .f.actionbuttons.replicas.entries.l }
		name {repairwithnamedfile .f.actionbuttons.newfile.fname.ent }
		default {exit -1}
	}
   }
   button $w.finalize.cancel -text "Cancel" -command {\
	destroy .
	exit 0
   }
   pack append $w.finalize $w.finalize.ok {left} $w.finalize.cancel {right}

   pack append $w $w.announce {top fillx } \
      $w.actionbuttons {top fillx } \
      $w.finalize {bottom fillx}

   bind $w <KeyPress-Return> {puts stdout "returning $action"; return $action}

#   after 60000 "destroy .; exit 0"
}

proc getreplicas {frame} {
   #make a frame 
   $frame configure  -width 80c -height 3

   #put a frame for displaying the replicas
   frame $frame.entries

   canvas $frame.message -height 1c -width 20c
   pack append $frame $frame.message {bottom fillx}\
	$frame.entries {top fillx}
   makeentries $frame.entries
   getentries $frame.entries.l
}

#make the listbox (and the scrollbar) for displaying the replicas 
proc makeentries {entry} {
   listbox $entry.l -relief raised -bd 2 -geometry 80x5 \
	-yscrollcommand "$entry.sb set" \
	-font -Adobe-Courier-medium-r-normal--*-120*
   scrollbar $entry.sb -relief flat -command "$entry.l yview"
   pack append $entry $entry.sb {right filly} $entry.l {expand fill}
}

# get the meta information for each replica and insert into listbox
proc getentries {listbox} {
   global children nreplicas oname
   set cwd [pwd]
   cd $oname
   for {set i 0} {$i < $nreplicas} {incr i +1} {
      set newentry [exec ls -l [lindex $children $i]]
      $listbox insert end $newentry
   }
   $listbox select from 0
   cd $cwd 
}

# procedure invoked when ok is pressed
proc oklistboxcommand {listbox} {
   global children oname 
   set cs [$listbox curselection]
   set howmanyentries [llength $cs] 
   if {$howmanyentries == 0} {
      set echotext "You need to select an entry first" 
      set i [.f.actionbuttons.replicas.message create text 6c 0.3c -text "$echotext" -fill red -anchor nw]
      after 1000 ".f.actionbuttons.replicas.message delete $i"
   } else { 
      if {$howmanyentries > 1} {
         set echotext "You must select only one entry" 
         set i [.f.actionbuttons.replicas.message create text 6c 0.3c -text "$echotext" -fill red -anchor nw]
         after 1000 ".f.actionbuttons.replicas.message delete $i"
      } else {
         set repname [lindex $children [lindex $cs 0]]
         destroy .
	 global errorCode
         catch {exec /usr/coda/bin/filerepair $oname $oname/$repname}
	 puts stdout "repaired by $oname/$repname"
         exit 4
      }
   }
}
#proc invoked when ok is pressed with the button "use another file" is on
proc repairwithnamedfile {entry} {
   global oname 
   set cs [$entry get]
   set fnamelength [llength $cs]
   if {$fnamelength <= 0} {
      set echotext "You need to enter the name of a file first" 
      set i [.f.actionbuttons.replicas.message create text 6c 0.3c -text "$echotext" -fill red -anchor nw]
      after 1000 ".f.actionbuttons.replicas.message delete $i"
   } else {
      if {[file exists $cs] == 0} {
         set echotext  "$cs does not exist"
         set i [.f.actionbuttons.replicas.message create text 6c 0.3c -text "$echotext" -fill red -anchor nw]
         after 1000 ".f.actionbuttons.replicas.message delete $i"
      } elseif {[file isfile $cs] == 0} {
         set echotext "$cs isn't a file"
         set i [.f.actionbuttons.replicas.message create text 6c 0.3c -text "$echotext" -fill red -anchor nw]
         after 1000 ".f.actionbuttons.replicas.message delete $i"         
      } else {
	 catch {exec /usr/coda/bin/filerepair $oname $cs}
         puts stdout "repaired by $cs"
         exit 5
      }
   }
}

set action nothing 
frame .f -bd 2 -relief flat -background black 

set dname [lindex $argv 0]
set fname [lindex $argv 1]
set oname "$dname/$fname"
set children [exec ls $oname]
set nreplicas [llength $children]

GetIncResp

pack append . .f {top}
