#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for grabbing svn urls.(subversion)
##
##=head1 DESCRIPTION
##
## This file contains functions for I<downloading> files through svn.
##
## In order for svn urls to be downloaded, the I<subversion-client> spell 
## must have been cast. This script first determines if subversion has been 
## installed before attempting to download a svn url.
##
##=head1 SVN URL Format
##
## There is no standard (that I know of) for svn urls so we use a 
## source mage specific format:
##
##      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 normail http url.
## The DIR_NAME will be the tail element of the SOURCE_DIRECTORY.
##
## 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
##
## (Note about svn repositories requiring passwords? Haven't seen one yet)
##
##=head1 IMPLEMENTATION NOTE
##
## Downloading is supported but SVN url verification is not
## currently supported.
##
##=head1 COPYRIGHT
##
## Copyright 2004 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------


#---------------------------------------------------------------------
#=item url_svn_strip_prefix <url>
# 
# Private function.  Strips off the svn:// prefix from svn urls
#
#---------------------------------------------------------------------
function url_svn_strip_prefix() {
  echo $1 | sed 's/^svn:\/\///'
}


#---------------------------------------------------------------------
##=item url_file_download <url>
## 
## Fetch the specified svn url.
##
#---------------------------------------------------------------------
function url_svn_download() { 
  if [ ! -x /usr/bin/svn ]; then
    return 1;
  fi

  local             URL=`url_svn_strip_prefix "$1"`
  local        SVN_ROOT=`echo $URL | sed "s#\(^[^/]*[^:]*\):.*#\1#"`
  local  SVN_MODULE_TAG=`echo $URL | sed "s#^[^/]*[^:]*\(.*\)#\1#"`
  local      SVN_MODULE=`echo $SVN_MODULE_TAG | cut -d : -f2` 
  local     SVN_TAGNAME=`echo $SVN_MODULE_TAG | cut -d : -f3`
  local         SVN_TAG=${SVN_TAGNAME:=HEAD}

  if [ -z "$SVN_ROOT" -o -z "$SVN_MODULE" ]; then
    return 1;
  fi

  if [ -z "$FILE" ]; then
    return 1;
  fi

  local S_FILE=`echo $FILE            |
                sed "s/\.tar\.gz$//"  |
                sed "s/\.tgz$//"      |
                sed "s/\.tar\.bz2$//"`

  if [ -f $SOURCE_CACHE/$S_FILE.tar.bz2 ]; then
    message "${MESSAGE_COLOR}Previous source found. Extracting package $S_FILE.tar.bz2...${DEFAULT_COLOR}"
    tar --bzip2 -xf $SOURCE_CACHE/$S_FILE.tar.bz2
    message "${MESSAGE_COLOR}Running svn update...${DEFAULT_COLOR}"
    if cd $S_FILE ; then
        svn update -q --non-interactive -r $SVN_TAG
        cd ..
	fi
  else
    message "${MESSAGE_COLOR}Running svn checkout...${DEFAULT_COLOR}"
    #NOTE: hardcoded http:// on next line
	svn checkout -q -r $SVN_TAG http://$SVN_ROOT $SVN_MODULE
  fi

  if [[ $nocache == 1 ]] ; then
    return $?
  fi

  if [ $? == 0 ]; then
    message "${MESSAGE_COLOR}Creating package $S_FILE.tar.bz2...${DEFAULT_COLOR}"
    tar --bzip2 --remove-files -cf $S_FILE.tar.bz2 $S_FILE/
    rm -fr $S_FILE/
    FILE=$S_FILE.tar.bz2
  else
    false;
  fi
}


#---------------------------------------------------------------------
##=item url_svn_verify <url>
## 
## Verifies the specified svn url exists.  Returns true for now until
## I get around to implementing it.
##
#---------------------------------------------------------------------u
function url_svn_verify() {
  true
}


#---------------------------------------------------------------------
##=item url_svn_set_options <option>
##
## Set options for url handling
## NOTE!: I dunno what this should do for svn...
##
#---------------------------------------------------------------------
function url_svn_set_options() {
  eval "$1=1"
}

#---------------------------------------------------------------------
##=item url_svn_hostname <url>
##
## Get the hostname of the url
## NOTE!: I dunno what this should do for svn...
##
#---------------------------------------------------------------------
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 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
##
#---------------------------------------------------------------------
