#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for grabbing cvs urls.
##
##=head1 DESCRIPTION
##
## This file contains functions for I<downloading> files through cvs.
##
## In order for cvs urls to be downloaded, the I<cvs> spell must have been
## cast. This script first determines if cvs has been installed before 
## attempting to download a cvs url.
##
##=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:anoncvs@subversions.gnu.org:/cvsroot/sorcerer:sorcery
##
## If we want the 0.1.4 release instead (i.e., those files tagged with
## release-0_1_4, we would use the following url:
##
##      cvs://:pserver:anoncvs@subversions.gnu.org:/cvsroot/sorcerer:sorcery:release-0_1_4
##
## 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 IMPLEMENTATION NOTE
##
## Downloading is supported but CVS url verification is not
## currently supported.
##
##=head1 COPYRIGHT
##
## Copyright 2002 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------


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


#---------------------------------------------------------------------
##=item url_file_download <url>
## 
## Fetch the specified cvs url.
##
#---------------------------------------------------------------------
url_cvs_download() { 
  if ! spell_installed cvs; then
    return 1;
  fi

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

  if [ -z "$CVS_ROOT" -o -z "$CVS_MODULE" ]; then
    return 1;
  fi

  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 cvs update...${DEFAULT_COLOR}"
    if cd $S_FILE ; then
        cvs $CVS_COMPRESSION -q update -dP -r $CVS_TAG
        cd ..
	fi
  else
    message "${MESSAGE_COLOR}Running cvs checkout...${DEFAULT_COLOR}"
	cvs $CVS_COMPRESSION -q -d$CVS_ROOT checkout -r $CVS_TAG -d $S_FILE $CVS_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_cvs_verify <url>
## 
## Verifies the specified cvs url exists.  Returns true for now until
## I get around to implementing it.
##
#---------------------------------------------------------------------u
url_cvs_verify() {
  true
}


#---------------------------------------------------------------------
##=item url_cvs_set_options <option>
##
## Set options for url handling
##
#---------------------------------------------------------------------
url_cvs_set_options() {
  eval "$1=1"
}

#---------------------------------------------------------------------
##=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
##
#---------------------------------------------------------------------

