# Edit Makefile.in and run ./configure

KVERSION = 5.8.3-desktop-1.mga8
KDIR = /usr/src/kernel-5.8.3-desktop-1.mga8
KINSTDIR = $(shell dirname /usr/src/kernel-5.8.3-desktop-1.mga8)
KOPTS =  -DCONFIG_NF_NAT_NEEDED -DHAVE_LLIST
IPTABLES_CFLAGS = -DXTABLES  
IPTABLES_MODULES = /lib64/iptables
DEPMOD = /sbin/depmod -a
CARGS = --kdir=/usr/src/kernel-5.8.3-desktop-1.mga8
SNMPTGSO = /usr/lib/snmp/dlmod/snmp_NETFLOW.so
SNMPCONF = /etc/snmp/snmpd.conf
SNMPLINE = dlmod netflow $(SNMPTGSO)
CC = gcc

# https://www.kernel.org/doc/Documentation/kbuild/modules.txt
# https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt
obj-m = ipt_NETFLOW.o
ccflags-y =  -DCONFIG_NF_NAT_NEEDED -DHAVE_LLIST

all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so 

ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h compat_def.h compat.h Makefile
	@echo Compiling for kernel $(KVERSION)
	make -C $(KDIR) M=$(CURDIR) modules CONFIG_DEBUG_INFO=y
	@touch $@
compat_def.h: gen_compat_def
	./gen_compat_def > $@
sparse: | version.h ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile
	@rm -f ipt_NETFLOW.ko ipt_NETFLOW.o
	@echo Compiling for kernel $(KVERSION)
	make -C $(KDIR) M=$(CURDIR) modules C=1
	@touch ipt_NETFLOW.ko
coverity:
	coverity-submit -v

minstall: | ipt_NETFLOW.ko
	@echo " *"
	make -C $(KDIR) M=$(CURDIR) modules_install INSTALL_MOD_PATH=$(DESTDIR)
	$(DEPMOD)
mclean:
	make -C $(KDIR) M=$(CURDIR) clean
lclean:
	-rm -f *.so *_sh.o
clean: mclean lclean
	-rm -f *.so *.o modules.order version.h compat_def.h

snmp_NETFLOW.so: snmp_NETFLOW.c
	$(CC) -fPIC -shared -o $@ $< -lnetsnmp

sinstall: | snmp_NETFLOW.so IPT-NETFLOW-MIB.my
	@echo " *"
	install -D IPT-NETFLOW-MIB.my $(DESTDIR)/usr/share/snmp/mibs/IPT-NETFLOW-MIB.my
	install -D snmp_NETFLOW.so $(DESTDIR)$(SNMPTGSO)
	@if ! egrep -qs "^ *$(SNMPLINE)" $(SNMPCONF); then \
	echo " *"; \
	echo " *  Add this line to $(SNMPCONF) to enable IPT-NETFLOW-MIB:"; \
	echo " *"; \
	echo " *     $(SNMPLINE)"; \
	echo " *"; \
	fi
	@if killall -0 snmpd >/dev/null 2>&1; then \
	  echo " *  (snmpd needs restart for changes to take effect.)"; \
	else \
	  echo " *  (snmpd is not started.)"; \
	fi

%_sh.o: libipt_NETFLOW.c
	$(CC) $(CFLAGS) -O2 -Wall -Wunused $(IPTABLES_CFLAGS) -fPIC -o $@ -c libipt_NETFLOW.c

%.so: %_sh.o
	$(CC) -shared -o $@ $<

version.h: ipt_NETFLOW.c ipt_NETFLOW.h compat.h Makefile
	@./version.sh --define > version.h

linstall: | libipt_NETFLOW.so libip6t_NETFLOW.so
	@echo " *"
	install -D libipt_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so
	install -D libip6t_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libip6t_NETFLOW.so

dinstall:
	@echo " *"
	@./install-dkms.sh --install

install: minstall linstall  

uninstall:
	-rm -f $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so
	-rm -f $(DESTDIR)$(IPTABLES_MODULES)/libip6t_NETFLOW.so
	-rm -f $(DESTDIR)/usr/share/snmp/mibs/IPT-NETFLOW-MIB.my
	-rm -f $(DESTDIR)$(SNMPTGSO)
	@if egrep -qs "^ *$(SNMPLINE)" $(SNMPCONF); then \
	echo " *"; \
	echo " *  Remove this line from $(SNMPCONF):"; \
	echo " *"; \
	echo " *     "`egrep "^ *$(SNMPLINE)" $(SNMPCONF)`; \
	echo " *"; \
	fi
	@if [ "" = dinstall ]; then ./install-dkms.sh --uninstall; fi
	-rm -f $(DESTDIR)$(KINSTDIR)/extra/ipt_NETFLOW.ko

Makefile: Makefile.in configure
	./configure --make ${CARGS}

load: all
	-insmod ipt_NETFLOW.ko active_timeout=5 protocol=9
	-iptables -I OUTPUT -j NETFLOW
	-iptables -I INPUT -j NETFLOW
	-ip6tables -I OUTPUT -j NETFLOW
	-ip6tables -I INPUT -j NETFLOW

unload:
	-iptables -D OUTPUT -j NETFLOW
	-iptables -D INPUT -j NETFLOW
	-ip6tables -D OUTPUT -j NETFLOW
	-ip6tables -D INPUT -j NETFLOW
	-rmmod ipt_NETFLOW.ko

reload: unload load

ChangeLog:
	gitlog-to-changelog > ChangeLog
.PHONY: ChangeLog

