#  Script: visit_svn_help
#
#  Purpose:
#      A common location for SVN routines.
#
#  Programmer: Hank Childs
#  Creation:   May 12, 2007
#
#  Modifications:
#
#    Hank Childs, Wed Jan  9 08:44:30 PST 2008
#    Fix typo, add svn_mv_dir.
#
#    Hank Childs, Thu Aug  7 08:41:15 PDT 2008
#    Add support for two digit minor releases, such as 1.10 or 1.10.1.
#
#    Gunther H. Weber, Fri Mar 13 11:04:00 PDT 2009
#    Replaced bashisms with POSIX so that script runs on Ubuntu with dash   
#
#    Hank Childs, Thu Apr 23 15:22:52 PDT 2009
#    Fix some mistakes with "==" getting translated to "-ne".
#
#    Jeremy Meredith, Tue Aug  9 16:09:28 EDT 2011
#    Updated repository to be at new location.
#
# *****************************************************************************


##
##
## Check to make sure that environment variables are set
##
##

if [ "$SVN_NERSC_NAME" = "" ] ; then
   echo "You must define the environment variable SVN_NERSC_NAME to use these scripts."
   echo "This should be your username on edison.nersc.gov"
   exit 1
fi


##
##
## Set up environment variables for other scripts
##
##

VISIT_SVN_REPO=svn+ssh://${SVN_NERSC_NAME}@edison.nersc.gov/project/projectdirs/visit/svn/visit
VISIT_SVN_BRANCHES=${VISIT_SVN_REPO}/branches
VISIT_SVN_TRUNK=${VISIT_SVN_REPO}/trunk
VISIT_SVN_TAGS=${VISIT_SVN_REPO}/tags


##
##
## Define utility functions
##
##

get_latest_rev()
{
    # This assumes a certain format for SVN log.  It gets the second line,
    # which has "r<REV> | ...", and then strips off the "| ...", and then 
    # strips off the 'r'.
    # first check for old non posix tail command
    if [ $(uname -s) != "SunOS" ] ; then
    REV=$(svn log $VISIT_SVN_REPO --revision HEAD | head -n 2 | tail -n 1 | cut -d'|' -f1 | sed 's/r//g')
    else
    REV=$(svn log $VISIT_SVN_REPO --revision HEAD | head -2 | tail -1 | cut -d'|' -f1 | sed 's/r//g')
    fi
    if (( REV < 950 )) ; then
       echo "An error occurred in getting revision!!" >&2
       echo "Pursue this problem with a VisIt-SVN guru" >&2
       echo "1000000"
    else
       echo $REV
    fi
}


# Note that the is_version_string test would fail for numbers bigger than 9,
# such as 1.6.10.  If this ever comes up, it should be easy to fix.
is_version_string()
{
    if [ ${#1} -eq 3 ] ; then
       is_two_digit_version_string $1
       return $?
    elif [ ${#1} -eq 4 ] ; then
       is_two_digit_version_string $1
       return $?
    elif [ ${#1} -eq 5 ] ; then
       is_three_digit_version_string $1
       return $?
    elif [ ${#1} -eq 6 ] ; then
       is_three_digit_version_string $1
       return $?
    fi

    return 0
}

is_two_digit_version_string()
{
    if [ ${#1} -ne 3 ] ; then
        if [ ${#1} -ne 4 ] ; then
            return 0
        fi
    fi

    typeset X
    typeset -L1 X=$1
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L2 X=$1
    typeset -R1 X
    if [ "$X" != "." ] ; then
        return 0
    fi

    typeset -L3 X=$1
    typeset -R1 X
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    if [ ${#1} = 4 ] ; then
        typeset -L4 X=$1
        typeset -R1 X
        if [ "$X" != [0-9] ] ; then
            return 0
        fi
    fi

    return 1
}

is_three_digit_version_string()
{
    if [ ${#1} != 5 ] ; then
        return 0
    fi

    typeset X
    typeset -L1 X=$1
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L2 X=$1
    typeset -R1 X
    if [ "$X" != "." ] ; then
        return 0
    fi

    typeset -L3 X=$1
    typeset -R1 X
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L4 X=$1
    typeset -R1 X
    if [ "$X" = "." ] ; then
        typeset -L5 X=$1
        typeset -R1 X
        if [ "$X" != [0-9] ] ; then
            return 0
        fi
    elif [ "$X" != "." ] ; then
        if [ "$X" != [0-9] ] ; then
            return 0
        fi
        typeset -L5 X=$1
        typeset -R1 X
        if [ "$X" != "." ] ; then
            return 0
        fi
        typeset -L6 X=$1
        typeset -R1 X
        if [ "$X" != [0-9] ] ; then
            return 0
        fi
    fi

    return 1
}

is_four_digit_version_string()
{
    if [ ${#1} != 7 ] ; then
        return 0
    fi

    typeset X
    typeset -L1 X=$1
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L2 X=$1
    typeset -R1 X
    if [ "$X" != "." ] ; then
        return 0
    fi

    typeset -L3 X=$1
    typeset -R1 X
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L4 X=$1
    typeset -R1 X
    if [ "$X" != "." ] ; then
        return 0
    fi

    typeset -L5 X=$1
    typeset -R1 X
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    typeset -L6 X=$1
    typeset -R1 X
    if [ "$X" != "." ] ; then
        return 0
    fi

    typeset -L7 X=$1
    typeset -R1 X
    if [ "$X" != [0-9] ] ; then
        return 0
    fi

    return 1
}


svn_cp_dir()
{
   SRC=$1
   DEST=$2

   # Confirm presence of $SRC
   svn ls $SRC 2>/dev/null > /dev/null
   if [ $? -ne 0 ] ; then
      echo "Cannot locate directory ($SRC) for copying.  Aborting!"
      echo "(Try using ls_branches to locate the directory.)"
      exit 1
   fi

   # Confirm that $DEST does not already exist
   svn ls $DEST 2>/dev/null > /dev/null
   if [ $? -eq 0 ] ; then
      echo "Destination ($DEST) already exists.  Aborting!"
      exit 1
   fi

   echo "Creating ${DEST}...."
   svn copy $SRC $DEST -m "Making copy of $SRC as $DEST"
   if [ $? -eq 0 ] ; then
      echo "Successful."
   else
      echo "Not successful."
      exit 1
   fi
}


svn_mv_dir()
{
   SRC=$1
   DEST=$2

   # Confirm presence of $SRC
   svn ls $SRC 2>/dev/null > /dev/null
   if [ $? -ne 0 ] ; then
      echo "Cannot locate directory ($SRC) for moving.  Aborting!"
      echo "(Try using ls_branches to locate the directory.)"
      exit 1
   fi

   # Confirm that $DEST does not already exist
   svn ls $DEST 2>/dev/null > /dev/null
   if [ $? -eq 0 ] ; then
      echo "Destination ($DEST) already exists.  Aborting!"
      exit 1
   fi

   echo "Creating ${DEST}...."
   svn move $SRC $DEST -m "Making copy of $SRC as $DEST"
   if [ $? -eq 0 ] ; then
      echo "Successful."
   else
      echo "Not successful."
      exit 1
   fi
}


svn_rm_dir()
{
   DIR=$1
   # Confirm presence of $DIR
   svn ls $DIR 2>/dev/null > /dev/null
   if [ $? -ne 0 ] ; then
      echo "Cannot locate release candidate ($DIR) for deleting.  Aborting!"
      exit 1
   fi

   svn delete $DIR -m "Removing directory $DIR"
   return $?
}



