#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 SYNOPSIS
## 
## Url handler functions for grabbing rsync urls.
##
##=head1 DESCRIPTION
##
## This file contains functions for I<downloading> files through rsync.
##
## In order for rsync urls to be downloaded, the I<rsync> spell must have been
## cast. This script first determines if rsync has been installed before 
## attempting to download a rsync url.
##
##=head1 RSYNC URL Format
##
##
##      rsync://SERVER::MODULE_NAME
##
## The above url will download the latest version of the specified
## module.
##
##=head1 EXAMPLES
##
## Suppose we want to download the latest version of the sorcery
## stable grimoire via rsync.  We'd use the following url:
##
##      rsync://codex.sourcemage.org::stable
##
##=head1 IMPLEMENTATION NOTE
##
## Downloading is supported but rsync url verification is not
## currently supported.
##
##=head1 COPYRIGHT
##
## Copyright 2003 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------


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


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

  local  URL=`url_rsync_strip_prefix "$1"`
  local  DEST=`echo $URL | cut -d':' -f 3-`

  [ -z "$FILE" ]  &&  FILE=`basename  $DEST`


  if  echo  $URL  |  grep  -q  sourcemage.org;  then

    local  WWW="sourcemage.org::"
    
    # check for which url to codex is to be used... based on rsync module given.
    case  $DEST  in
    
      test)       URL="${WWW}sourcemage/codex/test/"
                  DEST="$CODEX_ROOT/$DEST/"
                  ;;
      stable)     URL="${WWW}sourcemage/codex/stable/"
                  DEST="$CODEX_ROOT/$DEST/"
                  ;;
      games)      URL="${WWW}sourcemage/codex/games/"
                  DEST="$CODEX_ROOT/$DEST/"
                  ;;
      z-rejected) URL="${WWW}sourcemage/codex/z-rejected/"
                  DEST="$CODEX_ROOT/$DEST/"
                  ;;
      *)          message "${MESSAGE_COLOR}Not an available rsync package (test | stable | games | z-rejected), try again.${DEFAULT_COLOR}"
                  return 1
                  ;;
    esac
    
  else
    if echo $DEST | grep -q \/;  then
      DEST=\.
    fi
  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 rsync...${DEFAULT_COLOR}"
    if cd $S_FILE ; then
        rsync -rz --delete --stats $URL .
        cd ..
	fi
  else
    message "${MESSAGE_COLOR}Running rsync...${DEFAULT_COLOR}"
	rsync -rz --delete --stats $URL $DEST
  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
    FILE=$S_FILE.tar.bz2
  else
    false;
  fi
}


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


#---------------------------------------------------------------------
##=item url_rsync_set_options <option>
##
## Set options for url handling
##
#---------------------------------------------------------------------
url_rsync_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
##
#---------------------------------------------------------------------

