From 28b5496083d4471b25f8a3b67e0d71aaf27a8a3d Mon Sep 17 00:00:00 2001
From: Harald Hoyer <harald@redhat.com>
Date: Fri, 5 Aug 2011 14:18:54 +0200
Subject: [PATCH] Makefile: add "rpm" and "syncheck" target

---
 Makefile    | 33 +++++++++++++++-----------
 dracut.spec | 16 ++++---------
 git2spec.pl | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 26 deletions(-)
 create mode 100755 git2spec.pl

diff --git a/Makefile b/Makefile
index 64da76c..4484e9e 100644
--- a/Makefile
+++ b/Makefile
@@ -68,28 +68,33 @@ dist: dracut-$(VERSION).tar.bz2
 dracut-$(VERSION).tar.bz2:
 	git archive --format=tar $(VERSION) --prefix=dracut-$(VERSION)/ |bzip2 > dracut-$(VERSION).tar.bz2
 
+dracut-$(VERSION).tar.gz:
+	git archive --format=tar $(VERSION) --prefix=dracut-$(VERSION)/ |gzip > dracut-$(VERSION).tar.gz
+
 dracut-$(VERSION)-$(GITVERSION).tar.bz2:
 	git archive --format=tar HEAD --prefix=dracut-$(VERSION)-$(GITVERSION)/ |bzip2 > dracut-$(VERSION)-$(GITVERSION).tar.bz2
 
 
-rpm: clean dracut-$(VERSION).tar.bz2
-	rpmbuild --define "_topdir $$PWD" --define "_sourcedir $$PWD" --define "_specdir $$PWD" --define "_srcrpmdir $$PWD" --define "_rpmdir $$PWD" -ba dracut.spec 
-	rm -fr BUILD BUILDROOT
-
-gitrpm: dracut-$(VERSION)-$(GITVERSION).tar.bz2
-	echo "%define gittag $(GITVERSION)" > dracut.spec.git
-	cat dracut.spec >> dracut.spec.git
-	mv dracut.spec dracut.spec.bak
-	mv dracut.spec.git dracut.spec
-	rpmbuild --define "_topdir $$PWD" --define "_sourcedir $$PWD" --define "_specdir $$PWD" --define "_srcrpmdir $$PWD" --define "_rpmdir $$PWD" --define "gittag $(GITVERSION)" -ba dracut.spec || :
-	mv dracut.spec.bak dracut.spec
-	rm -fr BUILD BUILDROOT
+rpm: dracut-$(VERSION).tar.bz2
+	mkdir -p rpmbuild
+	cp dracut-$(VERSION).tar.bz2 rpmbuild
+	cd rpmbuild; ../git2spec.pl $(VERSION) < ../dracut.spec > dracut.spec; \
+	rpmbuild --define "_topdir $$PWD" --define "_sourcedir $$PWD" \
+	        --define "_specdir $$PWD" --define "_srcrpmdir $$PWD" \
+		--define "_rpmdir $$PWD" -ba dracut.spec && \
+	( cd ..; mv rpmbuild/noarch/*.rpm .; mv rpmbuild/*.src.rpm .;rm -fr rpmbuild; ls *.rpm )
 
-check: all
+syncheck:
 	@ret=0;for i in modules.d/99base/init modules.d/*/*.sh; do \
+                [ "$${i##*/}" = "caps.sh" ] && continue; \
 		dash -n "$$i" ; ret=$$(($$ret+$$?)); \
 	done;exit $$ret
-	make -C test check
+	@ret=0;for i in dracut modules.d/02caps/caps.sh modules.d/*/install modules.d/*/installkernel modules.d/*/check; do \
+		bash -n "$$i" ; ret=$$(($$ret+$$?)); \
+	done;exit $$ret
+
+check: all syncheck
+	$(MAKE) -C test check
 
 testimage: all
 	./dracut -l -a debug -f test-$(shell uname -r).img $(shell uname -r)
diff --git a/dracut.spec b/dracut.spec
index 480429c..05ca285 100644
--- a/dracut.spec
+++ b/dracut.spec
@@ -11,23 +11,15 @@
 %define with_nbd 0
 %endif
 
-%if %{defined gittag}
-%define rdist .git%{gittag}%{?dist}
-%define dashgittag -%{gittag}
-%else
-%define rdist %{?dist}
-%endif
-
 Name: dracut
 Version: 004
-Release: 1%{?rdist}
+Release: 1%{?dist}
 Summary: Initramfs generator using udev
 Group: System Environment/Base		
 License: GPLv2+	
 URL: http://apps.sourceforge.net/trac/dracut/wiki
-# Source can be generated by 
-# http://dracut.git.sourceforge.net/git/gitweb.cgi?p=dracut/dracut;a=snapshot;h=%{?dashgittag};sf=tgz
-Source0: dracut-%{version}%{?dashgittag}.tar.bz2
+
+Source0: http://www.kernel.org/pub/linux/utils/boot/dracut/dracut-%{version}.tar.bz2
 
 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
 
@@ -142,7 +134,7 @@ Requires: cpio gzip
 This package contains tools to assemble the local initrd and host configuration.
 
 %prep
-%setup -q -n %{name}-%{version}%{?dashgittag}
+%setup -q -n %{name}-%{version}
 
 chmod 0755 modules.d/*/check
 # make rpmlint happy
diff --git a/git2spec.pl b/git2spec.pl
new file mode 100755
index 0000000..1f21067
--- /dev/null
+++ b/git2spec.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/perl
+
+sub last_tag {
+    open( GIT, 'git log  --pretty=format:%H |');
+  LINE: while( <GIT> ) {
+      open( GIT2, "git tag --contains $_ |");
+      while( <GIT2> ) {
+	  chomp;
+	  last LINE if /..*/;
+      }
+      close GIT2;
+  }
+    $tag=$_;
+    close GIT2;
+    close GIT;         # be done
+    return $tag;
+};
+
+sub create_patches {
+    my $tag=shift;
+    my $num=0;
+    open( GIT, 'git format-patch --no-renames -N --no-signature '.$tag.' |');
+    @lines=<GIT>;
+    close GIT;         # be done
+    return @lines;
+};
+
+sub filter_patch {
+	my $patch=shift;
+	open(P, $patch);
+	@lines=<P>;
+	close(P);
+	grep (/^ 0 files changed/, @lines);
+}
+
+use POSIX qw(strftime);
+my $datestr = strftime "%Y%m%d", gmtime;
+
+my $tag=shift;
+$tag=&last_tag if not defined $tag;
+my @patches=&create_patches($tag);
+my $num=$#patches + 2;
+$tag=~s/[^0-9]+?([0-9]+)/$1/;
+my $release="$num";
+$release="1" if $num == 1;
+
+while(<>) {
+    if (/^Version:/) {
+	print "Version: $tag\n";
+    }
+    elsif (/^Release:/) {
+	print "Release: $release%{?dist}\n";
+    }
+    elsif ((/^Source0:/) || (/^Source:/)) {
+	print $_;
+	$num=1;
+	for(@patches) {
+	    next if filter_patch $_;
+	    print "Patch$num: $_";
+	    $num++;
+	}
+	print "\n";
+    }
+    elsif (/^%setup/) {
+	print $_;
+	$num=1;
+	for(@patches) {
+	    next if filter_patch $_;
+	    print "%patch$num -p1\n";
+	    $num++;
+	}
+	print "\n";
+    }
+    else {
+	print $_;
+    }
+}
-- 
1.8.3.1

