+ rm -f $@
+ echo "%_topdir $(HOME)" >> $@
+ echo "%_tmppath $(HOME)/tmp" >> $@
+ ./getrpmmacros.sh >> $@
+
+### this utility allows to extract various info from a spec file
+### and to define them in makefiles
+spec2make: spec2make.c
+ $(CC) -g -Wall $< -o $@ -lrpm -lrpmbuild
+
+### run spec2make on the spec file and include the result
+# usage: spec2make package
+define target_mk
+MAKE/$(1).mk: $($(1).specpath) spec2make .rpmmacros
+ mkdir -p MAKE
+ ./spec2make $($(1)-RPMFLAGS) $($(1).specpath) $(1) > MAKE/$(1).mk
+ @if [ -z MAKE/$(1).mk ] ; then rm MAKE/$(1).mk ; exit 1 ; fi
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_mk,$(package))))
+
+# stores PLDISTRO in a file
+# this is done at stage1. later run wont get confused
+pldistro.mk:
+ echo "PLDISTRO:=$(PLDISTRO)" > $@
+ echo "PLDISTROTAGS:=$(PLDISTROTAGS)" >> $@
+
+savepldistro: pldistro.mk
+.PHONY: savepldistro
+
+# always refresh this
+all: savepldistro
+
+#################### regular make
+
+define stage2_variables
+### devel dependencies
+$(1).rpmbuild = $(if $($(1)-RPMBUILD),$($(1)-RPMBUILD),$(RPMBUILD)) $($(1)-RPMFLAGS)
+$(1).all-devel-rpm-paths := $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).rpm-path))
+$(1).depend-devel-packages := $(sort $(foreach rpm,$($(1)-DEPEND-DEVEL-RPMS),$($(rpm).package)))
+ALL-DEVEL-RPMS += $($(1)-DEPEND-DEVEL-RPMS)
+endef
+
+$(foreach package,$(ALL),$(eval $(call stage2_variables,$(package))))
+ALL-DEVEL-RPMS := $(sort $(ALL-DEVEL-RPMS))
+
+
+### pack sources into tarballs
+ALLTARBALLS:= $(foreach package, $(ALL), $($(package).tarballs))
+tarballs: $(ALLTARBALLS)
+ @echo $(words $(ALLTARBALLS)) source tarballs OK
+.PHONY: tarballs
+
+SOURCES/%.tar.bz2: SOURCES/%
+ tar chpjf $@ -C SOURCES $*
+
+SOURCES/%.tar.gz: SOURCES/%
+ tar chpzf $@ -C SOURCES $*
+
+SOURCES/%.tgz: SOURCES/%
+ tar chpzf $@ -C SOURCES $*
+
+##
+URLS/%: url=$(subst @colon@,:,$(subst @slash@,/,$(notdir $@)))
+URLS/%: basename=$(notdir $(url))
+URLS/%:
+ echo curl $(url) -o SOURCES/$(basename)
+ touch $@
+
+### the directory SOURCES/<package>-<version> is made
+# with a copy -rl from CODEBASES/<package>
+# the former is $(package.source) and the latter is $(package.codebase)
+ALLSOURCES:=$(foreach package, $(ALL), $($(package).source))
+# so that make does not use the rule below directly for creating the tarball files
+.SECONDARY: $(ALLSOURCES)
+
+sources: $(ALLSOURCES)
+ @echo $(words $(ALLSOURCES)) versioned source trees OK
+.PHONY: sources
+
+define target_link_codebase_sources
+$($(1).source): $($(1).codebase) ; mkdir -p SOURCES ; cp -rl $($(1).codebase) $($(1).source)
+endef
+
+$(foreach package,$(ALL),$(eval $(call target_link_codebase_sources,$(package))))
+
+### codebase extraction
+ALLCODEBASES:=$(foreach package, $(ALL), $($(package).codebase))
+# so that make does not use the rule below directly for creating the tarball files
+.SECONDARY: $(ALLCODEBASES)
+
+codebases : $(ALLCODEBASES)
+ @echo $(words $(ALLCODEBASES)) codebase OK
+.PHONY: codebases
+
+### extract codebase
+# usage: extract_single_module package
+define extract_single_module
+ mkdir -p CODEBASES
+ $(if $($($(1).module)-SVNPATH), cd CODEBASES && svn export $($($(1).module)-SVNPATH) $(1), cd CODEBASES && cvs -d $($($(1).module)-CVSROOT) export -r $($($(1).module)-TAG) -d $(1) $($(1).module))
+endef
+
+# usage: extract_multi_module package
+define extract_multi_module
+ mkdir -p CODEBASES/$(1) && cd CODEBASES/$(1) && (\
+ $(foreach m,$($(1)-MODULES), $(if $($(m)-SVNPATH), svn export $($(m)-SVNPATH) $(m);, cvs -d $($(m)-CVSROOT) export -r $($(m)-TAG) $(m);)))
+endef
+
+CODEBASES/%: package=$(notdir $@)
+CODEBASES/%: multi_module=$(word 2,$($(package)-MODULES))
+CODEBASES/%:
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG CODEBASE $(package) : $@ " ; date)
+ $(if $(multi_module),\
+ $(call extract_multi_module,$(package)),\
+ $(call extract_single_module,$(package)))
+ @(echo -n "XXXXXXXXXXXXXXX -- END CODEBASE $(package) : $@ " ; date)
+
+### source rpms
+ALLSRPMS:=$(foreach package,$(ALL),$($(package).srpm))
+srpms: $(ALLSRPMS)
+ @echo $(words $(ALLSRPMS)) source rpms OK
+.PHONY: srpms
+
+# usage: target_source_rpm package
+# select upon the package name, whether it contains srpm or not
+define target_source_rpm
+ifeq "$($(1)-BUILD-FROM-SRPM)" ""
+$($(1).srpm): $($(1).specpath) .rpmmacros $($(1).tarballs)
+ mkdir -p BUILD SRPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using SOURCES) " ; date)
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ $($(1).rpmbuild) -bs $($(1).specpath)
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)
+else
+$($(1).srpm): $($(1).specpath) .rpmmacros $($(1).codebase)
+ mkdir -p BUILD SRPMS tmp
+ @(echo -n "XXXXXXXXXXXXXXX -- BEG SRPM $(1) (using make srpm) " ; date)
+ $(if $($(1).all-devel-rpm-paths), $(RPM-INSTALL-DEVEL) $($(1).all-devel-rpm-paths))
+ make -C $($(1).codebase) srpm SPECFILE=$(HOME)/$($(1).specpath) && \
+ rm -f SRPMS/$(notdir $($(1).srpm)) && \
+ ln $($(1).codebase)/$(notdir $($(1).srpm)) SRPMS/$(notdir $($(1).srpm))
+ $(if $($(1)-DEPEND-DEVEL-RPMS), $(RPM-UNINSTALL-DEVEL) $($(1)-DEPEND-DEVEL-RPMS))
+ @(echo -n "XXXXXXXXXXXXXXX -- END SRPM $(1) " ; date)