#
# Based somewhat on android/Makefile in OpenConnect (David Woodhouse)
#

NDK     := /opt/android-ndk-r10e
ARCH    := arm
GCCVER  := 4.9

# You should be able to just 'make ARCH=x86' and it should DTRT.
ARCH_LIST := arm arm64 x86
APIVER := 21
ifeq ($(ARCH),arm)
TRIPLET := arm-linux-androideabi
TOOLCHAIN := $(TRIPLET)-$(GCCVER)
NDK_ABINAME := armeabi
endif
ifeq ($(ARCH),arm64)
TRIPLET := aarch64-linux-android
TOOLCHAIN := $(TRIPLET)-$(GCCVER)
APIVER  := 21
NDK_ABINAME := arm64-v8a
endif
ifeq ($(ARCH),x86)
TRIPLET := i686-linux-android
TOOLCHAIN := x86-$(GCCVER)
NDK_ABINAME := x86
endif

NDK_SYSROOT := $(NDK)/platforms/android-$(APIVER)/arch-$(ARCH)

BINDIR := $(firstword $(wildcard $(NDK)/toolchains/$(TOOLCHAIN)/prebuilt/*/bin))
PATH := $(BINDIR):$(NDK):$(PATH)

RESDIR := $(shell pwd)/../app/src/main/res/raw/
DESTDIR := $(shell pwd)/$(TRIPLET)/out

CONFIGURE_ARGS := --host=$(TRIPLET) --disable-shared --enable-static \
		  CFLAGS="--sysroot=$(NDK_SYSROOT) -O2 -fvisibility=default -fPIE" \
                  LDFLAGS="-rdynamic -pie" \

PER_ARCH_TARGETS := iptables busybox nflog

.PHONY: all unpack clean
all: $(addsuffix -binaries,$(ARCH_LIST))
clean: $(addsuffix -clean,$(ARCH_LIST))
	rm -rf sources
unpack: $(addsuffix -unpack,$(PER_ARCH_TARGETS))

#####################################################################
#
# Build iptables
#
IPTABLES_VER := 1.4.20
IPTABLES_SRC := sources/iptables-$(IPTABLES_VER)
IPTABLES_BUILD := $(TRIPLET)/iptables

dist/iptables-$(IPTABLES_VER).tar.bz2:
	mkdir -p dist
	curl http://ftp.netfilter.org/pub/iptables/iptables-$(IPTABLES_VER).tar.bz2 -o $@.tmp && mv $@.tmp $@

$(IPTABLES_SRC)/configure: dist/iptables-$(IPTABLES_VER).tar.bz2
	rm -rf $(IPTABLES_SRC)
	mkdir -p sources
	tar -jxf $< -C sources
	cd $(IPTABLES_SRC) && \
		for x in ../../dist/iptables-patches/*; do \
			patch -p1 < $$x || exit 1; \
		done
	cd $(IPTABLES_SRC) && ./autogen.sh

$(IPTABLES_BUILD)/Makefile: $(IPTABLES_SRC)/configure
	mkdir -p $(IPTABLES_BUILD)
	cd $(IPTABLES_BUILD) && ../../$(IPTABLES_SRC)/configure \
		$(CONFIGURE_ARGS) --prefix=/

$(DESTDIR)/sbin/iptables: $(IPTABLES_BUILD)/Makefile
	$(MAKE) -C $(IPTABLES_BUILD)
	$(MAKE) -C $(IPTABLES_BUILD) install DESTDIR=$(DESTDIR)

.PHONY: iptables iptables-unpack
iptables: $(DESTDIR)/sbin/iptables
	cp -L $(DESTDIR)/sbin/iptables $(RESDIR)/iptables_$(ARCH)
	cp -L $(DESTDIR)/sbin/ip6tables $(RESDIR)/ip6tables_$(ARCH)
iptables-unpack: $(IPTABLES_SRC)/configure

#####################################################################
#
# Build busybox
#
BUSYBOX_VER := 1.26.2
BUSYBOX_BUILD := $(TRIPLET)/busybox

dist/busybox-$(BUSYBOX_VER).tar.bz2:
	mkdir -p dist
	curl http://busybox.net/downloads/busybox-$(BUSYBOX_VER).tar.bz2 -o $@.tmp && mv $@.tmp $@

$(BUSYBOX_BUILD)/.unpacked: dist/busybox-$(BUSYBOX_VER).tar.bz2
	rm -rf $(BUSYBOX_BUILD)
	mkdir -p $(BUSYBOX_BUILD)
	tar --strip 1 -jxf $< -C $(BUSYBOX_BUILD)
	cd $(BUSYBOX_BUILD) && \
		for x in ../../dist/busybox-patches/*; do \
			patch -p1 < $$x || exit 1; \
		done
	touch $@

$(BUSYBOX_BUILD)/.configured: $(BUSYBOX_BUILD)/.unpacked
	cp -f dist/busybox-config $(BUSYBOX_BUILD)/.config
	echo "CONFIG_CROSS_COMPILER_PREFIX=\"$(TRIPLET)-\"" >> $(BUSYBOX_BUILD)/.config
	echo "CONFIG_SYSROOT=\"$(NDK_SYSROOT)\"" >> $(BUSYBOX_BUILD)/.config
	touch $@

$(BUSYBOX_BUILD)/busybox: $(BUSYBOX_BUILD)/.configured
	$(MAKE) -C $(BUSYBOX_BUILD) KCONFIG_NOTIMESTAMP=1

.PHONY: busybox busybox-unpack
busybox: $(BUSYBOX_BUILD)/busybox
	cp $< $(RESDIR)/busybox_$(ARCH)
busybox-unpack: $(BUSYBOX_BUILD)/.configured

#####################################################################
#
# Build nflog
#
NDK_OUTDIR := ../libs/$(NDK_ABINAME)

$(NDK_OUTDIR)/nflog: .FORCE
	ndk-build APP_ABI=$(NDK_ABINAME) APP_PLATFORM=$(APIVER) NDK_TOOLCHAIN=$(TOOLCHAIN)

.FORCE:
.PHONY: nflog nflog-unpack
nflog: $(NDK_OUTDIR)/nflog
	cp $< $(RESDIR)/nflog_$(ARCH)
nflog-unpack:

#####################################################################
#
# Build Run PIE
#
# NDK_OUTDIR := ../libs/$(NDK_ABINAME)

# $(NDK_OUTDIR)/run_pie:
#	ndk-build APP_ABI=$(NDK_ABINAME)

#.PHONY: run_pie run_pie-unpack
#run_pie: $(NDK_OUTDIR)/run_pie
#	cp $< $(RESDIR)/run_pie_$(ARCH)
# run_pie-unpack:

#####################################################################
#
# Common targets
#
.PHONY: arch-clean %-clean %-binaries
arch-clean:
	rm -rf $(TRIPLET) $(NDK_OUTDIR)
%-clean:
	$(MAKE) ARCH=$* arch-clean
	ndk-build APP_ABI=$(NDK_ABINAME) clean
%-binaries: unpack
	$(MAKE) ARCH=$* $(PER_ARCH_TARGETS)
