#! /bin/csh -f
#
# USAGE
#     cd ~/projects/work/...; copy-work-to-alpha.csh
#
# DESCRIPTION:
#     This script uses tar to copy an entire projects work directory to its
#     corresponding projects alpha directory.  Emacs backup and other
#     unnecessary files are not copied.
#
# SEE ALSO:
#     ./update-work-to-alpha.csh, which updates only changed files.
#
#

# Ensure that we're in a projects/work dir.
set c = `pwd`
set d = $c:t
cd ..
set w =  `pwd`
set w = $w:t
cd ..
set p =  `pwd`
set p = $p:t
if ( ! ( ($w == "work") && ($p == "projects") ) ) then
    echo "Must be in a projects/work subdirectory."
    exit 1
endif

# Go back down to the src dir.
cd $c

# Update the copy log file with the current date and 'Full Copy' note.
echo "" >> administration/.copy-to-alpha.log
echo `date` >> administration/.copy-to-alpha.log
echo "Full Copy" >> administration/.copy-to-alpha.log

# Unconditionally nuke the dest dir (or file or link), and make a new one.
chmod -R u+w ../../alpha/$d
rm -rf ../../alpha/$d
mkdir ../../alpha/$d

# Do the deed with tar.  Note also that the copying is done fully, and the junk
# files are then deleted.  I tried the smarter way with find, a la,
# update-work-to-alpha, but it didn't do the trick.
tar cf - * | (cd ../../alpha/$d; tar xvf -)

# Update the files who's modification dates record the date of this copy as
# well as the two previous copies (or updates).  Note that copy and update both
# touch the same date files.
if (-e administration/.last-copied-to-alpha) then
    mv administration/.last-copied-to-alpha \
        administration/.previously-last-copied-to-alpha
else
    touch administration/.last-copied-to-alpha \
        administration/.previously-last-copied-to-alpha
endif

if (-e administration/.copied-to-alpha) then
    mv administration/.copied-to-alpha administration/.last-copied-to-alpha
else
    touch administration/.last-copied-to-alpha
endif

touch administration/.copied-to-alpha

# Finish things up by nuking all junk or work-only files.  The current
# definition of junk or work-only files is as follows:
#
#     FILENAME PATTERN		DESCRIPTION
#     ============================================================
#	*~			Emacs backup
#	.*~			  "     "
#	#*			  "   checkpoint
#	.copy-to-alpha.out	Log file of this script
#	*.o			Object file (must be rebuilt in alpha)
#	SUN*-work, HP*-work	Working build directories
#
cd ../../alpha/$d
find . \( \
        \( -name '*~' \) -o \( -name '.*~' \) -o \( -name '#*' \) -o \
        \( -name .copy-to-alpha.out \) -o \( -name '*.o' \) -o \
        \( -name 'SUN*-work' \) -o \( -name 'HP*-work' \) \) \
    -print \
    -exec rm -rf {} \;

# Set all alpha files to read only, to avoid any accidental modification of
# alpha files.  When we go to build in the alpha dir, this will require
# selective changing of files to allow the alpha builds to be done.  For now
# the accidental modification to alpha files is considered a bigger problem
# than having to selectively change protections for alpha builds.  In future,
# we can ameliorate the situation by either doing selective protection updates
# here, or in Makefiles.
chmod -R a-w .
