#
# 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 -pdLR "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/ || cleanup_and_exit 1 "source copy failed"
	else
	    cp -plR "$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() {
    :
}

productcompose_query_recipe() {
    perl -I$BUILD_DIR -MBuild::ProductCompose -e Build::ProductCompose::show "$BUILD_ROOT/$TOPDIR/SOURCES/$RECIPEFILE" "$1" "${BUILD_ARCH%%:*}" "$BUILD_FLAVOR"
}

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

    local milestone=$(productcompose_query_recipe milestone)
    local baseiso=$(productcompose_query_recipe baseiso)
    extra_args=

    while read kv ; do
        case "$kv" in
            [a-zA-Z0-9_]*)
                extra_args="$extra_args --build-option=$kv"
                ;;
        esac
    done < <( queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" --decode buildflags+ productcompose-buildoption )

    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
    if test -n "$baseiso"; then
        BUILD_USER=root
    fi

    local checksums
    if test -f "$BUILD_ROOT/$TOPDIR/SOURCES/repos/.createrepo_checksums" ; then
	checksums="CREATEREPO_CHECKSUMS=$TOPDIR/SOURCES/repos/.createrepo_checksums"
    fi

    chroot "$BUILD_ROOT" su -c "$checksums /usr/bin/product-composer build $extra_args -v --clean $TOPDIR/SOURCES/$RECIPEFILE $TOPDIR/PRODUCT" - $BUILD_USER < /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() {
    :
}
