#!/bin/bash

# Copyright (C) 2011 Red Hat, Inc.
#
# This file is part of rpm-build.
#
# rpm-build is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# any later version.
#
# rpm-build 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 rpm-build.  If not, see <http://www.gnu.org/licenses/>.

SELF="$0"
LC_ALL=C
export SELF LC_ALL

if tty 0>&2 >/dev/null; then
    R="\033[1;31m"
    G="\033[1;32m"
    N="\033[0m"
else
    R=
    G=
    N=
fi

printf "${R}
************************************************************************************
*                                                                                  *
*   ${N}rpmbuild-rawbuild is deprecated, please use '${G}csmock --skip-patches${N}' instead!${R}   *
*                                                                                  *
************************************************************************************
${N}\n" >&2

if test -z "$1" || test "x--help" = "x$1"; then
    printf "Usage: %s RPMBUILD_OPTIONS SPEC|SRPM

    This script performs a vanilla build of the given package.  It takes the
    same parameters as rpmbuild does.  Please mark patches that are needed for
    build of the pcakge by %%{?_rawbuild} at end of the %%patchNNN lines.  This
    script will then include them into the vanilla build.\n\n" "$SELF"

    exit 1
fi

# create a temp file for the patch wrapper
PWRAP="`mktemp`"
trap "rm -fv '$PWRAP'" EXIT

# write the patch wrapper
cat > "$PWRAP" << EOF
#!/bin/sh

need_for_build(){
    while test -n "\$1"; do
        if test "x--suffix" = "x\$1"; then
            shift
            if test "x_RAWBUILD" = "x\$1"; then
                return 0
            fi
        fi

        shift
    done

    return 1
}

if need_for_build "\$@"; then
    echo "\$0: applying a patch annotated by _RAWBUILD" >&2
    /usr/bin/patch "\$@"
else
    echo "\$0: ignoring a patch not annotated by _RAWBUILD" >&2
    dd of=/dev/null status=noxfer 2>/dev/null
fi
EOF

# make the patch wrapper executable
chmod 0755 "$PWRAP" || exit 1

# define RPM macros
RPMM="--define '__patch $PWRAP'         \
    --define '_rawbuild -b _RAWBUILD'   \
    --define '_with_vanilla 1'          \
    --define '_without_testsuite 1'     \
    --define 'apidocs 0'                \
    --define 'libguestfs_runtests 0'    \
    --define 'nofips 1'                 \
    --define 'nopam 1'                  \
    --define 'norunuser 1'              \
    --define 'noselinux 1'              \
    --define 'runselftest 0'            \
    --define 'with_docs 0'              \
    --define 'with_publican 0'          \
    --without binfilter                 \
    --without langpacks"

# run rpmbuild using the RPM macros above
eval rpmbuild "$RPMM" "$@"
