#!/bin/csh -f 

if ($#argv != 1) then 
   echo "Usage: frepair <filename>"
   exit -1
endif 

set fname = $argv[1]
set RMINCFILE = /usr/coda/bin/removeinc
set CFS = /usr/coda/bin/cfs
if ($?DISPLAY) then
   set xisrunning = 1
   set ASKUSERSCRIPT = "/usr/coda/bin/xaskuser"
else 
   set xisrunning = 0
   set ASKUSERSCRIPT = "/usr/coda/bin/noxaskuser"
#noxaskuser hasn't been implemented.....
   echo "Non-x version of this isn't available yet"
   exit -1
endif

set idir = $fname:h
set ifile = $fname:t
if ($ifile == $idir) set idir = $cwd

# create a new file to gather statistics
set uname = `/usr/ucb/whoami`

$CFS beginrepair $idir/$ifile
set askout = `$ASKUSERSCRIPT $idir $ifile`
set rc = $status
$CFS endrepair $idir/$ifile
switch ($rc)
   case 0:
      #do nothing
      exit 0
      breaksw;
   case 2:
      # remove the inconsistent object
      $RMINCFILE $idir/$ifile
      set retcode = $status
      exit $retcode
      breaksw
   case 3:
      #explode the inconsistent object 
      # make dir and copy replicas into directory
      mkdir $idir/$ifile.incdir
      $CFS beginrepair $idir/$ifile
      foreach f ($idir/$ifile/*)
         /bin/cp -p $f $idir/$ifile.incdir
      end
      $CFS endrepair $idir/$ifile
      #remove the inconsistent object and rename the directory to the original name
      $RMINCFILE $idir/$ifile
      /bin/mv $idir/$ifile.incdir $idir/$ifile
      set retcode = $status
      exit $retcode
      breaksw
   case 4:
      # use one of the replicas for repair 
      # this has already been repaired
      exit $rc
      breaksw
   case 5:
      # use a brand new file (possibly merged) for repair 
      exit $rc
      breaksw
   default:
      echo "Couldn't figure out what the user wanted to do "
      exit -1
      breaksw

# we NEED to collect statistics for the file repair
endsw

