# FreeS/WAN RedHat RPM makefile
# Copyright (C) 2002  Michael Richardson <mcr@freeswan.org>
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# RCSID $Id: Makefile,v 1.2 2002/08/30 01:27:57 mcr Exp $

FREESWANSRCDIR=$(shell cd ../.. && pwd)
include ${FREESWANSRCDIR}/Makefile.inc
include ${FREESWANSRCDIR}/Makefile.ver

# temporary directory to be used when building RPMs, and where to put the
# resulting RPM tree
RPMKERNDIR := $(shell echo `pwd`/tmp.rpmkernel)
RPMTMPDIR=$(shell echo `pwd`/tmp.rpmbuild)
RPMDEST := $(shell echo `pwd`/rpms)

krpm:
	@if [ ! -d ${RH_KERNELSRC}/configs ]; then echo "Please fix RH_KERNELSRC in Makefile.inc (${RH_KERNELSRC})"; exit 1; fi
	@KV=`${KVUTIL} $(RH_KERNELSRC)/Makefile | sed -e 's/custom//'` ; \
	MD=${RPMTMPDIR}/lib/modules/$$KV/kernel/net/ipsec; mkdir -p $$MD; \
	echo Installing into $$MD for $$KV; \
	cat kernel-list.txt | while read kerneltype arch subarch; \
	do \
	    mkdir -p BUILD.$$kerneltype; \
	    if [ -z "$$subarch" ]; then subarch=$$arch; fi; \
	    BUILDDIR=`pwd`/BUILD.$$kerneltype; \
	    HERE=`pwd` ;\
	    echo Building $$KV-$$kerneltype in $$BUILDDIR; \
	    ${MAKE} -C ${FREESWANSRCDIR} MODBUILDDIR=$$BUILDDIR KERNELSRC=${RH_KERNELSRC} ARCH=$$arch SUBARCH=$$subarch MODULE_DEF_INCLUDE=$$HERE/config-$$kerneltype.h module;\
	    cp $$BUILDDIR/ipsec.o $$MD/ipsec.o-$$kerneltype; \
	    goo="`nm -ao $$BUILDDIR/ipsec.o | ${FREESWANSRCDIR}/programs/calcgoo/calcgoo`"; \
	    (cd $$MD && ln ipsec.o-$$kerneltype $$goo); \
	done

# RPM-build userland install in temporary directory
rpminstall:
	mkdir -p $(RPMTMPDIR)
	$(MAKE) install DESTDIR=$(RPMTMPDIR)

rpmkinstall:
	mkdir -p ${RPMTMPDIR}

	(  \
		cd $(KERNELSRC) ; \
		if egrep -q '^CONFIG_MODULES=y' $(KCFILE) ; \
		then $(MAKE) $(KERNMAKEOPTS) INSTALL_MOD_PATH=$(RPMTMPDIR) \
			SUBDIRS=net/ipsec modules_install ; \
		else \
		  cp arch/$(ARCH)/boot/$(KERNEL) $(RPMBOOT)/vmlinuz-$$KV ; \
		  cp System.map $(RPMBOOT)/System.map-$$KV ; \
		fi; \
	) 2>&1 | tee out.rpmkinstall
	${ERRCHECK} out.rpmkinstall

rpmkmodinstall:
	mkdir -p $(RPMBOOT)
	(cd programs/calcgoo && make calcgoo)
	${ERRCHECK} out.rpmkinstall

# build RPMs
rpms:	rpm.spec $(RPMTMPDIR)
	rm -rf $(RPMDEST)
	mkdir $(RPMDEST)
	cd $(RPMDEST) ; mkdir SRPMS BUILD RPMS SPECS SOURCES
	cd $(RPMDEST)/RPMS ; mkdir $(ARCH) noarch
	rpm -bb --define "buildroot $(RPMTMPDIR)" \
		--define "_topdir $(RPMDEST)" rpm.spec
	# that has, incidentally, gotten rid of $(RPMTMPDIR)

# build spec file for building RPMs
rpm.spec:	rpm.in $(KERNELSRC)/Makefile $(KCFILE)
	KVORIG=`${KVUTIL} $(KERNELSRC)/Makefile | sed -e 's/custom//'` ; \
	KV=`echo $$KVORIG | sed -e 's/-/_/g' ` ; \
	IPSECVERSIONFIXED=`echo ${IPSECVERSION} | sed -e 's/-/_/g'`; \
	echo KVORIG: $$KVORIG KV: $$KV IV: $$IPSECVERSIONFIXED; \
	if egrep -q '^CONFIG_MODULES=y' $(KCFILE) ; \
	then KMO='/^@km */s///' ; \
	else KMO='/^@km */d' ; \
	fi ; \
	if egrep -q '^CONFIG_IPSEC=m' $(KCFILE) ; \
	then FMO='/^@fm */s///' ; \
	else FMO='/^@fm */d' ; \
	fi ; \
	sed -e "/@KERNELVERSION@/s;;$$KV;" \
                -e "/@KERNELVERSIONORIG@/s;;$$KVORIG;" \
		-e "/@IPSECVERSION@/s;;$$IPSECVERSIONFIXED;" \
		-e "$$KMO" \
		-e "$$FMO" \
		-e '/@PUBDIR@/s;;$(PUBDIR);' \
		-e '/@FINALBINDIR@/s;;$(FINALBINDIR);' \
		-e '/@MANTREE@/s;;$(MANTREE);' rpm.in >$@

