# $Id: Makefile,v 1.18 2000/07/11 15:19:42 moritz Exp $
SUBSTFILES=Makefile ipacsum ipacset fetchipac.8 ipacset.8 \
	ipacsum.8 ipac.h
SHFILES=ipacset ipacsum
ELFFILES=fetchipac
INSTFILES=fetchipac ipacsum ipacset
STRIP_EXT=inst
MANFILES=fetchipac.8 ipacset.8 ipacsum.8
CC=gcc
CFLAGS=-g -O2 -Wall
LDFLAGS=-L.
CONFIG=config

# =()<INSTALLPATH=@<INSTALLPATH>@>()=
INSTALLPATH=/usr/local/bin
# =()<IMANPATH=@<IMANPATH>@>()=
IMANPATH=/usr/local/man/man8
# =()<STRIP_DEBUG=@<STRIP_DEBUG>@>()=
STRIP_DEBUG=yes

PRGS=ipchains ipfwadm 

all: dosubst fetchipac

dosubst: subst subst-config $(SUBSTFILES)
	@echo "running subst on files: $(SUBSTFILES)..."
	@for file in $(SUBSTFILES); do \
		./subst -f subst-config $$file || exit 1; \
	done
	chmod +x $(SUBSTFILES)
	touch dosubst

dbstrip: $(SHFILES) $(ELFFILES)
	@echo -n "stripping debug code... "
	@for file in $(SHFILES); do 				\
		if [ "$(STRIP_DEBUG)" = "yes" ]; then 		\
			awk '					\
				BEGIN {db=0} 			\
				/^#@ DEBUG CODE BEGIN/ {db=1}	\
				db==0 {print}			\
				/^#@ DEBUG CODE END/ {db=0}	\
				' <$$file >$$file.$(STRIP_EXT); \
		else						\
			cp $$file $$file.$(STRIP_EXT);		\
		fi;						\
		chmod +x $$file.$(STRIP_EXT); 			\
		echo -n "$$file ";				\
	done
	@for file in $(ELFFILES); do				\
		if [ "$(STRIP_DEBUG)" = "yes" ]; then		\
			strip $$file;				\
		fi;						\
		echo -n "$$file ";				\
	done
	@echo "done."



# subst may have changed Makefile!
install: all dbstrip
	make doinstall

doinstall: install-bin install-man

install-bin:
	@for file in $(INSTFILES); do 				\
		SFILE="$$file.$(STRIP_EXT)";			\
		test -f $$SFILE || SFILE=$$file;			\
		echo "install -o root -g bin -m 755 $$SFILE $$RPM_BUILD_ROOT/$(INSTALLPATH)/$$file"; \
		install -o root -g bin -m 755 $$SFILE $$RPM_BUILD_ROOT/$(INSTALLPATH)/$$file || exit 1; \
	done

install-man:
	@for file in $(MANFILES); do \
		echo "install -o root -g bin -m 644 $$file $$RPM_BUILD_ROOT/$(IMANPATH)/$$file"; \
		install -o root -g bin -m 644 $$file $$RPM_BUILD_ROOT/$(IMANPATH)/$$file || exit 1; \
	done
	

subst: subst.c
	$(CC) -o subst subst.c

subst-config: config program-paths
	rm -f subst-config
	@if [ ! -d /proc/net ]; then \
		echo "ERROR: /proc not there. Mount it."; \
		exit 1; \
	fi
	@check_pgms() { \
		for prg in $$*; do \
			if grep $$prg program-paths >/dev/null 2>&1; then :; \
			else \
				echo "Error: Executable \"$$prg\" not found"; \
				echo " Install it, then rebuild."; \
				exit 1; \
			fi; \
		done; \
	}; \
	if [ -f /proc/net/ip_fwchains ]; then \
		echo -n "checking required prgrams for ipchains..."; \
		check_pgms ipchains awk mktemp; \
		echo "ok."; \
	elif [ -f /proc/net/ip_acct ]; then \
		echo -n "checking required programs for ipfwadm..."; \
		check_pgms ipfwadm; \
		echo "ok."; \
	else \
		echo "ERROR: firewall module missing in kernel!"; \
		echo " Recompile the kernel with CONFIG_IP_FIREWALL"; \
		echo " or CONFIG_IP_ACCT"; \
		exit 1; \
	fi
	cat $(CONFIG) program-paths > subst-config

program-paths: config
	@echo -n "looking up paths to executables... "
	@rm -f program-paths
	@./pathsearch ipchains IPCHAINS >> program-paths
	@./pathsearch ipfwadm IPFWADM >> program-paths
	@./pathsearch awk AWK >> program-paths
	@./pathsearch mktemp MKTEMP >> program-paths
	@echo "done."

fetchipac: fetchipac.o libipac.a
	$(CC) $(LDFLAGS) fetchipac.o -lipac -o fetchipac

%.o : %.c ipac.h
	$(CC) -c $(CFLAGS) $< -o $@

libipac.a: lock.o rule.o xmalloc.o
	ar -crus libipac.a $?

clean:
	rm -f subst dosubst subst-config program-paths *.o libipac.a \
		fetchipac
	@for file in $(INSTFILES); do			\
		echo "rm -f $$file.$(STRIP_EXT)";	\
		rm -f $$file.$(STRIP_EXT);		\
	done

