#!/bin/bash
#
#  Script: co_branch
# 
#  Purpose: 
#      Checks out a branch into the $PWD.
#
#  Programmer: Hank Childs
#  Creation:   May 12, 2007
#
#  Modifications:
#
#    Hank Childs, Sat Jun 23 15:47:53 PDT 2007
#    Added support for RC branches.
#
#    Hank Childs, Fri Feb 22 10:27:16 PST 2008
#    Ported from ksh to bash.
#
#    Hank Childs, Fri Feb 22 10:52:45 PST 2008
#    Add "-user" flag, so multiple developers can share a branch.
#
#    Hank Childs, Mon Feb 25 09:04:07 PST 2008
#    Remove print statements that were left in.
#
#    Sean Ahern, Thu May 15 16:53:56 EDT 2008
#    Fixed \t printing problems by turning them into spaces.
#
#    Cyrus Harrison, Mon Nov 23 12:30:38 PST 2009
#    Disabled the previous merge check b/c answering "yes" in the wrong
#    situation has the potential to ruin a branch. If this is needed
#    in the future it should be refactored to protect the health of the branch.
#
# *****************************************************************************

issueHelp="no"
CDPATH="" # Prevent unneeded echoing of chdir's.

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

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

if [[ "$issueHelp" == "yes" ]] ; then
   echo ""
   echo "Usage:   ${0##*/} <branch> <dir>"
   echo "Usage:   ${0##*/} <branch> <known-dir-combo>"
   echo "Optional argument: -user <username>"
   echo "     (Allows you to check out another developers branch)"
   echo "     (Make sure to specify that developers NERSC username)"
   echo ""
   echo "Example: ${0##*/} my_dev_work /src"
   echo "    will check out the /src directory of the branch my_dev_work "
   echo "    into the subdirectory /my_dev_work"
   echo ""
   echo "Example: ${0##*/} my_dev_work /src/plots/Volume"
   echo "    will check out the /src/plots/Volume directory of the branch "
   echo "    my_dev_work into the subdirectory /my_dev_work"
   echo ""
   echo "known-dir-combos: \"all\" (all subdirs), \"sdt\" (source-data-test)"
   echo ""
   echo "Example: ${0##*/} my_dev_work sdt"
   echo "    will check out the /src, /data/, and /test directories of the "
   echo "    branch my_dev_work into the subdirectory /my_dev_work"
   echo ""
   echo "Note: you *can* check out different directories through multiple "
   echo "invocations, for example: ${0##*/} branch /src followed by "
   echo "${0##*/} branch /data"
   echo ""
   echo "But you *CANNOT* do this if the directories are at different levels "
   echo "in the directory structure.  For example, you *CANNOT* do "
   echo "\"${0##*/} branch /src/plots/Volume\" and then later do \"${0##*/} branch /src/\""
   echo ""
   exit 1
fi


function checkout_dir
{
   BRANCH=${VISIT_SVN_BRANCHES}/${3}/${1}/${2#/}
   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."
      exit 1
   fi
   
   if [[ "${2#/}" == "${2}" ]] ; then
      TMP="/${2}"
      ROOT="${TMP%/*}"
   else
      ROOT="${2%/*}"
   fi
   if [[ "$ROOT" == "" ]] ; then
      ROOT="/"
   fi

   if [[ -d ${1} ]] ; then
      cd ${1}
      if [[ ! -f .branchname ]] ; then
         echo "The subdirectory I want to check out the files into appears to "
         echo "be corrupted.  It is missing the file \".branchname\"."
         exit 1
      fi
      NAME=$(cat .branchname)
      if [[ "$NAME" != "${1}" ]] ; then
         echo "The subdirectory I want to place the check out in is for a "
         echo "different branch.  You are requesting ${1}, but it is for "
         echo "branch $NAME"
         exit 1
      fi
      if [[ ! -f .rootpath ]] ; then
         echo "The subdirectory I want to check out the files into appears to "
         echo "be corrupted.  It is missing the file \".rootpath\"."
         exit 1
      fi
      ROOT2=$(cat .rootpath)
      if [[ "$ROOT" != "$ROOT2" ]] ; then
         echo "The subdirectory I want to place the check out in is at a different "
         echo "level of the directory hierarchy.  You can still do a checkout "
         echo "of this subdirectory, but you may not do it here, since it makes "
         echo "merging too difficult.  So you should go to a different directory "
         echo "and issue this checkout command again."
         echo "Your previous checkout(s) have been from ${ROOT2}"
         echo "You are now requesting a checkout from ${ROOT}"
         exit 1
      fi
      cd ..
   else
      mkdir ${1}
      cd ${1}
      echo ${1} > .branchname
      chmod 400 .branchname
      echo ${ROOT} > .rootpath
      chmod 400 .rootpath
      cd ..
   fi
   
   cd ${1}
   echo "Getting files for \"${2}\" from remote repo..."
   svn checkout --quiet --non-interactive $BRANCH

   mkdir tmp_forRev$$
   cd tmp_forRev$$
   svn checkout --quiet ${VISIT_SVN_BRANCHES}/${3}/${1}/svninfo > /dev/null
   cd svninfo
   init="notFound"
   if [[ ! -f Rev_initial ]] ; then
       echo "Cannot locate file Rev_initial ... is this a valid branch?"
   else
       init=$(cat Rev_initial)
   fi
   current="notFound"
   if [[ ! -f Rev_fromTrunk ]] ; then
       if [[ ! -f Rev_fromRCTrunk ]] ; then
           echo "Cannot locate file Rev_fromTrunk ... is this a valid branch?"
       else
           current=$(cat Rev_fromRCTrunk)
       fi
   else
       current=$(cat Rev_fromTrunk)
   fi
   cd ../../
   rm -Rf tmp_forRev$$
   #
   #  I disabled this check b/c answering "yes" in the wrong situation 
   #  has the potential to ruin a branch - Cyrus
   #
   #if [[ "$current" != "$init" ]] ; then
   #
   if [  ] ; then
      stop="no"
      while [[ "$stop" == "no" ]] ; do
           echo ""
           echo "ATTENTION"
           echo ""
           echo "A merge from the trunk has taken place on this branch previously."
           echo "The branch was created at revision ${init}, but the merge took it to ${current}."
           echo "It is not known whether the merge took place on the \"${2}\" directory"
           echo "or not.  You must decide if you would like to merge the changes "
           echo "from ${init} to ${current} onto \"${2}\"."
           echo ""
           echo "You have three options: \"yes\", \"no\", and \"abort\"." 
           echo ""
           echo "\"Yes\" means that you will do the merge and that the working copy "
           echo "you receive will be at the current revision of the trunk."
           echo ""
           echo "\"No\" means that you will not do the merge and nothing will "
           echo "change on this directory.  If you have previously checked out "
           echo "and done a merge on this directory, then this is the right "
           echo "option.  If you have *not* done a checkout or merge on this "
           echo "directory before, then you will get the same revision of this "
           echo "directory from when you issed \"mk_branch_from_trunk\"."
           echo ""
           echo "\"Abort\" will exit you from this script.  The checkout of \"${2}\""
           echo "has already occurred, but it was not merged and no further "
           echo "directories will be checked out."
           echo ""
           echo "Enter \"yes\", \"no\", or \"abort\":"
           read answer
           if [[ "$answer" == "yes" ]] ; then
              stop="yes"
           fi
           if [[ "$answer" == "no" ]] ; then
               stop="yes"
           fi
           if [[ "$answer" == "abort" ]] ; then
               stop="yes"
           fi
       done
       if [[ "$answer" == "abort" ]] ; then
           exit 1
       elif [[ "$answer" == "yes" ]] ; then
           echo ""
           echo "Merging"
           echo ""
           if [[ "$init" == "notFound" ]] ; then
              echo "Don't have the initial merging info ... can't proceed with merge"
           else
              RETURN_TO=${PWD}
              cd ${2}
              svn merge -r ${init}:HEAD ${VISIT_SVN_TRUNK}/$2
              cd ${RETURN_TO}
           fi
           echo ""
           echo "Done merging.  Note: the changes were not automatically "
           echo "committed.  If there were changes, you will need to commit them."
           echo ""
       fi 
   fi

   cd ..
}

if [[ "$2" == "sdt" ]] ; then
   echo "Checking out /src"
   checkout_dir $1 "/src" $user
   echo "Checking out /data" 
   checkout_dir $1 "/data" $user
   echo "Checking out /test"
   checkout_dir $1 "/test" $user
elif [[ "$2" == "all" ]] ; then
   echo "Checking out /src"
   checkout_dir $1 "/src" $user
   echo "Checking out /data"
   checkout_dir $1 "/data" $user
   echo "Checking out /test"
   checkout_dir $1 "/test" $user
   echo "Checking out /docs"
   checkout_dir $1 "/docs" $user
   echo "Checking out /third_party"
   checkout_dir $1 "/third_party" $user
   echo "Checking out /windowsbuild"
   checkout_dir $1 "/windowsbuild" $user
else
   checkout_dir $1 "$2" $user
fi

echo ""
echo "Your check out is ready!"
echo "cd to the \"$1\" directory to access the working copy."
echo ""

exit 0

