#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for parsing subversion urls.
##
##=head1 SVN URL Format
##
## The source mage specific format is:
##
##      svn://SVNURL:DIR_NAME
##      svn://SVNURL:DIR_NAME:REVISION_TAG
##
## It is exactly the same as a standard svn:// url, with additional
## tokens at the end DIR_NAME and optional REVISITION_TAG
##
## The svn://SVNURL portion of the url will appear on the svn command
## line as the url.
##
## The DIR_NAME will be the tail element of the SOURCE_DIRECTORY.
##
## In order to maintain compatibility with the original svn url format,
## which uses http:// as the underlying protocol, a hint may be specified
## named "old_svn_compat".
##
## For more details, see the SVN manual at 
## http://svnbook.red-bean.com/svnbook/ch03s04.html
##
##=head1 EXAMPLES
##
## Suppose we want to download the latest version of bmp-plugins
## from svn.  We'd use the following url:
##
##      svn://svn.pld-linux.org/svn/bmp-plugins/trunk:bmp-plugins-svn
##
## If we want the 4474 revision number we would use the following url:
##
##      svn://svn.pld-linux.org/svn/bmp-plugins/trunk:bmp-plugins-svn:4474
##
## svn repositories requiring passwords are not currently supported.
##
##=head1 COPYRIGHT
##
## Copyright 2004 by the Source Mage Team
## Copyright 2005 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------

#---------------------------------------------------------------------
##=item url_svn_crack <url>
## 
## Parse the specified svn url.
##
## @Global URL
## @Global SVN_ROOT
## @Global SVN_MODULE
## @Global SVN_TAG
##
#---------------------------------------------------------------------
function url_svn_crack() { 

  URL=`url_strip_prefix "$1" svn`
  SVN_ROOT=`echo $URL | sed "s#\(^[^/]*[^:]*\):.*#\1#"`
  if list_find "$2" old_svn_compat; then
    SVN_ROOT=http://$SVN_ROOT
  else
    SVN_ROOT=svn://$SVN_ROOT
  fi
  local SVN_MODULE_TAG=`echo $URL | sed "s#^[^/]*[^:]*\(.*\)#\1#"`
  SVN_MODULE=`echo $SVN_MODULE_TAG | cut -d : -f2` 
  local SVN_TAGNAME=`echo $SVN_MODULE_TAG | cut -d : -f3`
  SVN_TAG=${SVN_TAGNAME:=HEAD}

}

#---------------------------------------------------------------------
##=item url_svn_is_valid <url>
##
## Ensure that all the fields that should be parsed out from a url
## do indeed exist
#---------------------------------------------------------------------
function url_svn_is_valid() {
  local URL SVN_ROOT SVN_MODULE SVN_TAG item
  url_svn_crack $1
  for item in URL SVN_ROOT SVN_MODULE SVN_TAG; do
    if ! [[ ${!item} ]] ; then
      return 1
    fi
  done
}

#---------------------------------------------------------------------
##=item url_svn_hostname <url>
##
## Get the hostname of the url
##
#---------------------------------------------------------------------
function url_svn_hostname() {
  echo $1|sed 's#^svn://\([^/:]*\)[/:].*$#\1#'
}

#---------------------------------------------------------------------
##=item url_svn_netselect <url>
##
## Gets a netselect type output for the url
##
#---------------------------------------------------------------------
function url_svn_netselect() {
  local tmp_hostname url_speed each

  for each in "$@" ; do
    tmp_hostname=$(url_svn_hostname $each)
    # since we had to pull the url apart to give netselect
    # something it can understand we'll just pretend like
    # multiple A records wont exist for this host...
    url_speed=$(netselect -s 1 $tmp_hostname 2>/dev/null|awk '{print $1}')
    [[ -n $url_speed ]] && echo "$url_speed $each"
  done
}

#---------------------------------------------------------------------
##=back
##
##=head1 LICENSE
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
