#!/bin/sh -e
#ifndef _BLURB_
#define _BLURB_
#/*
#
#            Coda: an Experimental Distributed File System
#                             Release 4.0
#
#          Copyright (c) 1987-1996 Carnegie Mellon University
#                         All Rights Reserved
#
#Permission  to  use, copy, modify and distribute this software and its
#documentation is hereby granted,  provided  that  both  the  copyright
#notice  and  this  permission  notice  appear  in  all  copies  of the
#software, derivative works or  modified  versions,  and  any  portions
#thereof, and that both notices appear in supporting documentation, and
#that credit is given to Carnegie Mellon University  in  all  documents
#and publicity pertaining to direct or indirect use of this code or its
#derivatives.
#
#CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
#SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
#FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
#DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
#RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
#ANY DERIVATIVE WORK.
#
#Carnegie  Mellon  encourages  users  of  this  software  to return any
#improvements or extensions that  they  make,  and  to  grant  Carnegie
#Mellon the rights to redistribute these changes without encumbrance.
#*/
#
#/*
#Rewritten for the bourne shell by Anders Hammarquist <iko@iko.pp.se>
#*/
#
#static char *rcsid = "$Header: /afs/cs/project/coda-src/cvs/coda/coda-src/egasr/xfrepair,v 4.2.8.1 98/10/08 11:26:28 jaharkes Exp $";
#endif /*_BLURB_*/

CTOKENS=ctokens
RMINCFILE=removeinc
CFS=cfs

if [ $# != 1 ]; then
    echo "Usage: $0 <filename>"
    exit -1
fi

# Check coda authentication
if ! ctokens > /dev/null; then
    echo "You are not authenticated to the Coda servers"
    exit 1
fi

fname=$1

if [ "x$DISPLAY" != x ]; then
    ASKUSER=xaskuser
else
    echo "Non-X version of this repair tool isn't available yet"
    exit -1
fi

idir=`dirname $fname`
ifile=`basename $fname`

trap "$CFS endrepair $idir/$file" 1 2 15

$CFS beginrepair $idir/$ifile

# Check for local/global conflicts
if [ -e $fname/local -a -e $fname/global ]; then
    echo "$fname has a local/global conflict,"
    echo "please use the regular repair tool"
    $CFS endrepair $idir/$ifile
    exit -1
fi

set +e
$ASKUSER $idir $ifile > /dev/null
rc=$?
set -e
$CFS endrepair $idir/$ifile

case $rc in
    0)
        # do nothing
	exit 0
	;;

    1)
	# remove the inconsistent object
	$RMINCFILE $idir/$ifile
	;;

    2)
	# use one of the replicas for repair, which has already been 
	# done by the xaskuer script
	;;

    3)
	# use a new file for repair , which has already been
	# done by the xaskuer script
	;;

    *)
	echo "Couldn't figure out what the user wanted to do"
	exit -1
	;;
esac

exit 0
