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

mkdir_dest()
{
	mkdir -p "$1" && return
	sleep 100  # Hack due NFS troubles
	mkdir -p "$1" || fatal "Can't create $1"
}

# Cleanup target dir, used for all systems
clean_copying_destination()
{
	# Remove old packages from main dir
	if [ -n "$MAINFILESLIST" ] && [ -d "$DESTDIR" ] ; then
		echo "Removing old '$MAINFILESLIST' by mask '$EXPMAINFILES' from $DESTDIR..."
		pushd $DESTDIR >/dev/null || return 1
		rm -fv $EXPMAINFILES
		popd >/dev/null
	fi

	# Remove old packages from extra dir
	if [ -n "$EXTRAFILESLIST" ] && [ -d "$DESTDIR/extra" ] ; then
		echo "Removing old '$EXTRAFILESLIST' by mask '$EXPEXTRAFILES' from $DESTDIR/extra..."
		pushd $DESTDIR/extra >/dev/null || return 1
		rm -fv $EXPEXTRAFILES
		popd >/dev/null
	fi

}

# 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)
    $ETERBUILDBIN/rpmreqs -p $pkg > $TFILE || fatal "error with rpmreqs"
    set_destlogdir
    cat $TFILE | tee $DESTLOGDIR/$(get_pkgname_from_filename $pkg).rpm.depends
    rm -f $TFILE
}

# Obsoleted due rpm-dir (since p5?) - but rpm-dir only work locally
gen_base_apt_rpm()
{
	local SUBDIR=$1
	[ -d "$DESTDIR$SUBDIR" ] || return 1
	if [ ! -d "$DESTDIR$SUBDIR/RPMS.main" ] ; then
		ln -s ./ $DESTDIR$SUBDIR/RPMS.main
	fi
	genbasedir --create --progress --topdir=$TARGETPATH $dist$SUBDIR main
	#chmod o+r $DIR/* -R
# FIXME: need to set ftp/http URL
	cat <<EOF > $DESTDIR$SUBDIR/base/README.korinf.txt
You can add this repo to APT sources.list:
rpm file:$TARGETPATH$SUBDIR $dist main
EOF
}

mark_build_done()
{
	# FIXME
	chmod ug+rw $DESTDIR/* -R 2>/dev/null
	chmod o+r $DESTDIR/* -R 2>/dev/null

	clear_build_status
}

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

	mkdir_dest $destdir/
	echo "Copying built packages $expfiles to $destdir..."
	for i in $expfiles ; do
		if [ ! -e $i ] ; then
			error "Cannot find package $i, skip copy"
			return 1
		fi

		# FIXME: need move to build/hasher
		if [ "$dist" = "ALTLinux/Sisyphus" ]; then
		    make_alt_depends_list "$i" || warning "Some depends error with $i"
		fi

		cp -v $i $destdir/ || { error "Cannot copy new packages $expfiles" ; return 1; }
	done
	make_md5sum $destdir/
	true
}

copying_sources()
{
	# We need generated src.rpm in any case (only on the private ftp)
	if [ -r "$TARGETSRPM" ] && [ "$ALLOWPUBLICDEBUG" = "1" ] ; then
		echo "Copying converted source package"
		mkdir -p "$SOURCEPATH/$dist/"
		rm -f "$SOURCEPATH/$dist/$BUILDNAME-[0-9]*.src.rpm" 2>/dev/null
		mv -fv "$TARGETSRPM" "$SOURCEPATH/$dist/"
	else
		echo "Skip conf. source copying"
		rm -fv "$TARGETSRPM"
	fi
}

copying_packages()
{
	local RC=0
	local i

	pushd $BUILTRPM >/dev/null || fatal "Can't cd to built rpm dir '$BUILTRPM'"
	echo "Copying $PKGFORMAT packages from $BUILTRPM"

	# Check if the first package in the list is built
	for i in $EXPMAINFILES ; do
		[ -e $i ] && break
		RC=1
		error "First package '$i' from MAINFILES list wasn't built"
		popd >/dev/null
		return $RC
	done

	clean_copying_destination

	# Debug packages can contains source files, copy only if allowed
	if [ "$ALLOWPUBLICDEBUG" = "1" ] ; then
		# FIXME: some other names?
		for i in *${BUILDNAME}-*debuginfo* *${BUILDNAME}-*debug-* ; do
			if [ -e $i ] ; then
				mkdir_dest $DESTDIR/extra/
				# FIXME: already done when copying, but not for extra
				# HACK: can be not *BUILDNAME*
				rm -vf $DESTDIR/extra/*${BUILDNAME}-*debuginfo*
				rm -vf $DESTDIR/extra/*${BUILDNAME}-*debug-*
				mv -f $i $DESTDIR/extra/ || warning "Cannot move debug package $i to extra"
				EXPMAINFILES=$(regexp_exclude_list ".*debuginfo.* .*debug.*" "$EXPMAINFILES")
			fi
		done
	else
		# HACK: can be not *BUILDNAME*
		echo "Debug packages is forbidden, Remove debug packages"
		rm -vf *${BUILDNAME}-*debuginfo* *${BUILDNAME}-*debug-*
	fi

	copying_packages_by_list "$DESTDIR" "$EXPMAINFILES" || RC=1
	copying_packages_by_list "$DESTDIR/extra" "$EXPEXTRAFILES" || RC=1

	# Return OK if all files are exists
	for i in $EXPMAINFILES $EXPEXTRAFILES ; do
		test -r $i || RC=1
	done

	popd >/dev/null

	# Prepare binary repository
	case $PKGVENDOR in
		"alt")
			gen_base_apt_rpm
			gen_base_apt_rpm /extra
			;;
		*)
			;;
	esac

	if [ "$RC" = 0 ] ; then
		copying_sources
		mark_build_done
	fi

	return $RC
}
