#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for grabbing svn urls.(subversion)
##
##=head1 DESCRIPTION
##
## This file contains functions for parsing svn urls.
##
##=head1 SVN URL Format
##
## There did not used to be a standard for svn urls, a source mage
## specific format was invented:
##
##      svn://SVNURL:DIR_NAME
##
## The above url will download the latest version of the specified
## module (i.e., the HEAD revision). To specify a specific revision,
## the following format can be used:
##
##      svn://SVNURL:DIR_NAME:REVISION_TAG
##
## The SVNURL portion of the url will appear as a normal http url.
## The DIR_NAME will be the tail element of the SOURCE_DIRECTORY.
##
## Future work will differntiate between real svn urls and our smgl
## specific version, and expanding our smgl version to use protocols
## besides http.
##
## 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 the sorcery
## scripts 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
##
## The assumption is made that the svn url is actually an http url.
## ie. you could use your web browser to access:
## http://svn.pld-linux.org/svn/bmp-plugins/trunk
##
## svn repositories requiring passwords are not currently supported.
##
##=head1 COPYRIGHT
##
## Copyright 2004 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------

#---------------------------------------------------------------------
##=item url_file_download <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#"`
  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
##
#---------------------------------------------------------------------
