#! /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
# 
#*/

PATH=$PATH:/usr/sbin
export PATH

prefix=/usr
vicedir=
if [ -f /etc/coda/server.conf ] ; then
  . /etc/coda/server.conf
fi

if [ x${vicedir} = x ]; then
  vicedir=/vice
fi

if [ ! -f ${vicedir}/hostname -o ! -f ${vicedir}/db/scm ] ; then
   echo "Coda server configuration error.  Could not find needed files."
   exit 1
fi


# 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 ${vicedir}/hostname` != `cat ${vicedir}/db/scm` ]; then
	echo "This must be run from the scm (`cat ${vicedir}/db/scm`)"
	exit 1
fi

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

echon() {
    if [ "`echo -n`" ] ; then
        echo "$@"\c
    else
        echo -n "$@"
    fi
}


#
# Create these files if they don't already exist.
touch ${vicedir}/vol/AllVolumes
touch ${vicedir}/vol/VRList

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

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

if [ x$VOLNAME = x`awk ' $1 ~ /^'$1'$/  {print $1}' ${vicedir}/vol/VRList` ]; then
	echo "Volume $VOLNAME already exists in ${vicedir}/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}' ${vicedir}/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 ${vicedir}/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  [ -f ${vicedir}/vol/maxgroupid ]; then
		GROUPID=`cat ${vicedir}/vol/maxgroupid`

		case x`uname` in
		    xCYGWIN32_NT)
			HEXGROUPID=`printf %x $GROUPID`
			;;
		    xCYGWIN_NT-4.0)
			HEXGROUPID=`printf %x $GROUPID`
			;;
		    x*)	
			HEXGROUPID=`echo obase=16\;$GROUPID | bc`
			;;
		esac	
	
		MAXGROUPID=`expr $GROUPID + 1`
		echo $MAXGROUPID > ${vicedir}/vol/maxgroupid
	else
		echo ${vicedir}/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}' ${vicedir}/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 >> ${vicedir}/vol/VRList>"
echo $ENTRY >> ${vicedir}/vol/VRList
volutil makevrdb ${vicedir}/vol/VRList

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

read _ans_

if  [ x$_ans_ = xy -o x$_ans_ = xyes ]; then
    echon "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 >>${vicedir}/db/dumplist"
    echo "$HEXGROUPID        $_cycle_         $VOLNAME" >>${vicedir}/db/dumplist
fi
