#!/bin/sh
##
#  Korinf project
#
#  convert built packages in chrooted Linux system
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2013
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2013
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program 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 Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##


# Converts ALT Linux Sisyphus dependencies to target notation
# and print out result dependencies
# needs TARGETPATH, DISTRVENDOR, BUILDARCH, PKGFORMAT and so one
# call with packagename as arg
get_target_deplist()
{
	assert_var TARGETPATH DISTRVENDOR BUILDARCH PKGFORMAT
	local depfile=$TARGETPATH/ALTLinux/Sisyphus/log/$(echo $1 | filter_deb_pkgnames).rpm.depends
	if [ ! -r $depfile ]; then
		warning "Cannot locate '$depfile' file"
		return 1
	fi
	convert_pkgreqs_to_target $(cat $depfile)
}

# General local prepare for varios systems
prepare_convert()
{
	assert_var BUILTBINPKGLIST TARGETSRPM INTUSER
	# get requires
	local reqs
	for i in $BUILTBINPKGLIST ; do
		local depf=$(get_pkgname_from_filename $i)
		reqs="$reqs $(get_target_deplist $depf)"
	done
	PKGREQLIST="$reqs"

	# TODO: move to common place
	#get package fields
	PKGDESCR="$(querypackage $TARGETSRPM DESCRIPTION)"
	PKGCOMMENT="$(querypackage $TARGETSRPM SUMMARY)"
	PKGGROUP="$(querypackage $TARGETSRPM GROUP)"

	PKGURL="$(querypackage $TARGETSRPM URL)"
	#PKGARCH="$(querypackage $TARGETSRPM ARCH)"
	# FIXME: change to per package
	PKGARCH=$BUILDARCH
	PKGLICENSE="$(querypackage $TARGETSRPM LICENSE)"
	PKGPACKAGER="$(querypackage $TARGETSRPM PACKAGER)"

	# work dir in target system
	WRKDIR=/home/$INTUSER/tmp/work-$(basename $TARGETSRPM)
}
