# vim:expandtab:shiftwidth=4:softtabstop=4:tabstop=4:

# increment this if you have made a change that should force a new kernel
# to be built for this release of this distribution (only -- if you want to
# force a kernel build on all releases of this distribution, update the BUILD_GEN
# variable in build/lbuild-rhel and if you want to force kernel bulid for all
# distributions, update the BUILD_GEN variable in build/lbuild)
BUILD_GEN+=".0"

# This distro does not support zfs, so define WITH_ZFS
# use words that make the bash log readable.
WITH_ZFS="Zfs Not Supported"

source ${LBUILD_DIR}/lbuild-rhel

SPEC_NAME="kernel-2.6.spec"

patch_spec() {
    local buildid="$1"

    # edit the SPEC with our changes
    patch_spec_common "$buildid"
    sed -i -e '/^# empty final patch file to facilitate testing of kernel patches/i\
# adds Lustre patches\
Patch99995: linux-%{kversion}-lustre.patch' \
           -e '/^# conditionally applied test patch for debugging convenience/i\
# lustre patch\
%patch99995 -p1\
' \
           -e '/-e $RPM_SOURCE_DIR\/kabi_whitelist_/i\
    rm -f $RPM_SOURCE_DIR/kabi_whitelist_%{_target_cpu}$Flavour' \
           -e '/_sourcedir\/kabitool -b \./a\
    cp $RPM_BUILD_ROOT/kabi_whitelist $RPM_SOURCE_DIR/kabi_whitelist_%{_target_cpu}$Flavour' \
           -e '/^%prep$/,/^# END OF PATCH APPLICATIONS$/s/kernel-%{kversion}/%{name}-%{kversion}/g' \
    -e 's/^\(%define signmodules \).*/\10/' \
          SPECS/$SPEC_NAME 2>&1 || \
        fatal 1 "Error while editing SPECS/$SPEC_NAME"

    if $KERNEL_LUSTRE_NAMING; then
        # these are all of the changes needed because we change the package names
        # to kernel-lustre-*.  these should all go away when we stop this insanity
        sed -i -e 's/^\(Name:.*kernel\)/\1-lustre/' \
               -e '/^Provides: glibc-kernheaders = /a\
Provides: kernel-headers = %{rpmversion}-%{release}
Obsoletes: kernel-headers
' \
               -e '/^Provides: kernel-%{_target_cpu} = %{rpmversion}-%{release}$/a\
Provides: kernel = %{rpmversion}-%{release}
' \
               -e '/^Provides: kernel-devel-%{_target_cpu} = %{rpmversion}-%{release}$/a\
Provides: kernel-devel = %{rpmversion}-%{release}
Obsoletes: kernel-devel
' \
               -e '/^Provides: %{name}-debuginfo-common-%{_target_cpu} = %{KVERREL}$/a\
Provides: kernel-debuginfo-common = %{KVERREL}
Obsoletes: kernel-debuginfo-common
' \
               -e '/^Provides: %{name}-debuginfo-%{_target_cpu} = %{KVERREL}$/a\
Provides: kernel-debuginfo = %{KVERREL}
Obsoletes: kernel-debuginfo
' \
              SPECS/$SPEC_NAME 2>&1 || \
            fatal 1 "Error while editing SPECS/$SPEC_NAME"
    fi

    # finally, work around RH bug 491775, if needed
    if ! grep -q "cp -a asm-x86_64 \$RPM_BUILD_ROOT/lib/modules/\$KernelVer/build/include" \
              SPECS/$SPEC_NAME; then
        # XXX - i wonder if we will need to do this ad-hoc patching enough to
        #       formalize a patching system.  let's assume not for the time
        #       being.
        patch -s -p0 <<"EOF" 2>&1 || \
            fatal 1 "Error while patching SPECS/$SPEC_NAME"
--- SPECS/$SPEC_NAME.dist	2009-03-23 20:30:55.000000000 -0400
+++ SPECS/$SPEC_NAME	2009-03-23 20:37:03.000000000 -0400
@@ -6961,6 +6961,10 @@
     cd include
     cp -a acpi config keys linux math-emu media mtd net pcmcia rdma rxrpc scsi sound video asm asm-generic $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
     cp -a `readlink asm` $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
+    if [ "$Arch" = "i386" ]; then
+      mkdir -p $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include/asm-x86_64
+      cp -a asm-x86_64/{stacktrace,k8,pci-direct}.h $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include/asm-x86_64
+    fi
     if [ "$Arch" = "x86_64" ]; then
       cp -a asm-i386 $RPM_BUILD_ROOT/lib/modules/$KernelVer/build/include
     fi
EOF
    fi

    return 0

}

unpack_linux_devel_rpm-rhel5() {
    local callers_rpm="$1"

    unpack_linux_devel_rpm-rhel "$callers_rpm"

    if $USE_KABI; then
        if [ ! -f usr/src/kernels/${lnxmaj}${lnxmin}-${lnxrel}-$TARGET_ARCH/symsets-${lnxmaj}${lnxmin}-${lnxrel}.tar.gz ]; then
            fatal 1 "cannot build modules: the Kernel's symsets is missing."
        fi
    fi

    return 0

}

find_linux_rpm-rhel5() {
    local prefix="$1"
    local wanted_kernel="$2"
    local pathtorpms=${3:-"$KERNELRPMSBASE/$lnxmaj/$DISTROMAJ/$TARGET_ARCH"}

    find_linux_rpm-rhel "$prefix" "$wanted_kernel" "$pathtorpms"
}

kernel_srpm_location() {

    echo "http://ftp.redhat.com/pub/redhat/linux/enterprise/5Server/en/os/SRPMS/"

}
