mkfile_path := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))

ROOT_DIR := ${mkfile_path}
SCRIPTS_DIR := ${ROOT_DIR}/scripts
BUILD_DIR ?= ${ROOT_DIR}/build
BUILD_LINUX ?= ${BUILD_DIR}/linux
BUILD_WIN ?= ${BUILD_DIR}/win
INSTALL_DIR ?= ${ROOT_DIR}/install
RELEASE_DIR ?= ${ROOT_DIR}/release
WIN_RELEASE_DIR ?= ${RELEASE_DIR}/windows
LINUX_RELEASE_DIR ?= ${RELEASE_DIR}/linux

DEPS=cmake qtdeclarative5-dev libusb-1.0-0-dev
EXTRA_DEPS: rpm

#.PHONY: release prepare build clean windows windows-installer

.PHONY: deps prepare linux-clean windows-clean clean

prepare:
	mkdir -p ${BUILD_LINUX}
	mkdir -p ${BUILD_WIN}
	mkdir -p ${INSTALL_DIR}
	mkdir -p ${RELEASE_DIR}

$(BUILD_LINUX):
	mkdir -p ${BUILD_LINUX}

$(BUILD_WIN):
	mkdir -p ${BUILD_WIN}

$(INSTALL_DIR):
	mkdir -p ${INSTALL_DIR}

$(LINUX_RELEASE_DIR):
	mkdir -p ${LINUX_RELEASE_DIR}

$(WIN_RELEASE_DIR):
	mkdir -p ${WIN_RELEASE_DIR}
deps:
	sudo apt-get install ${DEPS}



### Linux
$(BUILD_LINUX)/nitrokey-app: | ${BUILD_LINUX}
	cd ${BUILD_LINUX} && \
	cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} ${ROOT_DIR} && \
	make

build: $(BUILD_LINUX)/nitrokey-app

install: build | ${INSTALL_DIR}
	cd ${BUILD_LINUX} && make install



### Windows
$(BUILD_WIN)/release/nitrokey-app.exe: | ${BUILD_WIN}
	export PATH=${MXE_ROOT}/usr/bin/:${PATH} && \
	cd ${BUILD_WIN} && \
	${MXE_ROOT}/usr/i686-w64-mingw32.static/qt5/bin/qmake -spec ${MXE_ROOT}/usr/i686-w64-mingw32.static/qt5/mkspecs/win32-g++ -o Makefile ${ROOT_DIR}/nitrokey-app-qt5.pro && \
	make

windows: $(BUILD_WIN)/release/nitrokey-app.exe



### Releasing
linux-release: build | $(LINUX_RELEASE_DIR)
	cd ${BUILD_LINUX} && \
	make package && \
	mv *.deb *.rpm ${LINUX_RELEASE_DIR} && cd ${LINUX_RELEASE_DIR} && \
	find . -name *.deb -type f -printf '%f' | xargs -0 -n1 -I{} sh -c '${SCRIPTS_DIR}/fix-deb.sh {}' && \
	find . -name *.deb -type f -printf "%f" | xargs -0 -n1 -I{} sh -c 'sha256sum -b {} > {}.sha256; md5sum -b {} > {}.md5' && \
	find . -name *.rpm -type f -printf "%f" | xargs -0 -n1 -I{} sh -c 'sha256sum -b {} > {}.sha256; md5sum -b {} > {}.md5'

windows-release: windows | $(WIN_RELEASE_DIR)
	cd ${ROOT_DIR}/nsis && \
	cp ${BUILD_WIN}/release/nitrokey-app.exe . && \
	makensis nitrokey-app.nsis && \
	cp Nitrokey-App-Installer.exe ${BUILD_WIN}/release/

release-all: linux-release windows-release



### Cleaning
linux-clean:
	cd ${BUILD_LINUX} && make clean
windows-clean:
	cd ${BUILD_WIN} && make clean
clean: linux-clean windows-clean

purge:
	rm -rf ${BUILD_DIR}
	rm -rf ${RELEASE_DIR}
