#!/bin/sh -e
#/* BLURB gpl
#
#                           Coda File System
#                              Release 6
#
#          Copyright (c) 1987-2003 Carnegie Mellon University
#                  Additional copyrights listed below
#
#This  code  is  distributed "AS IS" without warranty of any kind under
#the terms of the GNU General Public Licence Version 2, as shown in the
#file  LICENSE.  The  technical and financial  contributors to Coda are
#listed in the file CREDITS.
#
#                        Additional copyrights
#                           none currently
#
#*/

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
