#
# (C)opyright 1993-1997 by Darren Reed.
#
# Redistribution and use in source and binary forms are permitted
# provided that this notice is preserved and due credit is given
# to the original author and the contributors.
#
BINDEST=/usr/local/bin
SBINDEST=/sbin
MANDIR=/usr/share/man
CC=cc
CFLAGS=-I$(TOP)
#
# For SunOS 5.x
#
PKGDIR=root/opt/CYBSipf
PKGMAN=$(PKGDIR)/man
PKGBIN=$(PKGDIR)/bin
#
TOP=..
DCPU=`uname -m`
CC=gcc -Wall
DEBUG=-g
LIBS=-lsocket -lnsl -lelf
DEF=-D_KERNEL -DSUNDDI -D$(DCPU) -D__$(DCPU)__ $(CPU)
ATON=-DNEED_INET_ATON
#
MFLAGS="BINDEST=$(BINDEST)" "SBINDEST=$(SBINDEST)" "MANDIR=$(MANDIR)" \
        'CFLAGS=$(CFLAGS) $(SOLARIS2)' "IPFLKM=$(IPFLKM)" \
        "IPFLOG=$(IPFLOG)" "LOGFAC=$(LOGFAC)" "POLICY=$(POLICY)" \
        "SOLARIS2=$(SOLARIS2)" "DEBUG=$(DEBUG)" "DCPU=$(CPU)" \
        "CPUDIR=$(CPUDIR)"
#
########## ########## ########## ########## ########## ########## ##########
#
CP=/bin/cp
RM=/bin/rm
CHMOD=/bin/chmod
INSTALL=$(TOP)/bsdinstall
#
DFLAGS=$(IPFLKM) $(IPFLOG) $(DEF) -DIPFILTER_LOG $(SOLARIS2)
#-DIPFDEBUG
MODOBJS=ip_sfil.o fil_k.o solaris.o ip_state.o ip_frag.o ip_nat.o ip_proxy.o \
	ip_auth.o ip_log.o
IPF=ipf.o parse.o opt.o inet_addr.o
IPT=ipt.o parse.o fil.o ipft_sn.o ipft_ef.o ipft_td.o ipft_pc.o opt.o \
    ipft_tx.o misc.o ip_state_u.o ip_frag_u.o ip_nat_u.o inet_addr.o \
    ip_fil_u.o ipft_hx.o ip_proxy_u.o ip_auth_u.o
FILS=fils.o parse.o kmem.o opt.o inet_addr.o

sunos5 solaris2 build: ipf.exe ipfstat ipftest ipmon ipnat ipf

ipfstat: $(FILS)
	$(CC) $(DEBUG) $(CFLAGS) $(FILS) -o $@ $(LIBS)

ipf.exe: $(IPF)
	$(CC) $(DEBUG) $(CFLAGS) $(IPF) -o $@ $(LIBS)
	/bin/rm -f $(TOP)/ipf
	ln -s `pwd`/ipf.exe $(TOP)/ipf

ipftest: $(IPT)
	$(CC) $(DEBUG) $(CFLAGS) $(IPT) -o $@ $(LIBS)
	/bin/rm -f $(TOP)/ipftest
	ln -s `pwd`/ipftest $(TOP)/ipftest

ipnat: ipnat.o kmem.o
	$(CC) $(CFLAGS) ipnat.o kmem.o -o $@ $(LIBS)

tests:
	(cd test; make )

fils.o: $(TOP)/fils.c $(TOP)/ip_fil.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/fils.c -o $@

fil.o: $(TOP)/fil.c $(TOP)/ip_fil.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/fil.c -o $@

fil_k.o: $(TOP)/fil.c $(TOP)/ip_fil.h $(TOP)/ipf.h
	$(CC) -I$(TOP) $(POLICY) $(DFLAGS) -c $(TOP)/fil.c -o $@

ipf.o: $(TOP)/ipf.c $(TOP)/ip_fil.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipf.c -o $@

ipt.o: $(TOP)/ipt.c $(TOP)/ip_fil.h $(TOP)/ipt.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipt.c -o $@

misc.o: $(TOP)/misc.c $(TOP)/ip_fil.h $(TOP)/ipt.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/misc.c -o $@

inet_addr.o: $(TOP)/inet_addr.c
	$(CC) $(ATON) $(DEBUG) $(CFLAGS) -c $(TOP)/inet_addr.c -o $@

opt.o: $(TOP)/opt.c $(TOP)/ip_fil.h $(TOP)/ipf.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/opt.c -o $@

ipnat.o: $(TOP)/ipnat.c $(TOP)/ip_fil.h $(TOP)/ipf.h $(TOP)/ip_nat.h
	$(CC) $(CFLAGS) -c $(TOP)/ipnat.c -o $@

ipft_sn.o: $(TOP)/ipft_sn.c $(TOP)/ipt.h $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/snoop.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_sn.c -o $@

ipft_ef.o: $(TOP)/ipft_ef.c $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/ipt.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_ef.c -o $@

ipft_td.o: $(TOP)/ipft_td.c $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/ipt.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_td.c -o $@

ipft_pc.o: $(TOP)/ipft_pc.c $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/ipt.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_pc.c -o $@

ipft_tx.o: $(TOP)/ipft_tx.c $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/ipt.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_tx.c -o $@

ipft_hx.o: $(TOP)/ipft_hx.c $(TOP)/ipf.h $(TOP)/ip_fil.h $(TOP)/ipt.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ipft_hx.c -o $@

ipf:  $(MODOBJS)
	ld -r $(MODOBJS) -o $@

ip_nat.o: $(TOP)/ip_nat.c $(TOP)/ip_nat.h $(TOP)/ip_compat.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_nat.c -o $@

ip_state.o: $(TOP)/ip_state.c $(TOP)/ip_state.h $(TOP)/ip_compat.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_state.c -o $@

ip_proxy.o: $(TOP)/ip_proxy.c $(TOP)/ip_proxy.h $(TOP)/ip_compat.h $(TOP)/ip_ftp_pxy.c
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_proxy.c -o $@

ip_frag.o: $(TOP)/ip_frag.c $(TOP)/ip_frag.h $(TOP)/ip_compat.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_frag.c -o $@

ip_auth.o: $(TOP)/ip_auth.c $(TOP)/ip_auth.h $(TOP)/ip_compat.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_auth.c -o $@

ip_nat_u.o: $(TOP)/ip_nat.c $(TOP)/ip_nat.h $(TOP)/ip_compat.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_nat.c -o $@

ip_frag_u.o: $(TOP)/ip_frag.c $(TOP)/ip_frag.h $(TOP)/ip_compat.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_frag.c -o $@

ip_state_u.o: $(TOP)/ip_state.c $(TOP)/ip_state.h $(TOP)/ip_compat.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_state.c -o $@

ip_auth_u.o: $(TOP)/ip_auth.c $(TOP)/ip_auth.h $(TOP)/ip_compat.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_auth.c -o $@

ip_proxy_u.o: $(TOP)/ip_proxy.c $(TOP)/ip_proxy.h $(TOP)/ip_compat.h $(TOP)/ip_ftp_pxy.c
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_proxy.c -o $@

ip_fil_u.o: $(TOP)/ip_fil.c $(TOP)/ip_fil.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/ip_fil.c -o $@

ip_sfil.o: $(TOP)/ip_sfil.c $(TOP)/ip_fil.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_sfil.c -o $@

ip_log.o: $(TOP)/ip_log.c $(TOP)/ip_fil.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/ip_log.c -o $@

solaris.o: $(TOP)/solaris.c $(TOP)/ipl.h
	$(CC) -I$(TOP) $(DFLAGS) -c $(TOP)/solaris.c -o $@

kmem.o: $(TOP)/kmem.c
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/kmem.c -o $@

parse.o: $(TOP)/parse.c $(TOP)/ip_fil.h
	$(CC) $(DEBUG) $(CFLAGS) -c $(TOP)/parse.c -o $@

ipmon: $(TOP)/ipmon.c
	$(CC) $(DEBUG) $(CFLAGS) $(LOGFAC) $(TOP)/ipmon.c -o $@ $(LIBS)

clean:
	${RM} -f core *.o ipt fils ipf ipfstat ipftest ipmon ipnat ipf.exe
	${RM} -f ipnat.5 ipnat.4 ipnat.1 ipfilter.5
	find root -name CVS -prune -o -type f -print | xargs /bin/rm -f
	make -f Makefile.ipsend clean
	-(for i in *; do \
		if [ -d $${i} -a -f $${i}/Makefile ] ; then \
			cd $${i}; (make clean); cd ..; \
			rm $${i}/Makefile $${i}/Makefile.ipsend; \
			rmdir $${i}; \
		fi \
	 done)

package install:
	@if [ `id|sed -e 's/^.[^(]*(\([^)]*\)).*/\1/'` != root ] ; then \
		echo "Can only build package and install if root"; \
		exit 1; \
	fi
	mkdir -p $(PKGBIN) root/sbin root/usr/kernel/drv root/etc/init.d
	mkdir -p root/usr/include/netinet $(PKGDIR)/examples
	mkdir -p $(PKGMAN)/man1 $(PKGMAN)/man4 $(PKGMAN)/man5 $(PKGMAN)/man8
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipftest $(PKGBIN)/ipftest
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipmon $(PKGBIN)/ipmon
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipsend $(PKGBIN)/ipsend
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipresend $(PKGBIN)/ipresend
	-$(INSTALL) -c -g root -m 755 -o root $(TOP)/mkfilters $(PKGBIN)/mkfilters
	-$(INSTALL) -c -g root -m 755 -o root $(DCPU)/ipf root/usr/kernel/drv/ipf
	-$(INSTALL) -c -g root -m 644 -o root ipf.conf root/usr/kernel/drv
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipnat root/sbin/ipnat
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipf.exe root/sbin/ipf
	-$(INSTALL) -c -s -g root -m 755 -o root $(DCPU)/ipfstat root/sbin/ipfstat
	-$(INSTALL) -c -g root -m 755 -o root ipfboot root/etc/init.d
	-cp $(TOP)/man/*.[0-9] .
	-$(INSTALL) -g root -m 444 -o root ipf.1 $(PKGMAN)/man1
	-$(INSTALL) -g root -m 444 -o root ipnat.1 $(PKGMAN)/man1
	-$(INSTALL) -g root -m 444 -o root ipftest.1 $(PKGMAN)/man1
	-$(INSTALL) -g root -m 444 -o root mkfilters.1 $(PKGMAN)/man1
	-$(INSTALL) -g root -m 444 -o root ipf.4 $(PKGMAN)/man4
	-$(INSTALL) -g root -m 444 -o root ipnat.4 $(PKGMAN)/man4
	-$(INSTALL) -g root -m 444 -o root ipl.4 $(PKGMAN)/man4
	-$(INSTALL) -g root -m 444 -o root ipf.5 $(PKGMAN)/man5
	-$(INSTALL) -g root -m 444 -o root ipnat.5 $(PKGMAN)/man5
	-$(INSTALL) -g root -m 444 -o root ipfilter.5 $(PKGMAN)/man5
	-$(INSTALL) -g root -m 444 -o root ipfstat.8 $(PKGMAN)/man8
	-$(INSTALL) -g root -m 444 -o root ipmon.8 $(PKGMAN)/man8
	-cp $(TOP)/rules/* $(PKGDIR)/examples
	-cp $(TOP)/ip_fil.h $(TOP)/ip_compat.h $(TOP)/ip_state.h .
	-cp $(TOP)/ip_nat.h $(TOP)/ip_frag.h $(TOP)/ip_proxy.h .
	-cp $(TOP)/ip_auth.h .
	-$(INSTALL) -g root -m 644 -o root ip_fil.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_compat.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_state.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_nat.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_frag.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_proxy.h root/usr/include/netinet
	-$(INSTALL) -g root -m 644 -o root ip_auth.h root/usr/include/netinet
	pkgmk -o
	-pkgadd -d /var/spool/pkg

