#!/bin/bash
#
#  Script: merge_trunk_to_branch
# 
#  Purpose: 
#      Merges the trunk into a branch
#
#  Programmer: Hank Childs
#  Creation:   May 14, 2007
#
#  Modifications:
#
#    Hank Childs, Mon Dec 17 16:12:45 PST 2007
#    Explicitly enumerate the list of directories to merge over.
#
#    Brad Whitlock, Fri Nov 13 15:33:10 PST 2009
#    I added a -user argument.
#
# *****************************************************************************

issueHelp="no"

P=$(which $0)
P2=${P%/*}
. ${P2}/visit_svn_helper

# Prevent print statements when changing directories
CDPATH=""

BRANCH=""

user=$SVN_NERSC_NAME
if [[ $# == 2 ]] ; then
   if [[ "$1" == "-user" ]] ; then
       user=$2
   else
       issueHelp="yes"
   fi
elif [[ $# != 0 ]] ; then
   issueHelp="yes"
fi

if [[ ! -f .branchname ]] ; then
   echo ""
   echo "ERROR: You must run this script at the top of your branch directory"
   echo ""
   issueHelp="yes"
elif [[ ! -f .rootpath ]] ; then
   echo ""
   echo "ERROR: You must run this script at the top of your branch directory"
   echo ""
   issueHelp="yes"
elif [[ "$issueHelp" == "no" ]] ; then
   BRANCHNAME=$(cat .branchname)
   ROOTPATH=$(cat .rootpath)
   BRANCH=${VISIT_SVN_BRANCHES}/${user}/${BRANCHNAME}${ROOTPATH}
   svn ls $BRANCH 2>/dev/null > /dev/null
   if [[ $? != 0 ]] ; then
      echo "The branch $1 does not appear to exist."
      echo "(Looking for it at $BRANCH)"
      echo "Try using the script ls_branches to locate your branch."
      issueHelp="yes"
   fi
fi

if [[ "$issueHelp" == "yes" ]] ; then
   echo ""
   echo "Usage:   ${0##*/} [-user name]"
   echo ""
   echo "Example: ${0##*/}"
   echo "         ${0##*/} -user name"
   echo ""
   echo "\tThis command should be run at the top level of your checked out branch."
   echo "\tIt will use the contents of the file \".branchname\" to determine which branch "
   echo "\tto merge the trunk into."
   echo ""
   
   exit 1
fi


echo ""
echo "Merging trunk into branch $BRANCH"
echo ""

echo ""
echo "Getting record of last merge from trunk into branch..."

mkdir tmp_forRev$$
cd tmp_forRev$$
svn checkout --quiet ${VISIT_SVN_BRANCHES}/${user}/${BRANCHNAME}/svninfo
cd svninfo
REV=$(cat Rev_fromTrunk)
cd ../..
echo "The revision of the last merge was $REV"
echo ""

for i in src data docs test third_party windowsbuild ; do
   if [[ -d $i ]] ; then
       cd $i
       echo ""
       echo "Merging directory \"$i\""
       echo ""
       svn merge -r ${REV}:HEAD ${VISIT_SVN_TRUNK}/${ROOTPATH}/$i
       cd ..
   fi
done
NEW_REV=$(get_latest_rev)

cd tmp_forRev$$/svninfo
echo "$NEW_REV" > Rev_fromTrunk
echo ""
echo "Updating branch $BRANCHNAME to contain a record of this merge..."
echo ""
svn commit --quiet -m "Update for revision sent from trunk to $BRANCHNAME, $REV to $NEW_REV"
cd ../..
rm -Rf tmp_forRev$$

echo ""
echo "The changes from the trunk have been put on your working copy."
echo "You need to review the changes and do an \"svn commit\" for these changes "
echo "to be saved.  Note that the branch has been updated with the info that this "
echo "merge took place and future merges from the trunk will not attempt to merge "
echo "these revisions again.  If you want to back out this merge, you will need "
echo "invoke the following command to update our bookkeeping: "
echo ""
echo "  set_trunk_to_branch_revision $BRANCHNAME $REV"
echo ""

exit 0

