#!/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: makebeta,v 3.2.1.1 95/10/11 10:18:30 raiff Exp $";
#endif /*_BLURB_*/


### Set defaults paths
set ALPHAROOT = /coda/project/coda
set BETAROOT  = /coda/project/coda

# ******* Process input args ********

while ($1 != "")

set KEYWORD = `expr $1 : '\(.*\)=.*'`
set VALUE = `expr $1 : '.*=\(.*\)'`

switch ($KEYWORD)

case ALPHAROOT:
	set ALPHAROOT = $VALUE
	breaksw

case BETAROOT:
	set BETAROOT = $VALUE
	breaksw

default:
	echo "Usage: makebeta [ALPHAROOT=<pathname for root of alpha>] [BETAROOT=<pathname for root of beta>]"
	exit
	breaksw

endsw

shift
end

# Let user know what values of key variables are
echo "ALPHAROOT is $ALPHAROOT"
echo "BETAROOT is $BETAROOT"

### Set defaults paths
set ALPHA = $ALPHAROOT/alpha
set RCSDIR = $ALPHA/src/RCSLINK

### Construct new beta directory

# First construct a unique name for this release
set TIMESTAMP = (`date`)
set DATE = $TIMESTAMP[3]$TIMESTAMP[2]$TIMESTAMP[6]
set TIME = `echo $TIMESTAMP[4] | sed "s/:/ /g"`
@ SECONDS = $TIME[1] * 3600 + $TIME[2] * 60 + $TIME[3]
set RELEASE = beta-$DATE"_"$SECONDS
echo "Name of this release is $RELEASE"
set BETA = $BETAROOT/$RELEASE

### Get user to create new directory
# We don't do this automatically because this may really be 
# the mount point of a brand new Coda or AFS volume
set reply
while (! (-e $BETA))
echo -n "Create empty directory or volume $BETA, then hit return ... "
set reply = "$<"
end

# We are sure directory exists; now make sure user wasn't
# lying about it being empty; this is a precaution to 
# prevent accidental clobbering of old directories
set DIRSIZE = `ls -a -1 $BETA| wc -l`
if ($DIRSIZE != 2) then
echo " LIAR\!\! The directory $RELEASE is not empty"
exit
endif



### Get names of all the files and directories in ALPHA

# First get all the names in ALPHA/src
# Then examine each one to see if it is a directory
cd $ALPHA/src
set ALL = (*)
set DIRS = "Toplevel"
foreach N ($ALL)
if (-d $N) then
if ($N != "RCS" && $N != "RCSLINK") set DIRS = ($DIRS $N)
endif
end

### Construct an image of ALPHA/src in BETA
# We assume that the state of ALPHA is frozen throughout these steps
# To truly enforce this assumption, we should do some form of locking

# Initialize
mkdir $BETA/src
ln -s $RCSDIR $BETA/src/RCSLINK
echo " "
echo "Checking out files from RCS and comparing with ALPHA"
unset ASK_USER_IF_OK

# Checkout files and verify they match ALPHA
# Note that rcsdiff doesn't set exit status correctly, so can't
# be used instead of rcsco followed by diff

foreach D ($DIRS)
echo -n "    $D :  "
if ($D == "Toplevel") then
cd $BETA/src
ln -s RCSLINK/TOPLEVEL $BETA/src/RCS
set DIFFDIR = $ALPHA/src
else
mkdir $BETA/src/$D
cd $BETA/src/$D
ln -s ../RCSLINK/$D RCS
set DIFFDIR = $ALPHA/src/$D
endif

rcsco -q RCS/* >& /dev/null
foreach F (*)
if ($F == "RCS" || $F == "RCSLINK") continue
unset NOTOK
# Does anyone have a lock on the file?
egrep -s "locks[\ ]+[a-z | A-Z | 0-9]+:" RCS/$F,v
if ($status == 0) set NOTOK =
# Does the RCS copy match what is in ALPHA?
diff $F $DIFFDIR/$F >& /dev/null
if ($status != 0) set NOTOK = 
if ($?NOTOK) then
set ASK_USER_IF_OK =
echo -n " $F "
else
echo -n "."
endif
end
echo " "
end

# Give user chance to examine differences, then ask whether to proceed
echo " "
if ($?ASK_USER_IF_OK) then
echo "Carefully examine suspicious files listed above"
echo "Also ensure RCS locks on their trunks are released"
set reply
while (($reply != "y") && ($reply != "n"))
echo -n "Should I continue? [yn] "
set reply = "$<"
if ($reply == "n") exit
if ($reply == "y") break
end
endif

### Create RCS branches and re-checkout to set $Header: makebeta,v 3.2.1.1 95/10/11 10:18:30 raiff Exp $ correctly
# The contents of BETA are now known to be correct

echo " "
echo "Creating new RCS branches and checking out"
foreach D ($DIRS)
echo -n "    $D :  "
if ($D == "Toplevel") then
cd $BETA/src
else
cd $BETA/src/$D
endif
foreach F (*)
if ($F == "RCS" || $F == "RCSLINK" || -d $F) continue
rcsci -b -f -n$RELEASE -m"Branch for release $RELEASE" -q $F >& /dev/null
rcsco -r$RELEASE -q $F >& /dev/null
echo -n "."
end
echo " "
end

### Edit MAKECODA & Makeconf to set new defaults

echo " "
echo "Editing toplevel files to set new defaults"
chmod 0644 $BETA/src/MAKECODA $BETA/src/Makeconf
sed -e "s|^set ROOT = .*|set ROOT = $BETA|" $BETA/src/MAKECODA > $BETA/src/MAKECODA.new
mv $BETA/src/MAKECODA.new $BETA/src/MAKECODA
sed -e "s|^ROOT = .*|ROOT = $BETA|" $BETA/src/Makeconf > $BETA/src/Makeconf.new
mv $BETA/src/Makeconf.new $BETA/src/Makeconf
chmod 0444 $BETA/src/MAKECODA $BETA/src/Makeconf


### Copy over special directories, and set up rest of tree
FOO:

# First construct machine-independent image
cd $BETA
ln -s @sys/lib lib
ln -s @sys/bin bin
ln -s @sys/lib-special lib-special
ln -s @sys/bin-special bin-special
mkdir include include-special
copy $ALPHA/include-special/* include-special
if (-e $ALPHA/src-special) then
mkdir src-special
copy $ALPHA/src-special/* src-special
endif


# Then construct machine-specific images
cd $ALPHA
echo " "
echo -n "Copying special directories for machine types:"
foreach M (*)
if (-d $M/lib-special && -d $M/bin-special) then
echo -n " $M"
mkdir $BETA/$M $BETA/$M/lib $BETA/$M/bin $BETA/$M/lib-special $BETA/$M/bin-special
copy $ALPHA/$M/bin-special/* $BETA/$M/bin-special
copy $ALPHA/$M/lib-special/* $BETA/$M/lib-special
endif
end
echo " "

### Print final message
echo " "
echo "New beta directory is now ready"
echo "    cd to $BETA/src"
echo "    run MAKECODA for each machine type"
