# Copyright 1999-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
#
# automatically generated by g-sorcery
# please do not edit this file
#
# Original Author: Jauhien Piatlicki <piatlicki@gmail.com>
# Purpose: base routines for g-sorcery backends' eclasses
#
# Bugs to piatlicki@gmail.com
#
# @ECLASS: g-sorcery.eclass
#
# @ECLASS-VARIABLE: REPO_URI
# @DESCRIPTION: address of a repository with sources
#
# @ECLASS-VARIABLE: DIGEST_SOURCES
# @DESCRIPTION: whether manifest for sources exists
#
# @ECLASS-VARIABLE: SOURCEFILE
# @DESCRIPTION: source file name
#
# @ECLASS-VARIABLE: GSORCERY_STORE_DIR
# @DESCRIPTION: store location for downloaded sources
GSORCERY_STORE_DIR="${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}"
#
# @ECLASS-VARIABLE: GSORCERY_FETCH_CMD
# @DESCRIPTION: fetch command
GSORCERY_FETCH_CMD="wget"

EXPORT_FUNCTIONS src_unpack

# The live property will disable Portage's network-sandbox in src_unpack()
PROPERTIES+=" live"

g-sorcery_fetch() {
	addwrite "${GSORCERY_STORE_DIR}"
	pushd "${GSORCERY_STORE_DIR}" >/dev/null || die "can't chdir to ${GSORCERY_STORE_DIR}"
	if [[ ! -f "${SOURCEFILE}" ]]; then
		$GSORCERY_FETCH_CMD ${REPO_URI}${SOURCEFILE} || die
	fi
	popd >/dev/null || die
}

g-sorcery_src_unpack() {
	if [[ x${DIGEST_SOURCES} = x ]]; then
		g-sorcery_fetch
	fi

	cp ${GSORCERY_STORE_DIR}/${SOURCEFILE} . || die
	unpack ./${SOURCEFILE}
}
