#!/bin/sh
##
#  Korinf project
#
#  Gentoo build related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 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/>.
##

# BUILDROOT - root of current system
# BUILDERHOME - absolute path to user dir in current system

# TODO: user DISTRVERSION
load_mod rpm


gen_ebuild()
{
	local RET
        echo "Copying to $BUILDROOT/tmp"
        cp -f  $KORINFDIR/korinf/remote-scripts/remote-gentoo.sh $BUILDROOT/tmp || { warning "Cannot copy script" ; return 1 ; }
        $SUDO chroot $BUILDROOT su -l -c "sh -x /tmp/remote-gentoo.sh generate \"$PACKAGE\" $DESTURL $(basename $TARGETSRPM) $EXPRPMMAINFILES" korinfer
        RET=$?
        if [ $RET != 0 ] ; then
                warning "Can't build"
                return 1
        fi
        true
}

# FIXME: is it used? we already have convertor
convert_gentoo()
{
#	fatal "Can't use this code"
	echo "Using alien now"
	for i in *${BUILDNAME}*.rpm ; do
	    run_alien --to-tgz $i || { warning "alien problem with tbz2"; RES=11 ; }
	    local TARARCH=`querypackage "$i" ARCH`
	    local RELPKG=$PACKAGERELEASE$PKGVENDOR.$TARARCH
	    for j in *${BUILDNAME}*.tgz ; do
		local TARBNAME=`basename $j .tgz`
		cat "$j" | gunzip -c | bzip -c > "$TARBNAME-$RELPKG.tar.bz2" || { warning "can't repack files" ; RES=11 ; }
		rm -f "$j"
	    done
	done
}

copying_gentoo2007()
{
	clean_copying_destination

	echo "Copying to ftp"
        cp -f $BUILDROOT/usr/local/portage/packages/All/*$BUILDNAME*.tar.gz $DESTDIR || { warning "Cannot copy packages" ; return 1; }
	echo "Done with copying"
        chmod g+rw -R $DESTDIR/* || true
}

clean_gentoo2007()
{
        true
}
