################################################################################
#
# svn.cygclass - functions for building packages from SVN checkouts
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2015 Yaakov Selkowitz
# Provided by the Cygwin Ports project <http://sourceware.org/cygwinports/>
#
# cygport 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 3 of the License, or
# (at your option) any later version.
#
# cygport 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 cygport.  If not, see <http://www.gnu.org/licenses/>.
#
################################################################################

#****h* Cygclasses/svn.cygclass
#  SYNOPSIS
#  SVN_URI="..."
#  [SVN_BRANCH="..."]
#  [SVN_DATE="..."]
#  [SVN_REV="..."]
#  inherit svn
#  DESCRIPTION
#  Subversion is a centralized version control system, designed to be mostly
#  compatible with CVS while adding some new features and avoiding some of
#  CVS' limitations.  It is still widely used.
#
#  This cygclass creates source tarballs from Subversion checkouts.
#  NOTE
#  This cygclass sets a special SRC_URI for the tarball it creates.  If
#  additional sources are required, be sure to _add_ to SRC_URI rather than
#  outright setting it.
#  REQUIRES
#  subversion
#****

#****v* svn.cygclass/SVN_URI
#  DESCRIPTION
#  Address of the Subversion repository.  This must be set before inherit()ing
#  svn.cygclass.
#  NOTE
#  While a Subversion repository URI can include the desired branch or tag, for
#  these purposes it is usually best to use the repository top-level (which
#  customarily includes the folders branch/, tags/, and trunk/) as the SVN_URI,
#  and the desired branch or tag subdirectory as the SVN_BRANCH.
#****

if ! defined SVN_URI
then
	error "SVN_URI must be defined"
fi

SVN_URI=${SVN_URI%/}

#****v* svn.cygclass/SVN_BRANCH
#  DESCRIPTION
#  The branch or tag subdirectory to be downloaded relative to SVN_URI, such
#  as "branches/some_feature" or "tags/X.Y.Z".
#
#  This must be set before inherit()ing svn.cygclass.  If undefined, the "trunk"
#  branch will be used.
#****
#****v* svn.cygclass/SVN_REV
#  DESCRIPTION
#  The revision number to be downloaded.  If undefined, the latest revision
#  from the given SVN_BRANCH will be used.
#****
#****v* svn.cygclass/SVN_DATE
#  DESCRIPTION
#  The revision date to be downloaded.  Preferred form is YYYY-MM-DD [HH:MM],
#  where the time is optional; the hyphens in the date can be omitted.
#  NOTE
#  If a date is specified without a time, then the time 00:00 is implied,
#  which will exclude all revisions made on the given date.  Use the
#  following day in SVN_DATE instead.
#****

if defined SVN_BRANCH
then
	svn_branch="${SVN_BRANCH}"
elif defined SVN_TAG
then
	svn_branch="tags/${SVN_TAG}"
else
	svn_branch="trunk"
fi

case ${SVN_URI} in
	*/${svn_branch})
		svn_uri="${SVN_URI}"
		SVN_URI="${SVN_URI%/${svn_branch}}"
		;;
	*/${svn_branch}/*)
		svn_uri="${SVN_URI}" ;;
	*)
		svn_uri="${SVN_URI}/${svn_branch}" ;;
esac

SVN_MODULE=${SVN_MODULE:-${SVN_URI##*/}}
svn_tarball="${SVN_MODULE}-${PV}.tar.bz2"

SRC_URI="${svn_tarball} "
SRC_DIR="${SVN_MODULE}"

svn_fetch() {
	local svn_rev

	check_prog_req svn subversion

	if defined SVN_REV
	then
		svn_rev="-r ${SVN_REV}"
	elif defined SVN_DATE
	then
		svn_rev="-r {${SVN_DATE}}"
	fi

	# T likely doesn't exist at this point, so create it first
	mkdir -p ${T}
	cd ${T}
	verbose svn export ${svn_uri} ${svn_rev} ${SVN_MODULE}

	tar ${_tar_bz2_flag}cf ${top}/${svn_tarball} --exclude-vcs ${SVN_MODULE}
}

readonly -f svn_fetch
