# @configure_input@

DISTRIBUTION = @DISTRIBUTION@
include ../../include/config.Makefile

# we want to ignore the --datarootdir setting, whatever that means, so here we we mention it: @datarootdir@

TIDY = yes
VPATH = @srcdir@
DEPENDS = yes

# To get set up to sign rpm files as "Macaulay2", add these two lines to ~/.rpmmacros :
#   %_signature gpg
#   %_gpg_name  Macaulay2
#
# This is needed to verify signatures:
#   sudo rpm --import Macaulay2-key.txt
# where Macaulay2-key.txt is the public key file obtained from the web site.
# For details, see http://fedoranews.org/tchung/gpg/
#
# To add a signature to an rpm file that doesn't have one, use "rpmsign"
#
# To examine permissions and ownership of files in an rpm, use "rpm -qlvp foo.rpm"
# We do that below after each use of "rpmbuild"

PKG_RPM = @PACKAGE_TARNAME@-@PACKAGE_VERSION@-$(DISTRIBUTION).@ARCH@-@OS@-@ISSUE@.rpm
PKG_COM_RPM = @PACKAGE_TARNAME@-@PACKAGE_VERSION@-$(DISTRIBUTION).common.rpm

############################## main targets
all: chk-opts ../../$(PKG_COM_RPM) ../../$(PKG_RPM) maybe-tidy

# targets for testing the rpm files:
test-rpms: test-signatures test-install test-remove
test-signatures:
	rpm --checksig ../../$(PKG_COM_RPM)
	rpm --checksig ../../$(PKG_RPM)
test-install:
	sudo rpm -i -v ../../$(PKG_COM_RPM)
	sudo rpm -i -v ../../$(PKG_RPM)
test-remove:
	-sudo rpm -e -v Macaulay2-@PACKAGE_VERSION@
	sudo rpm -e -v Macaulay2-common-@PACKAGE_VERSION@
# command for finding what package a file is in:
#   $ rpm --query -f /usr/lib/libmpfr.so
#   mpfr-devel-2.3.0-1.fc8
#   $ rpm --query -f /usr/lib/libmpfr.so.1.1.0 
#   mpfr-2.3.0-1.fc8

chk-opts: check-prefix-is-slash-usr check-debug-disabled check-optimize-enabled
check-prefix-is-slash-usr:; [ "${prefix}" = /usr ]
check-debug-disabled:; [ @DEBUG@ = no ]
check-optimize-enabled:; [ @OPTIMIZE@ = yes ]
check-strip-enabled:; [ @ENABLE_STRIP@ = yes ]
ifeq ($(TIDY),yes)
maybe-tidy: tidy
else
maybe-tidy:
endif

SEDOPTS = -e 's=^\.="=' -e 's=$$="='

T=@TAR@ --create --mode=a+rX,og-ws --exclude-from=@srcdir@/../tar-exclusions --file=-

# One reason to copy all the files into a new directory is that the rpmbuild command tends to delete them.

.prepared:
	rm -rf files
	umask 022 ; $(MKDIR_P) files@prefix@
	umask 022 ; $(T) -C @pre_exec_prefix@ . | @TAR@ xfp - -C files@prefix@

.prepared-common:
	rm -rf files-common
	umask 022 ; $(MKDIR_P) files-common@prefix@
	umask 022 ; $(T) -C @pre_prefix@ . | @TAR@ xfp - -C files-common@prefix@
	: warning: fixed layout in use, just trying both common possibilities:
	@FIND@ files-common@prefix@/man/man1 files-common@prefix@/share/man/man1 -name \*.1 | xargs gzip
	@FIND@ files-common@prefix@ -name \*.info | xargs gzip

Macaulay2.spec: Macaulay2-body.spec packages-used Makefile .prepared 
	sed -e '/Requires/,$$d' \
	    -e 's/^Release: .*/Release: $(DISTRIBUTION)/' <$< >$@
	( echo -n "Requires: Macaulay2-common = @PACKAGE_VERSION@" && \
	  cat packages-used | while read pkg rel ; do echo -n ", $$pkg >= $$rel" ; done && \
	  echo ) >>Macaulay2.spec
	sed -e '1,/Requires/d' -e 's/%PKG_RPM%/$(PKG_RPM)/g' <$< >>$@
	(cd files && @FIND@ . -type d -a -name Macaulay2 -prune -o -type f) | sed $(SEDOPTS) >>Macaulay2.spec

../../$(PKG_RPM) : Macaulay2.spec .prepared
	rpmbuild --buildroot @abs_builddir@/files -bb Macaulay2.spec
	rpm -qlvp ../../$(PKG_RPM) | head

Macaulay2-common.spec: Macaulay2-common-body.spec Makefile .prepared-common
	sed -e 's/^Release: .*/Release: $(DISTRIBUTION)/' \
	    -e 's/%PKG_COM_RPM%/$(PKG_COM_RPM)/g' <$< >$@
	(cd files-common && @FIND@ -type d -a -name Macaulay2 -prune -o -type f) | sed $(SEDOPTS) >>Macaulay2-common.spec

../../$(PKG_COM_RPM) : Macaulay2-common.spec .prepared-common
	rpmbuild --buildroot @abs_builddir@/files-common -bb Macaulay2-common.spec
	rpm -qlvp ../../$(PKG_COM_RPM) | head

file-prereqs: .prepared always
	for i in files@bindir@/M2@EXE@ files@programsdir@/* ;		\
	do if [ -f $$i ] ; then ( set -x ; ldd $$i ) ; fi ;		\
	done								\
	    | sed -e 's/.* => //' -e 's/^	//' -e 's/ .*//'	\
	    | egrep -v '^$(BUILTLIBPATH)'					\
	    | sort | uniq						\
	    > $@ || true
	for i in @FILE_PREREQS@ ; do echo $$i ; done >>$@

# Typical results returned by "rmp --query -f":
#     gmp-devel-4.3.1-7.el6_2.2.x86_64
#     cddlib-094g-9.fc20.x86_64
packages-used: file-prereqs
	cat file-prereqs											\
	| while read x ;											\
	do rpm --query -f "$$x" | sed 's/\([^.]*\)-\(.*\)\(-.*$$\)/\1 \2/' ;					\
	done | fgrep -v 'is not owned by any package' | sort | uniq | sed -e 's=.*/==' -e 's=\.list$$==' >$@

ifneq (@RPMLINT@,)
check:; @RPMLINT@ -i ../../$(PKG_COM_RPM) ../../$(PKG_RPM)
endif

tidy:; rm -rf files files-common Macaulay2.spec Macaulay2-common.spec
clean:: tidy; rm -f ../../$(PKG_RPM) ../../$(PKG_COM_RPM) file-prereqs file-prereqs.tmp packages-used
distclean: clean; rm -f Makefile Macaulay2-body.spec Macaulay2-common-body.spec
Makefile: Makefile.in; cd ../..; ./config.status distributions/rpm/Makefile
Macaulay2-body.spec: Macaulay2-body.spec.in; cd ../..; ./config.status distributions/rpm/Macaulay2-body.spec
Macaulay2-common-body.spec: Macaulay2-common-body.spec.in; cd ../..; ./config.status distributions/rpm/Macaulay2-common-body.spec

# Local Variables:
# compile-command: "make -C $M2BUILDDIR/distributions/rpm "
# End:
.NOTPARALLEL:
