#
# KIWI specific functions. Handle with care.
#
################################################################
#
# Copyright (c) 2023 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################


recipe_setup_productcompose() {
    TOPDIR=/usr/src/packages
    test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
    mkdir -p "$BUILD_ROOT$TOPDIR"
    mkdir -p "$BUILD_ROOT$TOPDIR/OTHER"
    mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES"
    mkdir -p "$BUILD_ROOT$TOPDIR/PRODUCT"
    # compat, older build versions did not clean TOPDIR ...
    mkdir -p "$BUILD_ROOT$TOPDIR/BUILD"
    mkdir -p "$BUILD_ROOT$TOPDIR/RPMS"
    mkdir -p "$BUILD_ROOT$TOPDIR/SRPMS"

    if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir; then
	mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
    else
	if test -z "$LINKSOURCES" ; then
	    cp -dLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/ || cleanup_and_exit 1 "source copy failed"
	else
	    cp -lR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/ || cleanup_and_exit 1 "source copy failed"
	fi
    fi
    chown -hR "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
}

recipe_prepare_productcompose() {
    :
}

recipe_build_productcompose() {
    echo "running product composer..."

    local milestone=$(kiwi_query_recipe milestone)
    extra_args=
    if test -n "$RELEASE" ; then
	extra_args=" $extra_args --release $RELEASE"
    fi
    if test -n "$BUILD_FLAVOR" ; then
	extra_args=" $extra_args --flavor $BUILD_FLAVOR"
    fi
    if test -n "$DISTURL" ; then
	extra_args=" $extra_args --disturl $DISTURL"
    fi
    if test -n "$BUILD_VCSURL" ; then
	extra_args=" $extra_args --vcs $BUILD_VCSURL"
    fi
    chroot "$BUILD_ROOT" su -c "/usr/bin/product-composer build $extra_args -v --clean $TOPDIR/SOURCES/$RECIPEFILE $TOPDIR/PRODUCT" - abuild < /dev/null && BUILD_SUCCEEDED=true
    pushd "$BUILD_ROOT/$TOPDIR/PRODUCT"
    for i in * ; do
	test -e "$i" || continue
	case $i in
	    *.iso)
		test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/PRODUCT/${i%.iso}.milestone
		;;
	    *.report)
		test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/PRODUCT/${i%.report}.milestone
		;;
	    *)
		test -d "$i" || continue
		test -n "$milestone" && echo "$milestone" > $BUILD_ROOT/$TOPDIR/PRODUCT/${i}.milestone
		;;
        esac
    done
    popd
}

recipe_resultdirs_productcompose() {
    echo PRODUCT
}

recipe_cleanup_productcompose() {
    :
}
