#!/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/>.
##

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

}

# Obsoleted due rpm-dir (since p5?)
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
	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

	# remove fatal flag
	rm -fv $DESTDIR/log/$BUILDNAME.autobuild.failed
	rm -fv $DESTDIR/log/$BUILDNAME.build.failed

	true
}

copying_sources()
{
	# Copying sources if possible
	if [ "$ALLOWPUBLICDEBUG" = "1" ] ; then
		mkdir -p "$SOURCEPATH/$dist/"
		rm -f "$SOURCEPATH/$dist/$BUILDNAME-[0-9]*.src.rpm" 2>/dev/null
		mv -fv "$TARGETSRPM" "$SOURCEPATH/$dist/$(basename "$TARGETSRPM")"
	else
		rm -fv "$TARGETSRPM"
	fi
}

copying_packages()
{
	local RC=0
	local i

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

	# Check if first package in the list is built
	for i in $EXPMAINFILES ; do
		[ -e $i ] && break
		RC=1
		warning "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/
				mv -f $i $DESTDIR/extra/ || warning "Cannot move debug package $i to extra"
			fi
		done
	else
		# Remove debug packages
		rm -f *${BUILDNAME}-*debuginfo* *${BUILDNAME}-*debug-*
	fi

	if [ -n "$MAINFILESLIST" ] ; then
		mkdir_dest $DESTDIR/
		echo "Copying built packages to $DESTDIR..."
		for i in $EXPMAINFILES ; do
			if [ -e $i ] ; then
				cp -v $i $DESTDIR/ || { RC=1 ; warning "Cannot copy new packages $EXPMAINFILES" ; }
			else
				warning "Cannot copy new package $i"
			fi
		done
		make_md5sum $DESTDIR/
	fi

	if [ -n "$EXTRAFILESLIST" ] ; then
		mkdir_dest $DESTDIR/extra
		echo "Copying built packages to $DESTDIR/extra..."
		for i in $EXPEXTRAFILES ; do
			if [ -e $i ] ; then
				cp -v $i $DESTDIR/extra/ || { RC=1 ; warning "Cannot copy extra packages $EXPEXTRAFILES" ; }
			else
				warning "Cannot copy extra package $i"
			fi
		done
		make_md5sum $DESTDIR/extra/
	fi

	# Return OK if one of files is exists
	for i in $EXPMAINFILES $EXPEXTRAFILES ; do
		test -r $i && RC=0
	done

	popd >/dev/null

	# Prepare binary repository
	case $dist_name in
		"ALTLinux")
			gen_base_apt_rpm
			gen_base_apt_rpm /extra
			;;
		*)
			;;
	esac

	mark_build_done

	copying_sources

	return $RC
}
