#!/bin/sh 
# BLURB gpl
# 
#                            Coda File System
#                               Release 5
# 
#           Copyright (c) 1987-1999 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
# 
#*/


# Check that the imput parameters are correct
if [ $# != 1 ]
then
	echo "purgevol_rep volumename"
	exit 1
fi

if [ `cat /vice/hostname` != `cat /vice/db/scm` ]
then
        echo "This must be run from the scm ("`cat /vice/db/scm`")"
        exit 1
fi

# Look up the replicated volume name in /vice/vol/VRList to determine the
# VolIDs of the replicated copies.
REPVOLNAME=$1
REPS=`awk '$1 ~ /^'$REPVOLNAME'$/ {for (i=4; i< $3 + 4; i++) printf "%s ", $i}' /vice/vol/VRList`
REPVOLID=`awk '$1 ~ /^'$REPVOLNAME'$/ {print $2}' /vice/vol/VRList`
VSG=`awk '$1 ~ /^'$REPVOLNAME'$/ {print $12}' /vice/vol/VRList`
HOSTS=`grep -i "^$VSG" /vice/db/VSGDB | sed "s/$VSG//"`

# Check to see if the volume actually exists 

if [ -z "$REPS" ]
then
	echo "Volume" $REPVOLNAME "doesn't exist!"
	exit 1
fi

# Remove the individual copies


for i in $REPS 
do 
	echo "Deleting volume " $i ;
	awk '$2 ~ /^'$i'$/ { print | "volutil -h "$3" purge " $2" " $1}' /vice/vol/AllVolumes
done

#
# Delete the entry for the volume from the VRList

awk ' $1 !~ /^'$REPVOLNAME'$/ { print } ' /vice/vol/VRList >/vice/vol/VRList.tmp
mv /vice/vol/VRList.tmp /vice/vol/VRList

#
# Delete the entry from the backup list.

awk ' $1 !~ /^'$REPVOLID'$/ { print } ' /vice/db/dumplist >/vice/db/dumplist.tmp
mv /vice/db/dumplist.tmp /vice/db/dumplist

# Make sure that the vldb and vrdb are updated.

volutil makevrdb /vice/vol/VRList
echo "fetching updated VolumeList from $HOSTS"
bldvldb.sh $HOSTS



