#! /bin/sh
#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.
#*/
#
#static char *rcsid = "$Header: /afs/cs/project/coda-src/cvs/coda/coda-src/scripts/createvol_rep,v 4.7.2.1 98/10/05 16:54:15 jaharkes Exp $";
#endif /*_BLURB_*/

PATH=$PATH:/usr/local/sbin
export PATH

[ -f /vice/hostname ] ||  exit 1
[ -f /vice/db/scm ] ||  exit 1



# Parge arguments
if  [ $# != 3 -a $# != 4 ]; then
	echo "bad args:  createvol_rep <volname> <vsgaddr> <partition-name> [<groupid>]"
	exit
fi

# 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 1
fi

# Initialize local variables
VOLNAME=$1
SERVERS=
NSERVERS=0
MAXSERVERS=8
PARTITION=$3
VSGADDR=$2

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

# Validate the <volname> parameter
if  [ x$VOLNAME = x`awk ' $1 ~ /^'$1'$/  {print $1}' /vice/vol/AllVolumes` ]; then
    echo "$VOLNAME exists as a non replicated volume in /vice/vol/AllVolumes"
    exit 1
fi

if [ x$VOLNAME = x`awk ' $1 ~ /^'$1'\.[0-7]$/  {print $1}' /vice/vol/AllVolumes` ]; then
    echo "$VOLNAME exists as a replica in /vice/vol/AllVolumes"
    exit 1
fi

if [ x$VOLNAME = x`awk ' $1 ~ /^'$1'$/  {print $1}' /vice/vol/VRList` ]; then
	echo "Volume $VOLNAME already exists in /vice/vol/VRList"
	exit 1
fi

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



# Validate the server list
if [ $NSERVERS -eq 0 -o  $NSERVERS -gt $MAXSERVERS ]; then
	echo Specify 1 - $MAXSERVERS servers
	exit 1
fi 


for SERVER in $SERVERS ; do
	grep $SERVER /vice/db/servers > /dev/null 
	if [ $? != 0 ];  then
		echo Server $SERVER not in servers file
		exit 1
	fi
done

# Allocate a new groupid if necessary
if  [ $# = 3 ]; then
	if  [ -e /vice/vol/maxgroupid ]; then
		GROUPID=`cat /vice/vol/maxgroupid`

		cat << EOF >/tmp/$$
ibase = 10
obase = 16
$GROUPID
EOF

		HEXGROUPID=`bc </tmp/$$`
		rm /tmp/$$
		MAXGROUPID=`expr $GROUPID + 1`
		echo $MAXGROUPID > /vice/vol/maxgroupid
	else
		echo /vice/vol/maxgroupid not found
		exit 1
	fi
else
	HEXGROUPID=$4
fi


echo HexGroupId is $HEXGROUPID


# Create the new volumes
N=0
for  SERVER in $SERVERS ; do
	echo "creating volume" $VOLNAME.$N on $SERVER
	volutil -h $SERVER create_rep $PARTITION $VOLNAME.$N $HEXGROUPID
	if [ $? != 0 ]; then 
		echo "Failure to make volume on $SERVER!. Abort.".
		exit 1
	fi
	N=`expr $N + 1`
done

# Rebuild the VLDB
bldvldb.sh $SERVERS

# Update the VRList and rebuild the VRDB
ENTRY="$VOLNAME $HEXGROUPID $NSERVERS"
N=0
for SERVER  in $SERVERS ; do

	VOLN=`awk ' $1 ~/'$VOLNAME.$N'$/  {print $2}' /vice/vol/AllVolumes`
	ENTRY="$ENTRY $VOLN"
	N=`expr $N + 1`
done 

# add nulentries and VSGDB to entry
while true  ; do
        if [ $N -eq 8 ]; then
	    break
	fi
        ENTRY="$ENTRY 0"
	N=`expr $N + 1`
done

ENTRY="$ENTRY $VSGADDR"

echo "<echo $ENTRY >> /vice/vol/VRList>"
echo $ENTRY >> /vice/vol/VRList
volutil makevrdb /vice/vol/VRList

echo -n "Do you wish this volume to be Backed Up (y/n)? [n] "

read _ans_

if  [ x$_ans_ = xy -o x$_ans_ = xyes ]; then
    echo -n "Day to take full dumps: [Mon] "

    read day

    if [ x"$day" = x"" ]; then 
	day="Mon"	
    fi

    case $day in 
       [Ss][Uu][Nn]*)
	    _cycle_="FIIIIII" ;;
	[Mm][Oo][Nn]*)
	    _cycle_="IFIIIII" ;;
	[Tt][Uu][Ee]*)
	    _cycle_="IIFIIII" ;;
	[Ww][Ee][Dd]*)
	    _cycle_="IIIFIII" ;;
	[Tt][Hh][Uu]*)
	    _cycle_="IIIIFII" ;;
	[Ff][Rr][Ii]*)
	    _cycle_="IIIIIFI" ;;
	[Ss][Aa][Tt]*)
	    _cycle_="IIIIIIF" ;;
	*) 
            echo "$day" is not a valid day, not adding $VOLNAME to backup list.
	    exit 
	    ;;
    esac

    echo "echoing $HEXGROUPID        $_cycle_        $VOLNAME >>/vice/db/dumplist"
    echo "$HEXGROUPID        $_cycle_         $VOLNAME" >>/vice/db/dumplist
fi


