ARCH=arm
VERSION=1.29.0
CCPREFIX=arm-linux-gnueabihf-

SRC := $(shell find src -type f | tail -n+2)
SRC_REL := $(shell cd src && find ./ -type f)
TARCONTENT :=

all: KoboRoot.tgz

syncthing-linux-$(ARCH)-v$(VERSION).tar.gz:
	curl -o $@ -L https://github.com/syncthing/syncthing/releases/download/v$(VERSION)/syncthing-linux-$(ARCH)-v$(VERSION).tar.gz

syncthing-linux-$(ARCH)-v$(VERSION): syncthing-linux-$(ARCH)-v$(VERSION).tar.gz
	tar xf $<

src/usr/local/syncthing/syncthing: syncthing-linux-$(ARCH)-v$(VERSION)
	cp syncthing-linux-$(ARCH)-v$(VERSION)/syncthing $@
	$(CCPREFIX)strip $@

src/usr/local/syncthing/AUTHORS.txt: syncthing-linux-$(ARCH)-v$(VERSION)
	cp syncthing-linux-$(ARCH)-v$(VERSION)/AUTHORS.txt $@

src/usr/local/syncthing/LICENSE.txt: syncthing-linux-$(ARCH)-v$(VERSION)
	cp syncthing-linux-$(ARCH)-v$(VERSION)/LICENSE.txt $@

src/usr/local/syncthing/README.txt: syncthing-linux-$(ARCH)-v$(VERSION)
	cp syncthing-linux-$(ARCH)-v$(VERSION)/README.txt $@

KoboRoot.tgz: src/usr/local/syncthing/syncthing src/usr/local/syncthing/AUTHORS.txt src/usr/local/syncthing/LICENSE.txt src/usr/local/syncthing/README.txt $(SRC)
	tar cvzf $@ -C src \
		./usr/local/syncthing/syncthing \
		./usr/local/syncthing/AUTHORS.txt \
		./usr/local/syncthing/LICENSE.txt \
		./usr/local/syncthing/README.txt \
		$(SRC_REL) \
		--owner=0 --group=0

clean:
	rm -rf syncthing-linux-*/ syncthing-linux-*.tar.gz KoboRoot.tgz
	rm -rf src/usr/local/syncthing/{syncthing,AUTHORS.txt,LICENSE.txt,README.txt}

.PHONY: clean