Appname:=$(shell cat appname.txt)
temp:=/tmp/fpm-jolla
sourcePath:=$(shell pwd)
output:=$(sourcePath)/output
dependencies=$(shell for file in `cat dependencies.txt`;do echo "-d "$${file};done;)
version:=0.5.0
iteration:=5
fpmExec:=$(shell echo "$${FPM_BIN:=fpm}")

all: clean build-tmp rpm-i686 rpm-jolla rpm-aarch64

build-tmp:
	mkdir -p $(output)
	mkdir -p $(temp)/usr/share/applications
	mkdir -p $(temp)/usr/share/icons/hicolor/86x86/apps
	mkdir -p $(temp)/usr/share/$(Appname)/src
	mkdir -p $(temp)/usr/bin
	cp -ar ./qml $(temp)/usr/share/$(Appname)
	cp -ar ./src/* $(temp)/usr/share/$(Appname)/src
	cp ./dat/$(Appname).desktop $(temp)/usr/share/applications/
	cp -ar ./dat/appicon.png $(temp)/usr/share/icons/hicolor/86x86/apps/$(Appname).png
	install -m 755 ./dat/$(Appname).sh $(temp)/usr/bin/$(Appname)

build: rpmname=$(Appname)-$(version)-$(iteration).$(arch).rpm
build: build-tmp
	cd $(temp); \
	$(fpmExec) -f -s dir -t rpm \
		--after-install $(sourcePath)/dat/upgradeScript.sh \
		--after-remove $(sourcePath)/dat/removeScript.sh \
		--rpm-changelog $(sourcePath)/changelog.txt \
		--directories "/usr/share/$(Appname)" \
		-v $(version) \
		--iteration $(iteration) \
		$(dependencies) \
		-p $(temp)/$(rpmname) \
		-n $(Appname) \
		-a $(arch) \
		--prefix / *; \
	mv $(temp)/*.rpm $(output)

rpm-i686: arch=i686
rpm-i686: build

rpm-jolla: arch=armv7hl
rpm-jolla: build

rpm-aarch64: arch=aarch64
rpm-aarch64: build

clean:
	rm -rf $(temp)
	rm -rf $(builddir)
	rm -rf ./$(Appname)-$(version)-$(iteration)*
