# $Id: Makefile,v 1.11 1999/04/12 23:18:37 moritz Exp $
SUBSTFILES=Makefile fetchipac ipacsum ipacset fetchipac.8 ipacset.8 \
	ipacsum.8
INSTFILES=fetchipac ipacsum ipacset
MANFILES=fetchipac.8 ipacset.8 ipacsum.8
CC=cc
CONFIG=config

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

PRGS=ipchains ipfwadm 

all: dosubst

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 $(INSTFILES)
	touch dosubst

# subst may have changed Makefile!
install: dosubst
	make doinstall

doinstall: install-bin install-man

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

install-man:
	@for file in $(MANFILES); do \
		echo "install -o root -g bin -m 644 $$file $(IMANPATH)/$$file"; \
		install -o root -g bin -m 644 $$file $(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."

clean:
	rm -f subst dosubst subst-config program-paths

