#!/bin/bash
#---------------------------------------------------------------------
##
##=head1 COPYRIGHT
##
## Copyright 2004 by the Source Mage Team
##
##=head1 FUNCTIONS
##
##=over 4
##
#---------------------------------------------------------------------


#---------------------------------------------------------------------
##=item dl_get_svn <url>
##
## Fetch the specified svn url.
##
## This handler only supports tree downloads.
##
#---------------------------------------------------------------------
dl_svn_get() {
  dl_command_check svn || return 254

  local target=$1
  local url_list=$2
  local hints=$3
  local dl_target=$4
  local dl_type=$5
  local url rc=0

  [[ $target ]] &&
  dl_connect || return 255

  for url in $url_list; do
    local URL SVN_ROOT SVN_MODULE SVN_TAG
    url_crack "$url" "$hints"

    local svn_args="--non-interactive "
    if list_find "$hints" "parse_username" ; then
      local USER_PASS=$(echo $SVN_ROOT|sed 's#.*//\(.*\)@.*#\1#')
      local SVN_ROOT=$(echo $SVN_ROOT|sed 's#//\(.*\)@#//#')
      local USER=${USER_PASS%:*};
      local PASS=${USER_PASS#*:};
      svn_args="$svn_args --username $USER --password $PASS"
    fi

    if list_find "$hints" no-check-certificate && [[ $(url_get_prefix "$url") == svn_https ]]; then
      svn_args="$svn_args --trust-server-cert"
    fi

    local svn_format=$(head -1 $target/.svn/entries)
    message "${MESSAGE_COLOR}Subversion working copy format: $svn_format${DEFAULT_COLOR}"
    if (( ${svn_format:-100} <= 10 )); then
      message "${MESSAGE_COLOR}Running svn upgrade...${DEFAULT_COLOR}"
      echo svn upgrade $svn_args $target
      svn upgrade $svn_args $target
    fi

    if test -d $target; then
      message "${MESSAGE_COLOR}Running svn update...${DEFAULT_COLOR}"
      echo svn update $svn_args -r $SVN_TAG $target
      svn update $svn_args -r $SVN_TAG $target
      rc=$?
      eval "$dl_target=\"$target\""
    else
      message "${MESSAGE_COLOR}Running svn checkout...${DEFAULT_COLOR}"
      echo svn checkout $svn_args -r $SVN_TAG $SVN_ROOT $SVN_MODULE
      svn checkout $svn_args -r $SVN_TAG $SVN_ROOT $SVN_MODULE
      rc=$?
      eval "$dl_target=\"$SVN_MODULE\""
    fi
    [[ $rc == 0 ]] && break
  done
  dl_disconnect

  eval "$dl_type=\"tree\""
  return $rc
}

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