#!/bin/sh
##
#  Korinf project
#
#  Check for package requires is present in distro
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2012
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2012
#
#  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/>.
##

kormod filelist log converts/common
load_mod rpm


# FIXME: move to common funcs
# args: dir mask
expand_mask_in_dir()
{
	local tdir=$1
	shift
	local i
	for i in $@ ; do
		echo $tdir/$i
	done
}


# args: dist file reqs
print_check_reqs_item()
{
	local dist=$1
	local fname=$(basename $2)
	shift 2
	local reqs="$*"
	reqs="$(quote_pkgname $reqs)"
	if [ "$CHECKREQUIRES" = "-R" ] ; then
		printf "* %s: %s" "$fname" "$(echo $reqs)"
		[ -z "$reqs" ] && skipped && echo && continue
		echo
		run_in_chroot $dist "epm --verbose --no-stdin --skip-installed simulate $reqs"
	else
		printf "  * %60s: " "$fname"
		[ -z "$reqs" ] && skipped && echo && continue
		run_in_chroot $dist "epm --verbose --no-stdin --skip-installed simulate $reqs" >/dev/null 2>&1
	fi
	print_status $?
}


# check package existing by REBUILDLIST
# used: TARGETPATH REBUILDLIST
check_package_requires()
{
	local BUILDNAME=$1
	local reqs
	local rc=0

	# TODO: can we for deb or rpm?
	# it have no place before done?
	echo "Note: we do not use info from built packages, only check if file is exists"

	for dist in $(get_distro_list $REBUILDLIST) ; do
		DESTDIR="$TARGETPATH/$dist"
		test -L "$DESTDIR" && continue

		# fills $BUILDARCH, $DISTRNAME, $DISTRVERSION
		parse_dist_name $dist
		[ -z "$DISTRNAME" ] && fatal "Empty DISTRNAME for $dist"

		# hack to skip ALT checking
		[ "$DISTRNAME" = "ALTLinux" ] && continue

		####### Create req file for binary packages #########

		# fills $PKGFORMAT, $PKGVENDOR, $RPMVENDOR
		set_target_var
		prepare_filelist
		[ "$CHECKREQUIRES" = "-R" ] && echo "====================="
		printf "%-20s\n" "$BUILDARCH/$DISTRNAME/$DISTRVERSION"

		mount_linux $dist >/dev/null || continue

		# Test for build requirements
		local pkgnames="$(get_target_package_names)"
		# first print about source package
		reqs="$($EPMCMD req $BUILDSRPM | grep -v "[<=>]" | grep -v "^rpmlib")"
		echo "Before convert $(basename $BUILDSRPM): $(estrlist strip_spaces $reqs)"
		reqs="$(convert_pkgreqs_to_target $reqs)"
		reqs="$(estrlist exclude "$pkgnames" "$reqs")"
		print_check_reqs_item $dist $BUILDSRPM $reqs

		# Test for install requirements
		for pkgname in $pkgnames ; do
			reqs=$(get_target_deplist $pkgname) || rc=$?
			reqs=$(estrlist exclude "$pkgnames" "$reqs")
			print_check_reqs_item $dist $pkgname $reqs
		done

		end_umount $dist > /dev/null
	done
	return $rc
}

# Test for install requirements (virtually, check only converted requirements)
test_package_requirements()
{
	local pkgname reqs pkgnames
	# use real built files for get pkgnames
	pkgnames="$(get_target_package_names)"

	for pkgname in $pkgnames ; do
		rhas "$pkgname" "-debugsource" && continue
		rhas "$pkgname" "-debuginfo" && continue
		rhas "$pkgname" "-debug-" && continue
		reqs=$(get_target_deplist $pkgname) || return 1
		reqs=$(estrlist exclude "$pkgnames" "$reqs")
		reqs="$(quote_pkgname $reqs)"
		run_in_chroot $dist "epm --no-stdin --skip-installed simulate $reqs"
		local res="$?"
		[ "$res" != "0" ] && [ "$res" != "22" ] && return $res
	done
	return 0
}

# run before packages are copied, so can't use files from DESTDIR
# EXPMAINFILES, EXPEXTRAFILES contains paths to the built files
test_package_files_requirements()
{
	# TODO
	return 22
	[ -n "$INITIALBOOTSTRAP" ] && return

	# FIXME: epm requires can't return correct requires list
	run_in_chroot $dist "cd $INTBUILT && epm --no-stdin --skip-installed simulate \$(epm requires ${EXPMAINFILES} ${EXPEXTRAFILES})"
	return

	# unworked way:
	local reqs pkgfile pkgname
	#pkgnames=$(get_pkgname_from_filename $EXPMAINFILES $EXPEXTRAFILES)
	# use real built files for get pkgnames?
	# didn't expanded!
	pkgnames=$(get_target_package_names)

	for pkgfile in $EXPMAINFILES $EXPEXTRAFILES; do
		reqs=$(epm requires $pkgfile)
		reqs=$(estrlist exclude "$pkgnames" "$reqs")
		reqs="$(quote_pkgname $reqs)"
		run_in_chroot $dist "epm --no-stdin --skip-installed simulate $reqs"
		local res="$?"
		[ "$res" != "0" ] && [ "$res" != "22" ] && return $res
	done
	return 0

	assert_var BUILDNAME INTBUILT EXPMAINFILES EXPEXTRAFILES

}
