#
# dsc 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_dsc() {
    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/DEBS"
    chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
    cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
    # FIX to work with baselibs_$PROJ etc
    if test -e "$MYSRCDIR/baselibs-deb.conf" ; then
	echo "dsc build and baselibs-deb.conf present: forcing --baselibs to true"
	CREATE_BASELIBS=true
    fi
}

recipe_prepare_dsc() {
    rm -rf "$BUILD_ROOT$TOPDIR/BUILD"
    mkdir -p "$BUILD_ROOT$TOPDIR/SOURCES.DEB"
    chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
    DEB_TRANSFORM=
    DEB_SOURCEDIR="$TOPDIR/SOURCES"
    DEB_DSCFILE="$RECIPEFILE"
    for f in $BUILD_ROOT$TOPDIR/SOURCES/debian.* ; do 
	test -f $f && DEB_TRANSFORM=true
    done
    # remove rpm macros (everything after "%")
    # they are not evaluated by the Debian build process
    DEB_RELEASE=`sed 's/%.*$//' <<< $RELEASE`
    OBS_DCH_RELEASE=""

    if test -n "$DEB_TRANSFORM" ; then 
	CHANGELOGARGS=
	test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
	echo "Found files matching debian.*, running debian transformer..."
        if [ "$RELEASE" ]; then
          echo "release: ($RELEASE), release (DEB) ($DEB_RELEASE)"
                 RELEASEARGS="--release $DEB_RELEASE"
        fi
        if ! debtransform $CHANGELOGARGS $RELEASEARGS $BUILD_ROOT$TOPDIR/SOURCES $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE $BUILD_ROOT$TOPDIR/SOURCES.DEB ; then
	    echo "debian transforming failed."
	    cleanup_and_exit 1
	fi
	DEB_SOURCEDIR=$TOPDIR/SOURCES.DEB
	for DEB_DSCFILE in $BUILD_ROOT/$DEB_SOURCEDIR/*.dsc ; do : ; done 
	DEB_DSCFILE="${DEB_DSCFILE##*/}"
    fi
    chroot $BUILD_ROOT su -c "dpkg-source -x $DEB_SOURCEDIR/$DEB_DSCFILE $TOPDIR/BUILD" - $BUILD_USER

    # Alternative to debtransform: apply OBS release number if tag OBS-DCH-RELEASE is set.
    if test -z "$DEB_TRANSFORM" && grep -Eq '^OBS-DCH-RELEASE: 1' $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE; then
        OBS_DCH_RELEASE="+$DEB_RELEASE"
        chroot $BUILD_ROOT su -c /bin/sh <<EOF
cd $TOPDIR/BUILD
[ ! -f debian/changelog ] && exit 0
# avoid devscripts dependency and mimic dch
PACKAGE=\$(dpkg-parsechangelog 2> /dev/null | grep -E '^Source:'  | awk '{ print \$NF }')
VERSION=\$(dpkg-parsechangelog 2> /dev/null | grep -E '^Version:' | awk '{ print \$NF }')
sed -i "s/\${PACKAGE} (\${VERSION})/\${PACKAGE} (\${VERSION}$OBS_DCH_RELEASE)/" debian/changelog
EOF
    fi

}

dsc_build() {
    local buildroot=$1

    DSC_BUILD_OPTIONS=
    if test -n "$BUILD_JOBS" ; then
	DSC_BUILD_OPTIONS="parallel=${BUILD_JOBS}"
    fi
    # Checks to see if a build script should be used
    # this allows the build environment to be manipulated
    # and alternate build commands can be used
    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 $buildroot/$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 $buildroot/$TOPDIR/SOURCES/build.script
    fi

    if test -n "$RUN_SHELL"; then
	chroot $buildroot su -
    else
	chroot $buildroot su -c "export DEB_BUILD_OPTIONS=${DSC_BUILD_OPTIONS} ; cd $TOPDIR/BUILD && $DSC_BUILD_CMD" - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
	if test "$BUILD_SUCCEEDED" = true -a "$DO_CHECKS" != "false" && ( chroot $buildroot su -c "which lintian > /dev/null" - $BUILD_USER < /dev/null ); then
	   DEB_CHANGESFILE=${DEB_DSCFILE%.dsc}$OBS_DCH_RELEASE"_"$(chroot $buildroot su -c 'dpkg-architecture -qDEB_BUILD_ARCH')".changes"
	   chroot $buildroot su -c "cd $TOPDIR && echo Running lintian && (set -x && lintian -i $TOPDIR/$DEB_CHANGESFILE)" - $BUILD_USER < /dev/null || BUILD_SUCCEEDED=false
	fi
    fi
}

dsc_move_build_result() {
    for DEB in $BUILD_ROOT/$TOPDIR/*.{deb,changes} ; 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_build_dsc() {
    dsc_build "$BUILD_ROOT"
    dsc_move_build_result
}

recipe_resultdirs_dsc() {
    echo DEBS
}
