TOOLCHAIN = i686-w64-mingw32
TOOLCHAIN_CMD = $(TOOLCHAIN)-

TWAPI_VERSION = 3.1.17
TWAPI_URL = http://sourceforge.net/projects/twapi/files/Current%20Releases/Tcl%20Windows%20API/twapi-$(TWAPI_VERSION)/twapi-$(TWAPI_VERSION).zip
TWAPI_ZIP = archive/twapi-$(TWAPI_VERSION).zip
TWAPI_SHA256 = 8803e90fdc60c8d6a1ca3d559b145e8d09ae2e1259df603ca27c89ff7b0416f4

TCPDUMP_VERSION = 4.5.1
TCPDUMP_URL = http://www.tcpdump.org/release/tcpdump-$(TCPDUMP_VERSION).tar.gz
TCPDUMP_TARBALL = archive/tcpdump-$(TCPDUMP_VERSION).tar.gz
TCPDUMP_SHA256 = 14ab39657128f3a650aed4cf455f76c7590601087b6101c253d698f6e73f0b96

WINPCAP_DEV_VERSION = 4.1.2
WINPCAP_DEV_URL = http://www.winpcap.org/install/bin/WpdPack_4_1_2.zip
WINPCAP_DEV_ZIP = archive/winpcap-dev-$(WINPCAP_DEV_VERSION).zip
WINPCAP_DEV_SHA256 = ea799cf2f26e4afb1892938070fd2b1ca37ce5cf75fec4349247df12b784edbd

WINPCAP_INST_VERSION = 4.1.3
WINPCAP_INST_URL = http://www.winpcap.org/install/bin/WinPcap_4_1_3.exe
WINPCAP_INST_7ZIP = archive/winpcap-inst-$(WINPCAP_INST_VERSION).exe
WINPCAP_INST_SHA256 = fc4623b113a1f603c0d9ad5f83130bd6de1c62b973be9892305132389c8588de
WINPCAP_INST_FILES = files/npf32.sys files/npf64.sys files/packet.dll files/pthreadvc.dll files/wpcap.dll

KITCREATOR_VERSION = 0.7.0
KITCREATOR_URL = http://www.rkeene.org/devel/kitcreator-$(KITCREATOR_VERSION).tar.gz
KITCREATOR_TARBALL = archive/kitcreator-$(KITCREATOR_VERSION).tar.gz
KITCREATOR_SHA256 = 2ee6993cdaafcfe6140364745d56eb1aca83cc6d9b7676e9c28d72ed0edfa177

# Point to real target
all: tcpdump.exe

# WinPcap
$(WINPCAP_INST_7ZIP):
	@-mkdir -p archive >/dev/null 2>/dev/null
	wget --no-use-server-timestamps -O "$(WINPCAP_INST_7ZIP).new" "$(WINPCAP_INST_URL)"
	@if test -n "$(WINPCAP_INST_SHA256)"; then if test "`openssl sha256 "$(WINPCAP_INST_7ZIP).new" | sed 's@^.*= *@@'`" != "$(WINPCAP_INST_SHA256)"; then echo "$(WINPCAP_INST_7ZIP): Checksum Mismatch!"; exit 1; fi; fi
	7z t "$(WINPCAP_INST_7ZIP).new"
	mv "$(WINPCAP_INST_7ZIP).new" "$(WINPCAP_INST_7ZIP)"

$(WINPCAP_INST_FILES): $(WINPCAP_INST_7ZIP)
	@-mkdir -p files
	rm -rf __TMP_WPCAP__
	mkdir __TMP_WPCAP__
	echo u | ( cd __TMP_WPCAP__ && 7z x ../$(WINPCAP_INST_7ZIP) )
	cp '__TMP_WPCAP__/$$SYSDIR/Packet_4.dll' files/packet.dll
	cp '__TMP_WPCAP__/$$SYSDIR/pthreadVC.dll' files/pthreadvc.dll
	cp '__TMP_WPCAP__/$$SYSDIR/wpcap.dll' files/wpcap.dll
	cp '__TMP_WPCAP__/$$SYSDIR/drivers/npf_1.sys' files/npf64.sys
	cp '__TMP_WPCAP__/$$SYSDIR/drivers/npf.sys' files/npf32.sys
	rm -rf __TMP_WPCAP__

# TCPDUMP
$(TCPDUMP_TARBALL):
	@-mkdir -p archive >/dev/null 2>/dev/null
	wget --no-use-server-timestamps -O "$(TCPDUMP_TARBALL).new" "$(TCPDUMP_URL)"
	@if test -n "$(TCPDUMP_SHA256)"; then if test "`openssl sha256 "$(TCPDUMP_TARBALL).new" | sed 's@^.*= *@@'`" != "$(TCPDUMP_SHA256)"; then echo "$(TCPDUMP_TARBALL): Checksum Mismatch!"; exit 1; fi; fi
	gzip -dc "$(TCPDUMP_TARBALL).new" | tar -tf - >/dev/null
	mv "$(TCPDUMP_TARBALL).new" "$(TCPDUMP_TARBALL)"

$(WINPCAP_DEV_ZIP):
	@-mkdir -p archive >/dev/null 2>/dev/null
	wget --no-use-server-timestamps -O "$(WINPCAP_DEV_ZIP).new" "$(WINPCAP_DEV_URL)"
	@if test -n "$(WINPCAP_DEV_SHA256)"; then if test "`openssl sha256 "$(WINPCAP_DEV_ZIP).new" | sed 's@^.*= *@@'`" != "$(WINPCAP_DEV_SHA256)"; then echo "$(WINPCAP_DEV_ZIP): Checksum Mismatch!"; exit 1; fi; fi
	unzip -l "$(WINPCAP_DEV_ZIP).new"
	mv "$(WINPCAP_DEV_ZIP).new" "$(WINPCAP_DEV_ZIP)"

files/tcpdump.exe: $(TCPDUMP_TARBALL) $(WINPCAP_DEV_ZIP) pcap-config
	rm -rf __TMP_TCP__
	mkdir __TMP_TCP__
	( \
		set -e; \
		cd __TMP_TCP__; \
		gzip -dc ../$(TCPDUMP_TARBALL) | tar -xf -; \
		unzip ../$(WINPCAP_DEV_ZIP); \
		cp ../pcap-config WpdPack/; \
		cd WpdPack; \
		echo '#define pcap_can_set_rfmon(x) 0' >> Include/pcap/pcap.h; \
		echo '#define pcap_set_rfmon(x, y) -1' >> Include/pcap/pcap.h; \
		echo '#define pcap_statustostr(x) ""' >>  Include/pcap/pcap.h; \
		cd ../tcpdump-$(TCPDUMP_VERSION); \
		./configure --without-smi --disable-smb --enable-ipv6 --without-crypto --host=$(TOOLCHAIN) PCAP_CONFIG="$$(pwd)/../WpdPack/pcap-config" LIBS='-lws2_32' CFLAGS='-D_WIN32_WINNT=0x0501' CPPFLAGS='-D_WIN32_WINNT=0x0501' ac_cv_func_getnameinfo='yes'; \
		sed -i 's@Ws2tcpip\.h@ws2tcpip.h@' tcpdump-stdinc.h; \
		grep -v '^# *define.*UNALIGNED' tcpdump-stdinc.h | \
			grep -v '<net/netdb\.h>' > tcpdump-stdinc.h.new && mv tcpdump-stdinc.h.new tcpdump-stdinc.h; \
		echo '#define HAVE_SOCKADDR_STORAGE 1' >> config.h; \
		echo '#define HAVE_U_INT8_T 1' >> config.h; \
		echo '#define HAVE_U_INT16_T 1' >> config.h; \
		echo '#define HAVE_U_INT32_T 1' >> config.h; \
		echo '#define HAVE_U_INT64_T 1' >> config.h; \
		echo '#define HAVE_INT8_T 1' >> config.h; \
		echo '#define HAVE_INT16_T 1' >> config.h; \
		echo '#define HAVE_INT32_T 1' >> config.h; \
		echo '#define HAVE_INT64_T 1' >> config.h; \
		echo '#define HAVE_GENERATED_VERSION 1' >> config.h; \
		echo '#define IN6_IS_ADDR_UNSPECIFIED IN6_IS_ADDR_UNSPECIFIED' >> config.h; \
		echo '#undef HAVE_ALARM' >> config.h; \
		grep -v '^# *define *inline' config.h > config.h.new && mv config.h.new config.h; \
		$(MAKE); \
	)
	@-mkdir -p files
	cp __TMP_TCP__/tcpdump-$(TCPDUMP_VERSION)/tcpdump files/tcpdump.exe.new
	$(TOOLCHAIN_CMD)strip files/tcpdump.exe.new
	mv files/tcpdump.exe.new files/tcpdump.exe
	rm -rf __TMP_TCP__

# TWAPI (TCL Windows API)
$(TWAPI_ZIP):
	@-mkdir -p archive >/dev/null 2>/dev/null
	wget --no-use-server-timestamps -O "$(TWAPI_ZIP).new" "$(TWAPI_URL)"
	@if test -n "$(TWAPI_SHA256)"; then if test "`openssl sha256 "$(TWAPI_ZIP).new" | sed 's@^.*= *@@'`" != "$(TWAPI_SHA256)"; then echo "$(TWAPI_ZIP): Checksum Mismatch!"; exit 1; fi; fi
	unzip -l "$(TWAPI_ZIP).new"
	mv "$(TWAPI_ZIP).new" "$(TWAPI_ZIP)"

# Icon
icon/tcpdump.ico: icon/tcpdump.png icon/Makefile
	$(MAKE) -C icon tcpdump.ico

# Tclkit
$(KITCREATOR_TARBALL):
	@-mkdir -p archive >/dev/null 2>/dev/null
	wget --no-use-server-timestamps -O "$(KITCREATOR_TARBALL).new" "$(KITCREATOR_URL)"
	@if test -n "$(KITCREATOR_SHA256)"; then if test "`openssl sha256 "$(KITCREATOR_TARBALL).new" | sed 's@^.*= *@@'`" != "$(KITCREATOR_SHA256)"; then echo "$(KITCREATOR_TARBALL): Checksum Mismatch!"; exit 1; fi; fi
	gzip -dc "$(KITCREATOR_TARBALL).new" | tar -tf - >/dev/null
	mv "$(KITCREATOR_TARBALL).new" "$(KITCREATOR_TARBALL)"

tclkit-zip: $(KITCREATOR_TARBALL) tcpdump.rc icon/tcpdump.ico
	rm -rf kitcreator-$(KITCREATOR_VERSION)
	gzip -dc "$(KITCREATOR_TARBALL)" | tar -xf -
	cp icon/tcpdump.ico kitcreator-$(KITCREATOR_VERSION)/kit.ico
	sed 's/@@TCPDUMP_VERSION_COMMA@@/$(shell echo $(TCPDUMP_VERSION) | sed 's@\.@,@g'),0/;s/@@TCPDUMP_VERSION@@/$(TCPDUMP_VERSION)/' tcpdump.rc > kitcreator-$(KITCREATOR_VERSION)/kit.rc
	cd kitcreator-$(KITCREATOR_VERSION) && KITCREATOR_PKGS=' ' AR=$(TOOLCHAIN_CMD)ar CC=$(TOOLCHAIN_CMD)gcc ./kitcreator 8.5.15 --host=$(TOOLCHAIN) --enable-kit-storage=zip --enable-threads
	cp kitcreator-$(KITCREATOR_VERSION)/tclkit-8.5.15 tclkit-zip.new
	mv tclkit-zip.new tclkit-zip
	rm -rf kitcreator-$(KITCREATOR_VERSION)

# The wrapper
## Split Tclkit header from zip file contents
tcpdump.exe.bin: tclkit-zip
	cp tclkit-zip tcpdump.exe.bin
	strip tcpdump.exe.bin

## Merge Tclkit zip file with TWAPI and re-combine
tcpdump.exe: main.tcl tclkit-zip tcpdump.exe.bin files/tcpdump.exe $(WINPCAP_INST_FILES) $(TWAPI_ZIP)
	rm -f tcpdump.exe.zip
	cp tclkit-zip tcpdump.exe.zip
	zip -J -A -r -u tcpdump.exe.zip files main.tcl
	zipmerge tcpdump.exe.zip $(TWAPI_ZIP)
	cat tcpdump.exe.bin tcpdump.exe.zip > tcpdump.exe.new
	mv tcpdump.exe.new tcpdump.exe
	rm -f tcpdump.exe.zip
	zip -A tcpdump.exe

# Cleanup
clean:
	rm -f tcpdump.exe.zip tcpdump.exe.bin tcpdump.exe.new tcpdump.exe
	rm -rf kitcreator-$(KITCREATOR_VERSION)
	rm -rf __TMP_TCP__ __TMP_WPCAP__
	$(MAKE) -C icon clean

distclean: clean
	rm -f "$(TWAPI_ZIP)" "$(TCPDUMP_TARBALL)" "$(WINPCAP_DEV_ZIP)" "$(KITCREATOR_TARBALL)" "$(WINPCAP_INST_7ZIP)"
	rm -f "$(TWAPI_ZIP).new" "$(TCPDUMP_TARBALL).new" "$(WINPCAP_DEV_ZIP).new" "$(KITCREATOR_TARBALL).new" "$(WINPCAP_INST_7ZIP).new"
	rm -f $(WINPCAP_INST_FILES)
	rm -f tclkit-zip files/tcpdump.exe
	-rmdir archive files

.PHONY: all clean distclean
