#!/bin/csh -f

#ifndef _BLURB_
#define _BLURB_
#/*
#
#            Coda: an Experimental Distributed File System
#                             Release 3.1
#
#          Copyright (c) 1987-1995 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: createvol_rep,v 3.2.1.1 95/10/11 10:18:09 raiff Exp $";
#endif /*_BLURB_*/


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

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

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

#
# 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 ($#argv == 3) then
	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
else
	set HEXGROUPID=$4
endif


echo HexGroupId is $HEXGROUPID

# Create the new volumes
set N=0
foreach SERVER ($SERVERS)
	echo "creating volume" $VOLNAME.$N on $SERVER
	/vice/bin/volutil -h $SERVER create_rep $PARTITION $VOLNAME.$N $HEXGROUPID
	@ N++
end

# Rebuild the VLDB
/vice/bin/bldvldb.sh

# Update the VRList and rebuild the VRDB
set ENTRY=($VOLNAME $HEXGROUPID $NSERVERS 0 0 0 0 0 0 0 0 $VSGADDR)
set N=0
foreach SERVER ($SERVERS)
	@ IX = $N + 4
	set ENTRY[$IX]=`awk ' $1 ~/'$VOLNAME.$N'$/  {print $2}' /vice/vol/AllVolumes`
	@ N++
end
echo "<echo $ENTRY >> /vice/vol/VRList>"
echo $ENTRY >> /vice/vol/VRList
/vice/bin/volutil makevrdb /vice/vol/VRList

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

set _ans_ = "$<"

if ("$_ans_" =~ [Yy] || "$_ans_" =~ [Yy][Ee][Ss]) then
    echo -n "Day to take full dumps: [Mon] "

    set day = "$<"

    if ("$day" == "") set day = "Mon"	

    switch ("$day")
	case [Ss][Uu][Nn]*:
	    set _cycle_ = "FIIIIII"
	    breaksw
	case [Mm][Oo][Nn]*:
	    set _cycle_ = "IFIIIII"
	    breaksw
	case [Tt][Uu][Ee]*:
	    set _cycle_ = "IIFIIII"
	    breaksw
	case [Ww][Ee][Dd]*:
	    set _cycle_ = "IIIFIII"
	    breaksw
	case [Tt][Hh][Uu]*:
	    set _cycle_ = "IIIIFII"
	    breaksw
	case [Ff][Rr][Ii]*:
	    set _cycle_ = "IIIIIFI"
	    breaksw
	case [Ss][Aa][Tt]*:
	    set _cycle_ = "IIIIIIF"
	    breaksw
	default:
            echo "$day" is not a valid day, not adding $VOLNAME to backup list.
	    breaksw
    endsw

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


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

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

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

#
# 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 ($#argv == 3) then
	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
else
	set HEXGROUPID=$4
endif


echo HexGroupId is $HEXGROUPID

# Create the new volumes
set N=0
foreach SERVER ($SERVERS)
	echo "creating volume" $VOLNAME.$N on $SERVER
	/vice/bin/volutil -h $SERVER create_rep $PARTITION $VOLNAME.$N $HEXGROUPID
	@ N++
end

# Rebuild the VLDB
/vice/bin/bldvldb.sh

# Update the VRList and rebuild the VRDB
set ENTRY=($VOLNAME $HEXGROUPID $NSERVERS 0 0 0 0 0 0 0 0 $VSGADDR)
set N=0
foreach SERVER ($SERVERS)
	@ IX = $N + 4
	set ENTRY[$IX]=`awk ' $1 ~/'$VOLNAME.$N'$/  {print $2}' /vice/vol/AllVolumes`
	@ N++
end
echo "<echo $ENTRY >> /vice/vol/VRList>"
echo $ENTRY >> /vice/vol/VRList
/vice/bin/volutil makevrdb /vice/vol/VRList

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

set _ans_ = "$<"

if ("$_ans_" =~ [Yy] || "$_ans_" =~ [Yy][Ee][Ss]) then
    echo -n "Day to take full dumps: [Mon] "

    set day = "$<"

    if ("$day" == "") set day = "Mon"	

    switch ("$day")
	case [Ss][Uu][Nn]*:
	    set _cycle_ = "FIIIIII"
	    breaksw
	case [Mm][Oo][Nn]*:
	    set _cycle_ = "IFIIIII"
	    breaksw
	case [Tt][Uu][Ee]*:
	    set _cycle_ = "IIFIIII"
	    breaksw
	case [Ww][Ee][Dd]*:
	    set _cycle_ = "IIIFIII"
	    breaksw
	case [Tt][Hh][Uu]*:
	    set _cycle_ = "IIIIFII"
	    breaksw
	case [Ff][Rr][Ii]*:
	    set _cycle_ = "IIIIIFI"
	    breaksw
	case [Ss][Aa][Tt]*:
	    set _cycle_ = "IIIIIIF"
	    breaksw
	default:
            echo "$day" is not a valid day, not adding $VOLNAME to backup list.
	    breaksw
    endsw

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