################################################################################
#
# hg.cygclass - functions for building packages from Mercurial checkouts
#
# Part of cygport - Cygwin packaging application
# Copyright (C) 2006-2016 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/hg.cygclass
#  SYNOPSIS
#  HG_URI="..."
#  [HG_REV="..."]
#  inherit hg
#  DESCRIPTION
#  Mercurial is a distributed version control system.  Originally developed
#  around the same time and for a similar reason as Git, it is used by a number
#  of projects, including Mozilla, OpenOffice.org, and OpenJDK.
#
#  This cygclass creates source tarballs from Mercurial 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
#  mercurial
#****

#****v* hg.cygclass/HG_URI
#  DESCRIPTION
#  Address of Mercurial repository from which to clone.  Must be defined before
#  inherit()ing hg.cygclass.
#****
if ! defined HG_URI
then
	error "HG_URI must be defined"
fi

HG_URI=${HG_URI%/}

HG_MODULE=${HG_MODULE:-${HG_URI##*/}}

hg_tarball="${HG_MODULE}-hg-${PV}.tar.bz2"

SRC_URI="${hg_tarball} "
SRC_DIR="${HG_MODULE}"

hg_fetch() {
	check_prog_req hg mercurial

#****v* hg.cygclass/HG_REV
#  DESCRIPTION
#  Specific revision to clone, either the changeset ID in the form of a SHA1
#  hash (a 40-digit hexadecimal) or an abbreviation thereof (the first 7
#  hexadecimal digits are usually sufficent), OR the sequential integer
#  revision number.  If undefined, the latest revision is cloned.
#****
	if defined HG_REV
	then
		hg_rev="-r ${HG_REV}"
	fi

	# T likely doesn't exist at this point, so create it first
	mkdir -p ${T}
	cd ${T}

	verbose hg clone ${hg_rev} ${HG_URI} ${HG_MODULE}

	tar ${_tar_bz2_flag}cf ${top}/${hg_tarball} --exclude-vcs ${HG_MODULE}
}

readonly -f hg_fetch
