#!/bin/bash
##
#  Korinf project
#
#  Copying functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-2009, 2013, 2015
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2013, 2015
#
#  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/>.
##

load_mod repl
kormod build/rpm log


# Create dependence file for ALT Linux Sisyphus package
make_alt_depends_list()
{
    local pkg="$1"
    [ -n "$pkg" ] || return
    echo "Dependencies list for $pkg:"
    local TFILE=$(make_temp_file)
    # TODO: in any way, rpmreqs run apt-get install directly!
    BUILDARCH=i586 $ETERBUILDBIN/rpmreqs -p $pkg > $TFILE || return
    set_destlogdir
    local DEPFILE=$DESTLOGDIR/$(get_pkgname_from_filename $pkg | filter_deb_pkgnames).rpm.depends
    rm -f $DEPFILE # for keep correct owner
    cat $TFILE | tee $DEPFILE
    rm -f $TFILE
}

# used $dist (in set_desklogdir?)
write_altdeps_by_list()
{
	local i
	local destdir="$1"
	local expfiles="$2"
	[ -n "$expfiles" ] || return 0

	# TODO: need we pushd here?
	pushd "$destdir" >/dev/null || return 0
	# trick with expand mask here
	for i in $expfiles ; do
		[ -s "$i" ] || continue
		make_alt_depends_list "$i" || { warning "Some depends error with $i" ; return 1 ; }
	done

	popd >/dev/null
}

write_altdeps()
{
	assert_var DESTDIR

	write_altdeps_by_list "$DESTDIR" "$EXPMAINFILES" || return
	write_altdeps_by_list "$DESTDIR/extra" "$EXPEXTRAFILES" || return
}
