##
## Usage: make rpm VERSION=X.X.X RELEASE=X
##

# Copyright (C) 2000,2001 Mandrakesoft
# Author Renaud Chaillat <rchaillat@mandrakesoft.com>

# $Id: Makefile,v 1.17 2003/11/26 21:44:06 florin Exp $

# If no version is given, make the rpm in the local CVS tree 
# with version and release taken from VERSION and RELEASE files
# If version is given, export from the CVS tree

ifdef VERSION
	EXPORT_CVS=1
endif

ifndef VERSION
	VERSION := $(shell cat VERSION)
endif
ifndef RELEASE
	RELEASE := $(shell cat RELEASE)
endif

PERL_ARCHLIB := $(shell perl -V:installarchlib | sed "s/.*='\(.*\)';/\1/")
PERL_PRIVLIB := $(shell perl -V:privlib | sed "s/.*='\(.*\)';/\1/")

NAME=naat-backend
DIST=$(NAME)-$(VERSION)
#FILES=Makefile configuration backend.conf isdndb.net bastille-firewall bastille-firewall.cfg pingtest.conf bastille-firewall-reset bastille-firewall-schedule ifup-local
#FILES=Makefile configuration backend.conf isdndb.net Bastille/{bastille*,ifup*}  pingtest.conf
FILES=Makefile configuration configuration.reset configuration.README backend.conf isdndb.net pingtest.conf
DOCS=README AUTHORS GPL COPYRIGHT ChangeLog
SCRIPTS=events.sh templates.sh scripts.sh install.sh clean.sh
EXECS=naat-console shorewallreset.sh adsl-reconnect.sh scripts/pingtest.sh
RPM=${HOME}/rpm
DESTDIR=


# Some problems with interfaces.pl (calls to DrakXtools)
# A few problems with map (), so for now we compile only 
# config-wrapper and event-handler (not scripts/*)
#	cd lib; \
#	for i in *.pm; do \
#	echo "compiling $$i"; \
#	perl -MO=C $$i > $$i.c ;\
#	perl /usr/lib/perl5/5.6.0/B/cc_harness -o `basename $$i pm`so $$i.c \
#	     /usr/lib/perl5/5.6.0/i386-linux/auto/DynaLoader/DynaLoader.a \
#	     /usr/lib/perl5/5.6.0/i386-linux/auto/Data/Dumper/Dumper.so; \
#	done; \
#	cd ..; \
#	cd scripts; \
#        for i in *.pl; do \
#        echo "compiling $$i"; \
#        perl -MO=C $$i > $$i.c ;\
#        perl /usr/lib/perl5/5.6.0/B/cc_harness -o $$i.bin $$i.c \
#             /usr/lib/perl5/5.6.0/i386-linux/auto/DynaLoader/DynaLoader.a \
#	     /usr/lib/perl5/5.6.0/i386-linux/auto/Data/Dumper/Dumper.so; \
#        done; \
#	cd ..;

all:
	mkdir -p lib/backend; \
	cp lib/config.pm lib/util.pm lib/backend; \
	cd runtime; \
	for i in config-wrapper.pl config-test.pl; do \
	echo "compiling $$i"; \
	perl -I `pwd`/../lib -MO=C $$i > $$i.c ;\
	perl $(PERL_PRIVLIB)/B/cc_harness -o $$i.bin $$i.c \
	     $(PERL_ARCHLIB)/auto/DynaLoader/DynaLoader.a \
	     $(PERL_ARCHLIB)/auto/Data/Dumper/Dumper.so; \
	done; \
	cd ..; \
	rm -Rf lib/backend; \
	cd scripts; \
	gcc -lrrd sumtraffic.c -o sumtraffic; \
	cd ..


rpm: 
# export CVS tree if VERSION was defined on the command line
	( if [ ! -z  $(EXPORT_CVS) ]; then \
		tag=`echo backend_$(VERSION)| sed y/./_/`; \
		rm -Rf naat-backend-cvs-$$tag; \
		cvs -Q export -d naat-backend-cvs-$$tag -r $$tag firewall2/backend; \
		cd naat-backend-cvs-$$tag; \
		echo ... In dir `pwd`; \
	fi; \
	if [ -f "$(DIST).tar.bz2" ]; then \
		 rm -f $(DIST).tar.bz2;   \
	fi; \
	mkdir -p $(DIST)/{runtime,lib}; \
	for file in runtime/*.pl; do    \
		cp $$file $(DIST)/runtime; \
	done; \
	for file in lib/*.pm; do \
		cp $$file $(DIST)/lib; \
	done; \
	cp VERSION RELEASE $(DIST); \
	cp -R templates $(DIST); \
	cp -R scripts $(DIST); \
	find $(DIST)/templates -name CVS -exec rm -Rf {} \; 2>/dev/null; \
	find $(DIST)/scripts -name CVS -exec rm -Rf {} \; 2> /dev/null; \
	cp $(SCRIPTS) $(DIST); \
	cp $(DOCS) $(DIST); \
	cp $(EXECS) $(DIST); \
	cp $(FILES) $(DIST); \
	cp netconnect_new.pm $(DIST)/netconnect.pm; \
	tar cvjf $(DIST).tar.bz2 $(DIST); \
	rm -rf $(DIST); \
	sed -e 's/_VERSION_/$(VERSION)/; s/_RELEASE_/$(RELEASE)/' spec/$(NAME).spec.proto > $(RPM)/SPECS/$(NAME).spec; \
	mv $(DIST).tar.bz2 $(RPM)/SOURCES/; \
	if [ ! -z $(EXPORT_CVS) ]; then cd ..; fi;  \
	echo "Building RPM"; \
	rpm -ba $(RPM)/SPECS/$(NAME).spec) 

install:
	sh install.sh $(DESTDIR)
