#! /bin/csh -f
#
# USAGE
#     cd ~/projects/work/<some-project-dir>; copy-work-to-alpha.csh
#
# DESCRIPTION:
#     This script uses tar to copy all newly updated files in a projects work
#     directory to the corresponding projects alpha directory, where "newly
#     updated" means changed since the last update or copy.  Emacs backup and
#     other unnecessary files are not copied.
#
# SEE ALSO:
#     ./copy-work-to-alpha.csh, which copies all files into a brand new alpha
#     dir.
#
#

# 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 into to the alpha dir to do some checking.
cd alpha
set a = `pwd`

# Ensure that the dest is a dir, not a file or a sym link, the latter of which
# would be disastrous tarwise.
if ( ! -d $d ) then
    echo "Destination alpha must be a directory."
    exit 1
endif

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

# Ensure that there's a .copied-to-alpha file, indicating that at least one
# copy has been made.
if (! -e administration/.copied-to-alpha) then
    echo "No copy to alpha has yet been made -- use copy-work-to-alpha.csh"
    echo "instead of update-work-to-alpha.csh."
    exit 1
endif

# Find all the newly updated non-junk, non-work-only files with find.  See
# ./copy-work-to-alpha.csh for an explanation of which files these are.  Note
# that this find omits dirs, but it will find new files in new dirs.  Given
# that tar is used to copy the found files, and tar creates dirs when
# necessary, the only things that will be missed are new dirs that contain no
# files, which is OK.
find . -newer administration/.last-copied-to-alpha -type f \! \( \
        \( -name '*~' \) -o \( -name '.*~' \) -o \( -name '#*' \) -o \
        \( -name .copy-to-alpha.out \) -o \( -name '*.o' \) -o \
        \( -name 'SUN*-work' \) -o \( -name 'HP*-work' \) \
    \) \
    -print \
    |& tee administration/.copy-to-alpha.out

# Do the copying and logging, if there are any files to copy.
if (! -z administration/.copy-to-alpha.out) then

    # Update the copy log file with the current date and append the list of 
    # files to be copied.
    echo "" >> administration/.copy-to-alpha.log
    echo `date` >> administration/.copy-to-alpha.log
    cat administration/.copy-to-alpha.out >> administration/.copy-to-alpha.log

    # Turn on alpha write permissions to enable the copying.
    chmod -R u+w ../../alpha/$d

    # Do the deed with tar.
    tar cf - `cat administration/.copy-to-alpha.out` | \
        (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
else
    echo "Nothing to copy, checking for deletes."
endif

# Delete all alpha files that are not also in work.  These are files that have
# been deleted from work since the last update.  Note that this assumes (and
# enforces to some extent) that no edits take place on alpha files.
cd $a/$d
find . -type f -exec \
    ~/projects/work/support/administration/delete-non-work-file.csh $d {} \;

# Delete all dirs similarly.  Deleting is done in these two passes so nothing
# funky happens if we try to delete a file that find finds after we've deleted
# its dir.  I'm not sure this could happen, but let's not push our luck.
find . -type d -exec \
    ~/projects/work/support/administration/delete-non-work-dir.csh $d {} \;


# 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 .
