#!/bin/bash
set -xe
rpmbuild=rpmbuild\ --define\ "_topdir${HOME}/redhat"
rpm=rpm\ --define\ "_topdir${HOME}/redhat"
if [ -n "$1" ] ; then
	K=$1
else
	K=$(dir -d1rt ~/Fedora/kernel*src.rpm ~/RH/FC3/updates/SRPMS/kernel*src.rpm  | tail -1)
fi
DirName=$(rpm -qp --qf '%{name}-%{version}\n' ${K})
${rpm} -i ${K}
date=$(date "+%a %b %d %Y")
sed -r <redhat/SPECS/kernel-2.6.spec >redhat/SPECS/kernel-2.6-dl.spec \
	-e 's=FC\>=dl=g'		\
	-e 's=FC2\>=dl2=g'	\
	-e 's=FC3\>=dl3=g'	\
	-e 's=%define builddoc 0=%define builddoc 1=' \
	-e "s=%changelog=%changelog\n* ${date} John Summerfield <summer@herakles.homelinux.org>\n- autobuild for dl  %{release}\n="
:
:
:
:
diff -u redhat/SPECS/kernel-2.6.spec redhat/SPECS/kernel-2.6-dl.spec || :
${rpmbuild} -bp --target i686 redhat/SPECS/kernel-2.6-dl.spec

echo Fixing configs
(
	set -x
	cd $(find ~/redhat -type d -name ${DirName})/lin*
	ls -la
	for f in configs/*.config
		do :
			make -s mrproper
			sed <   $f >.config \
				-e 's+# CONFIG_NTFS_FS is not set+CONFIG_NTFS_FS=m\nCONFIG_NTFS_DEBUG=n\nCONFIG_NTFS_RW=y+' 

			grep -q 'CONFIG_NTFS_FS is not set' .config && exit
			grep -4 NTFS .config
#			make -s oldconfig
			cp -bvp .config ~/redhat/SOURCES/$(basename $f)
			head -9 $f .config ~/redhat/SOURCES/$(basename $f)
			mv -b .config $f
		done
	)

echo Fixed configs

BO="-ba"
for arch in i686 i586 
	do :
		 ${rpmbuild} ${BO}  --clean --target ${arch} redhat/SPECS/kernel-2.6-dl.spec
		 BO="-bb --rmsource"
	done
