#!/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!
    # hack against imperfect rpmreqs
    if [ "$TARGETARCH" = "i586" ] && hostname | grep -q office.etersoft.ru ; then
        ssh builder@builder32 $ETERBUILDBIN/rpmreqs -p $(pwd)/$pkg > $TFILE || return
    else
        $ETERBUILDBIN/rpmreqs -p $pkg > $TFILE || return
    fi
    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

	mkdir -p "$destdir" || return 0
	# do expfiles expandable in a curent dir
	pushd "$destdir" >/dev/null || return 0
	# trick with expand mask here
	for i in $expfiles ; do
		[ -s "$i" ] || continue
		rhas "$i" "-debug(-|info)" && continue
		make_alt_depends_list "$i" || { warning "Some depends error with $i" ; popd; 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
}
