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


# Internal: printout args with add *.$PKGFORMAT
expand_filelist()
{
        [ -n"$PKGFORMAT" ] || fatal "PKGFORMAT is empty"
        # FIXME: bash related
        declare -a ar
        ar=( $@ )
        for i in `seq 0 $(( ${#ar[@]} - 1))`; do
                echo -n "${ar[i]}*.${PKGFORMAT} "
        done
}

prepare_filelist()
{
	# Use default value for simple backward compatibility
	if [ -z "$MAINFILESLIST$EXTRAFILESLIST" ] ; then
		MAINFILESLIST="${BUILDNAME}"
	fi

	# for these systems will pack all built packages in one big package
	if [ "$PKGVENDOR" = "freebsd" ] || [ "$PKGVENDOR" = "archlinux" ] || [ "$PKGVENDOR" = "gentoo" ] || [ "$PKGVENDOR" = "solaris" ] || [ "$PKGVENDOR" = "windows" ] ; then
		MAINFILESLIST="${BUILDNAME}"
		EXTRAFILESLIST=
	fi

	# expand file masks by PKGFORMAT
	EXPMAINFILES=$(expand_filelist $MAINFILESLIST)
	EXPEXTRAFILES=$(expand_filelist $EXTRAFILESLIST)

	# expand file masks by rpm
	EXPRPMMAINFILES=$(PKGFORMAT=rpm expand_filelist $MAINFILESLIST)
	EXPRPMEXTRAFILES=$(PKGFORMAT=rpm expand_filelist $EXTRAFILESLIST)

	# do lowercase for deb packages only
	if [ "$PKGFORMAT" = "deb" ] ; then
	    EXPMAINFILES=$(echo $EXPMAINFILES | filter_deb_pkgnames)
	    EXPEXTRAFILES=$(echo $EXPEXTRAFILES | filter_deb_pkgnames)
	fi

	[ -n "$ADEBUG" ] && echo "EXPMAINFILES: $EXPMAINFILES EXPEXTRAFILES: $EXPEXTRAFILES"
	[ -z "$EXPMAINFILES$EXPEXTRAFILES" ] && fatal "Logical error with EXPMAINFILES EXPEXTRAFILES"

	true
}

# fill MAILFILELIST and run prepare_filelist
fill_filelist()
{
	assert_var BUILDNAME BUILTRPM TARGETSRPM
	# TODO: use real package name list
	# HACK: remove unneeded debug packages
	if [ "$ALLOWPUBLICDEBUG" != "1" ] ; then
		rm -vf $BUILTRPM/*${BUILDNAME}-*debuginfo* $BUILTRPM/*${BUILDNAME}-*debug-*
	fi

	# HACK: the follow code copied from build/hasher
	# search for binary packages
	BUILTBINPKGLIST=$($EPMCMD print binpkgfilelist $BUILTRPM $(basename $TARGETSRPM))
	echo "For $TARGETSRPM follow packages is found:"
	echo "$BUILTBINPKGLIST"

	# hack: replace file list with real built packages filenames
	if [ -z "$MAINFILES$EXTRAFILES" ] ; then
		if [ -n "$BUILTBINPKGLIST" ] ; then
			MAINFILESLIST=$(rpm -qp $BUILTBINPKGLIST --queryformat="%{NAME} ")
			echo "Real MAINFILESLIST: $MAINFILESLIST"
		fi
	fi

	# Expand MAINFILELIST, EXTRAFILELIST
	prepare_filelist
}
