#!/bin/bash
##
#  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()
{
	local dir
	for dir in "$DESTDIR" "$DESTDIR/extra" ; do
		[ -d "$dir" ] || continue
		echo "Removing old '$MAINFILESLIST $EXTRAFILESLIST' by mask '$EXPMAINFILES $EXPEXTRAFILES' from $dir..."
		pushd $dir >/dev/null || return 1
		rm -fv $EXPMAINFILES
		rm -fv $EXPEXTRAFILES
		popd >/dev/null
	done
}

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
		cp -v $i $destdir/ || { error "Cannot copy new packages $expfiles" ; return 1; }
		chmod --verbose 0664 "$destdir/$i"
	done
	# use improved way
	#make_md5sum $destdir/
	true
}

copying_sources()
{
	assert_var dist
	# We need generated src.rpm in any case (only on the private ftp)
	if [ -r "$TARGETSRPM" ] && [ "$ALLOWPUBLICDEBUG" = "1" ] ; then
		assert_var BUILDNAME SOURCEPATH
		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=$(estrlist reg_exclude ".*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")
			kormod repos/apt-rpm
			gen_base_apt_rpm
			gen_base_apt_rpm /extra
			;;
		*)
			;;
	esac

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

	return $RC
}
