VERSION=1.5.1
VERSION_HEADER=src/version.h

BUILD_DIR   = build
DEBUG_DIR   = "${BUILD_DIR}/debug"
RELEASE_DIR = "${BUILD_DIR}/release"
PKG_DIR   = pkg

BINNAME   = xclicker
TARGET    = build/debug/src/${BINNAME}
DESKFILE  = assets/xclicker.desktop

debpkgdir="./${PKG_DIR}/deb/package"
appimgdir="${PKG_DIR}/AppImage/XClicker.AppDir"

ifeq ($(TARGET_ARCH),)
TARGET_ARCH := amd64
endif

.PHONY: build
build:
	@if test -d "./${DEBUG_DIR}"; then echo "Build dir is already made"; else meson ${DEBUG_DIR}; fi
# Not "meson compile" since it doesn't work in workflow
	ninja install -C ${DEBUG_DIR}

.PHONY: run
run:
	./${TARGET}
	
.PHONY: all
all: build run

.PHONY: version
version:
	@echo "// Do not edit this file, it is auto generated by the makefile" > ${VERSION_HEADER}
	@echo "#ifndef __VERSION_H" >> ${VERSION_HEADER}
	@echo "#define __VERSION_H" >> ${VERSION_HEADER}
	@echo "static const char *XCLICKER_VERSION = \"v${VERSION}\";" >> ${VERSION_HEADER}
	@echo "#endif" >> ${VERSION_HEADER}

.PHONY: release
release: version
	@if test -d "./${RELEASE_DIR}"; then echo "Build dir is already made"; else meson ${RELEASE_DIR} --buildtype release; fi
	ninja install -C ${RELEASE_DIR}

.PHONY: install
install: release
	@sudo install -Dm 755 ./${RELEASE_DIR}/src/${BINNAME} /usr/bin/${BINNAME}
	@sudo install -Dm 755 ./${DESKFILE} /usr/share/applications/xclicker.desktop
	@sudo install -Dm 644 ./assets/icon.png /usr/share/pixmaps/${BINNAME}.png
	@echo "Installed XClicker"

.PHONY: uninstall
uninstall:
	@sudo rm /usr/bin/${BINNAME}
	@sudo rm /usr/share/applications/xclicker.desktop
	@sudo rm /usr/share/pixmaps/${BINNAME}.png
	@echo "Uninstalled XClicker :("

.PHONY: deb
deb: release
	@rm -rf ${debpkgdir}
	@rm -f ${PKG_DIR}/deb/*.deb
	@mkdir -p ${debpkgdir}

	@install -Dm 644 ./${PKG_DIR}/deb/control ${debpkgdir}/DEBIAN/control
	@sed -i 's/%VERSION%/${VERSION}/g' ${debpkgdir}/DEBIAN/control
	@sed -i 's/%ARCH%/${TARGET_ARCH}/g' ${debpkgdir}/DEBIAN/control
	@install -Dm 755 ./${RELEASE_DIR}/src/${BINNAME} ${debpkgdir}/usr/bin/${BINNAME}
	@install -Dm 644 ./${DESKFILE} ${debpkgdir}/usr/share/applications/xclicker.desktop
	@install -Dm 644 ./assets/icon.png ${debpkgdir}/usr/share/pixmaps/${BINNAME}.png
	@dpkg-deb --build ${debpkgdir}
	@dpkg-name ${PKG_DIR}/deb/package.deb -o

.PHONY: appimg
appimg: release
	@rm -rf ${PKG_DIR}/AppImage/*.AppImage
	@mkdir -p ${appimgdir}
	@install -Dm 755 ./${RELEASE_DIR}/src/${BINNAME} ${appimgdir}/${BINNAME}
	@install -Dm 755 ./${DESKFILE} ${appimgdir}/xclicker.desktop
	@install -Dm 755 ./${PKG_DIR}/AppImage/AppRun ${appimgdir}/AppRun
	@install -Dm 644 ./assets/icon.png ${appimgdir}/${BINNAME}.png
	@cd ${PKG_DIR}/AppImage; appimagetool ./XClicker.AppDir; mv *.AppImage ${BINNAME}_${VERSION}_${TARGET_ARCH}.AppImage

.PHONY: clean
clean:
	@$(RM) -rv ${BUILD_DIR}
