#!/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
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
}

# get package names extracted from package mask
# internal
get_target_package_names()
{
	local j
	for j in $EXPMAINFILES $EXPEXTRAFILES ; do
		get_pkgname_from_filename $j
	done
}

# args: dist file reqs
print_check_reqs_item()
{
	local dist=$1
	local fname=$(basename $2)
	shift 2
	local reqs=$@

	if [ "$CHECKREQUIRES" = "-R" ] ; then
		printf "* %s: %s" "$fname" "$(echo $reqs)"
		[ -z "$reqs" ] && skipped && echo && continue
		echo
		run_in_chroot "epm --verbose --skip-installed simulate \"$reqs\""
	else
		printf "  * %60s: " "$fname"
		[ -z "$reqs" ] && skipped && echo && continue
		run_in_chroot "epm --verbose --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

	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

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

		mount_linux $dist >/dev/null || continue

		local pkgnames="$(get_target_package_names)"
		# first print about source package
		reqs=$($ETERBUILDBIN/rpmreqs -p $BUILDSRPM)
		reqs=$(convert_pkgreqs_to_target $reqs)
		reqs=$(do_exclude_list "$pkgnames" "$reqs")
		print_check_reqs_item $dist $BUILDSRPM $reqs
		
		for pkgname in $pkgnames ; do
		# FIXME: use pkgnames instead expand_mask_in_dir?
		#for j in $(expand_mask_in_dir $DESTDIR $EXPMAINFILES) $(expand_mask_in_dir $DESTDIR/extra $EXPEXTRAFILES) ; do
		#	[ -r "$j" ] || continue
		#	pkgname=$(get_pkgname_from_filename $j)
			reqs=$(get_target_deplist $pkgname)
			reqs=$(do_exclude_list "$pkgnames" "$reqs")
			print_check_reqs_item $dist $pkgname $reqs
		done

		end_umount > /dev/null
	done
}

# run before packages are copied, so can't use real files
# used DESTDIR, dist, EXPMAINFILES, EXPEXTRAFILES
test_package_requirements()
{
	local pkgname reqs
	local pkgnames="$(get_target_package_names)"
	for pkgname in $pkgnames ; do
		reqs=$(get_target_deplist $pkgname)
		reqs=$(do_exclude_list "$pkgnames" "$reqs")
		echo "Try install '$reqs' for $pkgname"
		[ -z "$reqs" ] && echo " empty list, skipped" && continue
		run_in_chroot "epm --skip-installed simulate \"$reqs\"" || return
	done
}
