#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for grabbing cvs urls.
##
##=head1 DESCRIPTION
##
## This file contains functions for parsing cvs urls.
##
##=head1 CVS URL Format
##
## There is no standard (that I know of) for cvs urls so we use a 
## source mage specific format:
##
##      cvs://CVSROOT:MODULE_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:
##
##      cvs://CVSROOT:MODULE_NAME:REVISION_TAG
##
## The CVSROOT portion of the url may include information such as type of cvs 
## server, port number for the server, user name, password, cvs repository 
## directory, etc.  The CVSROOT syntax is defined by cvs and is as follows:
## 
##      :method:[[user][:password]@]hostname[:[port]]/path/to/repository
##
## For more details, see the CVS manual at 
## http://www.cvshome.org/docs/manual/cvs.html
##
##=head1 EXAMPLES
##
## Suppose we want to download the latest version of the sorcery
## scripts from cvs.  We'd use the following url:
##
##      cvs://:pserver:anonymous@mplayerhq.hu:/cvsroot/mplayer:main
##
## If we want the 1.0pre7 release instead (i.e., those files tagged with
## 1_0pre7, we would use the following url:
##
##      cvs://:pserver:anonymous@mplayerhq.hu:/cvsroot/mplayer:main:1_0pre7
##
## Some cvs repositories require passwords.  One such repository is the
## cvs repository for the ROOT package (an object-oriented data analysis 
## framework, see L<http://root.cern.ch/root/>). The CVSROOT, without the 
## password, would look like this:
##
##      :pserver:cvs@root.cern.ch:/user/cvs
##
## The password for their cvs repository is I<cvs>.  Adding the password would
## make the CVSROOT look like this:
## 
##      :pserver:cvs:cvs@root.cern.ch:/user/cvs
##
## Thus, the full cvs url, including the password, would be:
##
##       cvs://:pserver:cvs:cvs@root.cern.ch:/user/cvs:root
##
##=head1 COPYRIGHT
##
## Copyright 2002 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------

#---------------------------------------------------------------------
##=item url_file_download <url>
## 
## Parse the specified cvs url.
##
## @global URL
## @global CVS_ROOT
## @global CVS_MODULE
## @global CVS_TAG
##
#---------------------------------------------------------------------
function url_cvs_crack() { 

  URL=`url_strip_prefix "$1" cvs`
  CVS_ROOT=`echo $URL | sed "s#\(^[^/]*[^:]*\):.*#\1#"`
  local CVS_MODULE_TAG=`echo $URL | sed "s#^[^/]*[^:]*\(.*\)#\1#"`
  CVS_MODULE=`echo $CVS_MODULE_TAG | cut -d : -f2` 
  local CVS_TAGNAME=`echo $CVS_MODULE_TAG | cut -d : -f3`
  CVS_TAG=${CVS_TAGNAME:=HEAD}

}

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

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

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

  for each in $@ ; do
    tmp_hostname=$(url_cvs_hostname $each)
    # since we had to pull the url appart 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
##
#---------------------------------------------------------------------

