#
# debootstrap specific functions.
#
################################################################
#
# Copyright (c) 1995-2014 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_debootstrap() {
    recipe_setup_dsc "$@"
}

recipe_prepare_debootstrap() {
    recipe_prepare_dsc "$@"
}

recipe_build_debootstrap() {
    local arch=$(chroot $BUILD_ROOT su -c "dpkg-architecture -qDEB_BUILD_ARCH")
    local dist=$(chroot $BUILD_ROOT su -c "lsb_release --codename --short")
    local myroot=debootstraproot
    test -d $BUILD_ROOT/.build.binaries || cleanup_and_exit 1
    if test "$DO_INIT" = true -o ! -d "$BUILD_ROOT/.build.binaries/dists" ; then
	echo "creating repository for debootstrap..."
	createrepo_debian $BUILD_ROOT/.build.binaries ${arch} ${dist}
    fi
    FULL_PKG_LIST=
    for PKG in $BUILD_ROOT/.build.binaries/*.deb ; do
	PKG="${PKG##*/}"
	FULL_PKG_LIST="$FULL_PKG_LIST,${PKG%.deb}"
    done
    FULL_PKG_LIST="${FULL_PKG_LIST#,}"
    rm -rf "$BUILD_ROOT/$myroot"
    set -- chroot $BUILD_ROOT debootstrap --no-check-gpg --variant=buildd --arch="${arch}" --include="$FULL_PKG_LIST" "$dist" "$myroot" file:///.build.binaries
    echo "running debootstrap..."
    "$@"
    # adapt passwd
    if test $BUILD_USER = abuild ; then
	echo "abuild:x:${ABUILD_UID}:${ABUILD_GID}:Autobuild:/home/abuild:/bin/bash" >>$BUILD_ROOT/$myroot/etc/passwd
	echo 'abuild:*::' >>$BUILD_ROOT/$myroot/etc/gshadow
	echo "abuild:x:${ABUILD_GID}:" >>$BUILD_ROOT/$myroot/etc/group
	mkdir -p $BUILD_ROOT/$myroot/home/abuild
	chown "$ABUILD_UID:$ABUILD_GID" $BUILD_ROOT/$myroot/home/abuild
    fi
    # move topdir over
    mv "$BUILD_ROOT/$TOPDIR" "$BUILD_ROOT/$myroot/${TOPDIR%/*}"
    # do the build
    DSC_BUILD_OPTIONS=
    test -n "$BUILD_JOBS" && DSC_BUILD_OPTIONS="parallel=${BUILD_JOBS}"
    DSC_BUILD_CMD="$(queryconfig --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$CONFIG_DIR" substitute dsc:build_cmd)"
    test -z "$DSC_BUILD_CMD" && DSC_BUILD_CMD="dpkg-buildpackage -us -uc"
    if test -e $BUILD_ROOT/$myroot/$TOPDIR/SOURCES/build.script ; then
        echo "Sourcing build.script to build - it should normally run 'dpkg-buildpackage -us -uc'"
        DSC_BUILD_CMD="source $TOPDIR/SOURCES/build.script"
        chmod +x $BUILD_ROOT/$myroot/$TOPDIR/SOURCES/build.script
    fi
    chroot "$BUILD_ROOT/$myroot" su -c "export DEB_BUILD_OPTIONS=${DSC_BUILD_OPTIONS} ; cd $TOPDIR/BUILD && $DSC_BUILD_CMD" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
    # run lintian
    if test "$BUILD_SUCCEEDED" = true -a "$DO_CHECKS" != "false"; then
	DEB_CHANGESFILE=${RECIPEFILE%.dsc}_"$(chroot "$BUILD_ROOT/$myroot" su -c 'dpkg-architecture -qDEB_BUILD_ARCH')".changes
	chroot $BUILD_ROOT/$myroot su -c "which lintian > /dev/null && cd $TOPDIR && echo Running lintian && (set -x && lintian -i $DEB_SOURCEDIR/$DEB_DSCFILE)" - $BUILD_USER < /dev/null
    fi
    # move topdir back
    mv "$BUILD_ROOT/$myroot/$TOPDIR" "$BUILD_ROOT/${TOPDIR%/*}"
    for DEB in $BUILD_ROOT/$TOPDIR/*.deb ; do
        test -e "$DEB" && mv "$DEB" "$BUILD_ROOT/$TOPDIR/DEBS"
    done
    # link used sources over to DEB directory
    ln $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE $BUILD_ROOT/$TOPDIR/DEBS/
    while read f ; do
        ln $BUILD_ROOT/$DEB_SOURCEDIR/$f $BUILD_ROOT/$TOPDIR/DEBS/
    done < <(sed -ne '/^Files:/,$s/^ ................................ [0-9][0-9]* //p' < $BUILD_ROOT/$DEB_SOURCEDIR/$DEB_DSCFILE)
}

recipe_resultdirs_debootstrap() {
    echo DEBS
}

