# bzflag
# Copyright (c) 1993 - 2001 Tim Riker
#
# This package is free software;  you can redistribute it and/or
# modify it under the terms of the license found in the file
# named LICENSE that should have accompanied this file.
#
# THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

DEPTH = ..
COMMONPREF = rpm
include $(DEPTH)/Make-common

RPM        = rpm
RPMBASEDIR = tmp
RPMDIR     = $(RPM)/$(RPMBASEDIR)
RPMRC      = rpmrc
SPECFILE   = spec
RPMROOT    = $(RPMDIR)/root
TMPROOT    = /var/tmp/bzflag-$(VERSIONID)-root

LDIRT    = $(RPM)/$(RPMRC)

#
# build RPM package
#
targets: binprep rpmprep
	rpmversion=`rpm --version | sed -e 's/[^0-9]*\([0-9]*\).*/\1/'`; \
	if [ $$rpmversion -ge 3 ]; then \
	  cd $(RPM); rpm -bb --target $(ARCH)-linux \
			--rcfile $(RPMRC) $(RPMBASEDIR)/SPECS/$(SPECFILE); \
	else \
	  cd $(RPM); rpm -bb --buildarch $(ARCH) \
			--rcfile $(RPMRC) $(RPMBASEDIR)/SPECS/$(SPECFILE); \
	fi
	mv $(RPMDIR)/RPMS/$(ARCH)/*.rpm $(PACKAGEDIR)

#
# prepare binaries
#
binprep:
	strip $(TARGETDIR)/bzflag
	strip $(TARGETDIR)/bzfls
	strip $(TARGETDIR)/bzfrelay
	strip $(TARGETDIR)/bzfs

#
# prepare RPM stuff
#
rpmprep:
	# clean up
	-$(RMR) $(RPMDIR) $(TMPROOT)

	# make RPM area
	$(MKDIR) $(RPMDIR) \
		$(RPMDIR)/RPMS $(RPMDIR)/RPMS/$(ARCH) \
		$(RPMDIR)/SOURCES $(RPMDIR)/SPECS $(RPMDIR)/SRPMS

	# build area just our top level directory
	ln -s `cd $(DEPTH); pwd` $(RPMDIR)/BUILD

	# adjust spec file.  replace version number and don't do anything
	# in the prep stage.  also tell rpm where the config file says the
	# data is supposed to go.
	#
	# also what version of rpm?  rpm 2.5 appears to need topdir in
	# rpmrc instead of defined in the spec file but rpm 3.0 won't
	# use that.
	rpmversion=`rpm --version | sed -e 's/[^0-9]*\([0-9]*\).*/\1/'`; \
	( if [ $$rpmversion -ge 3 ]; then \
		echo "%define _topdir $(RPMBASEDIR)"; fi; \
		cat $(RPM)/$(SPECFILE) ) | \
		sed -e 's/%setup -q -c//' \
		-e 's/%define version.*/%define version '$(VERSIONID)/ \
		-e 's@%define bzdatadir.*@%define bzdatadir '$(INSTALL_DATA_DIR)@ > \
						$(RPMDIR)/SPECS/$(SPECFILE)
	# make rpmrc file
	$(ECHO) "" > $(RPM)/$(RPMRC)
	rpmversion=`rpm --version | sed -e 's/[^0-9]*\([0-9]*\).*/\1/'`; \
	if [ $$rpmversion -lt 3 ]; then \
	    $(ECHO) "topdir: $(RPMBASEDIR)" >> $(RPM)/$(RPMRC); \
	fi

	# make buildroot named in spec file point into our RPM root area
	ln -s `pwd`/$(RPMROOT) $(TMPROOT)

#
# easy way to make the spec file.  note that this isn't used for the
# build.  it's just a handy way to get an up to date spec file for
# use directly with rpm.
#
newspec:
	# adjust spec file.  replace version number and tell rpm where
	# the config files say the data is supposed to go.
	cat $(RPM)/$(SPECFILE) | \
		sed -e 's/%define version.*/%define version '$(VERSIONID).$(VERSIONBUILD)/ \
		-e 's@%define bzdatadir.*@%define bzdatadir '$(INSTALL_DATA_DIR)@ > \
								newspec

clean: $(COMMONPREF)clean

clobber: $(COMMONPREF)clobber
	$(RMR) $(RPMDIR) $(TMPROOT) newspec
