#!/bin/csh -f

#ifndef _BLURB_
#define _BLURB_
#/*
#                           CODA FILE SYSTEM
#                      School of Computer Science
#                      Carnegie Mellon University
#                          Copyright 1987-89
#
#Present and past contributors to Coda include M. Satyanarayanan, James
#Kistler, Puneet Kumar, Maria Okasaki, Ellen Siegel, Walter  Smith  and
#David  Steere.  Parts of Coda are derived from the August 1986 version
#of the Andrew file system, which is owned  by  IBM,  and  may  not  be
#distributed without explicit permission.
#*/
#
#
#static char *rcsid = "$Header: /afs/cs/project/coda-src/cvs/coda/coda-src/scripts/recreatevol_rep,v 4.4 1998/01/20 20:53:14 braam Exp $";
#endif _BLURB_

# Parge arguments
if ($#argv != 3 && $#argv != 4) then
	echo "bad args:  createvol_rep <volname> <vsgaddr> <partition-name> [<cycle>]"
	exit
endif

# Ensure that this is the SCM
if (`cat /vice/hostname` != `cat /vice/db/scm`) then
	echo This must be run from the scm (`cat /vice/db/scm`)
	exit
endif

# Initialize local variables
set VOLNAME=$1
set SERVERS=()
set NSERVERS=0
set MAXSERVERS=8
set PARTITION=$3
set VSGADDR=$2
if ( $#argv == 4 ) then
    set CYCLE=$4
else
    set CYCLE=""
endif

#
# Create these files if they don't already exist.
touch /vice/vol/AllVolumes
touch /vice/vol/VRList

# Validate the <volname> parameter
if ($VOLNAME == `awk ' $1 ~ /^'$1'$/  {print $1}' /vice/vol/AllVolumes` ||\
    $VOLNAME == `awk ' $1 ~ /^'$1'$/  {print $1}' /vice/vol/VRList`) then
	echo Volume $VOLNAME already exists
	exit
endif

# Derive the server list from the <vsgaddr> parameter
set SERVERS=`awk '$1 ~ /^'$VSGADDR'$/ {print $2, $3, $4, $5, $6, $7, $8, $9}' /vice/db/VSGDB`
echo Servers are \($SERVERS\)
set NSERVERS=$#SERVERS

# Validate the server list
if ($NSERVERS == 0 || $NSERVERS > $MAXSERVERS) then
	echo Specify 1 - $MAXSERVERS servers
	exit
endif
set N=0
foreach SERVER ($SERVERS)
	if ($SERVER != `awk ' $2 ~/^'$SERVER'$/  {print $2}' /vice/db/hosts`) then
		echo Server $SERVER not in host file
		exit
	endif
	@ N++
end

# Allocate a new groupid if necessary
if (-e /vice/vol/maxgroupid) then
	set GROUPID=`cat /vice/vol/maxgroupid`

	cat << EOF >/tmp/$$
ibase = 10
obase = 16
$GROUPID
EOF
	set HEXGROUPID = `bc </tmp/$$`
	rm /tmp/$$
	@ MAXGROUPID = $GROUPID + 1
	echo $MAXGROUPID > /vice/vol/maxgroupid
else
	echo /vice/vol/maxgroupid not found
	exit
endif

echo HexGroupId is $HEXGROUPID

# Create the new volumes
set N=0
set ENTRY=($VOLNAME $HEXGROUPID $NSERVERS 0 0 0 0 0 0 0 0 $VSGADDR)
foreach SERVER ($SERVERS)
	echo "creating volume" $VOLNAME.$N on $SERVER
	set vol_msg=`volutil -h $SERVER create_rep $PARTITION $VOLNAME.$N $HEXGROUPID`
	echo $vol_msg
	@ IX = $N + 4
	set ENTRY[$IX]=`echo $vol_msg |awk '{print $7}'`
	@ N++
end

# Update the VRList
echo "<echo $ENTRY >> /vice/vol/VRList>"
echo $ENTRY >> /vice/vol/VRList

if ( "$CYCLE" != "" ) then
    echo "echoing $HEXGROUPID        $CYCLE        $VOLNAME >>/vice/db/dumplist"
    echo "$HEXGROUPID        $CYCLE         $VOLNAME" >>/vice/db/dumplist
endif
