#!/usr/bin/make -f

#SHELL := /bin/bash
MAKEFILE_FILEPATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(notdir $(patsubst %/,%,$(dir $(MAKEFILE_FILEPATH))))
MAKEFILE_PATH := $(dir $(MAKEFILE_FILEPATH))

CMAKE_COMMAND = cmake
CMAKE_SOURCE_DIR = $(MAKEFILE_PATH)source
CMAKE_BUILD_DIR = $(MAKEFILE_PATH)compile
DEPENDS = /bin/cmake
PREFIX ?= /usr/local
PKGNAME = $(MAKEFILE_DIR)
FILE_VER = source/${PKGNAME}.h

default_target: all

.PHONY: all init depend debug prepare check build uninstall install clean help

all: init build

init:
	@echo "Initialize ..."; \
	if [[ -d ".git" ]]; then \
	    LATEST_TAG=$$(git describe --abbrev=0 --tags | sed 's/^v//'); \
		if [[ -z "$${LATEST_TAG}" ]]; \
		then \
			LATEST_TAG=$$"0.0"; \
			echo "$${LATEST_TAG} is empty"; \
		fi; \
	else \
	    LATEST_TAG="Development"; \
	fi; \
	sed -r "s/^(string version_application).*/\1 = \"$${LATEST_TAG}\";/" -i ${FILE_VER}; \
	echo "-- Build path: ${CMAKE_BUILD_DIR}"

depend:
	@echo "Check depends ..."
	@for FILE_DEPEND in $(DEPENDS); do \
	    if [[ ! -f "$${FILE_DEPEND}" ]]; then \
		echo "ERROR: Depend '$${FILE_DEPEND}' not found !"; \
		exit 1; \
	    fi; \
	done; \
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B${CMAKE_BUILD_DIR} --check-build-system CMakeFiles/Makefile.cmake 1 || exit 1; \
	echo "Check depends: OK"

debug:
	@echo "Debug ..."
	if [[ ! -d "${CMAKE_BUILD_DIR}" ]]; then \
	    $(CMAKE_COMMAND) -S${CMAKE_SOURCE_DIR} -B${CMAKE_BUILD_DIR} -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX="${PREFIX}"; \
	fi; \
	echo "Debug: OK"

prepare:
	@echo "Prepare ..."; \
	if [[ ! -d "${CMAKE_BUILD_DIR}" ]]; then \
	    $(CMAKE_COMMAND) -S${CMAKE_SOURCE_DIR} -B${CMAKE_BUILD_DIR} -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="${PREFIX}"; \
	fi; \
	echo "Prepare: OK"

check:
	@echo "Check ..."; \
	if [[ -f "${CMAKE_BUILD_DIR}/${PKGNAME}" ]]; then \
	    echo "Check: OK"; \
	else \
	    echo "Check: ${CMAKE_BUILD_DIR}/${PKGNAME} not found !"; \
	    exit 1; \
	fi

build: depend prepare
	@echo "Build ..."; \
	$(MAKE_COMMAND) --directory=${CMAKE_BUILD_DIR}; \
	sed -r "s/^(string version_application).*/\1;/" -i ${FILE_VER}; \
	echo "Build: OK"

uninstall:
	@echo "Uninstall ..."
	@for LANG in $$(find ./locale -iname "*.po" -print | sed -En "s/.+_([[:alpha:]]+)\.po/\1/p" | sort -u); do \
#	    PATH_FILE_MO="${DESTDIR}${PREFIX}/share/locale/$${LANG}/LC_MESSAGES/${PKGNAME}.mo"; \
	    PATH_FILE_MO="${DESTDIR}/usr/share/locale/$${LANG}/LC_MESSAGES/${PKGNAME}.mo"; \
	    $(RM) "$${PATH_FILE_MO}"; \
	done
	@for SIZE in 16x16 32x32 48x48 scalable; do \
	    $(RM) "${DESTDIR}${PREFIX}/share/icons/hicolor/$${SIZE}/apps/com.ublinux.${PKGNAME}.svg"; \
	    $(RM) "${DESTDIR}${PREFIX}/share/icons/hicolor/$${SIZE}/apps/com.ublinux.${PKGNAME}.png"; \
	done
	@for FILE_SVG in $(wildcard *.svg); do \
	    for SIZE in 16x16 32x32 48x48 scalable; do \
	        $(RM) "${DESTDIR}${PREFIX}/share/icons/hicolor/$${SIZE}/status/$${FILE_SVG%.*}".{svg,png,jpg}; \
	    done; \
	done
	@for FILE_ICON in $(wildcard icons/*/*.svg); do \
	    SUB_NAME=$${FILE_ICON#*/}; SUB_NAME=$${SUB_NAME%/*}; \
	    $(RM) "${DESTDIR}${PREFIX}/share/icons/hicolor/scalable/$${SUB_NAME}/$${FILE_ICON}"; \
	done
	@$(RM) "${DESTDIR}${PREFIX}/bin/${PKGNAME}"
	@$(RM) "${DESTDIR}${PREFIX}/share/applications/${PKGNAME}.desktop"
	@if [[ "${PREFIX}" == @("/usr"|"/usr/") ]]; then \
	    $(RM) "${DESTDIR}${PREFIX}/share/polkit-1/actions/com.ublinux.${PKGNAME}.policy"; \
	else \
#	    $(RM) "${DESTDIR}${PREFIX}/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
	    $(RM) "${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
	fi
	@if [[ -z "${DESTDIR}" ]]; then \
	    [[ -d "${DESTDIR}${PREFIX}/share/icons/hicolor/" ]] && gtk-update-icon-cache -fiq "${DESTDIR}${PREFIX}/share/icons/hicolor/" &>/dev/null || true; \
	    update-desktop-database --quiet &>/dev/null || true; \
	    [[ -d "${DESTDIR}${PREFIX}/share/applications" ]] && touch "${DESTDIR}${PREFIX}/share/applications" &>/dev/null || true; \
	fi
	@echo "Uninstall: OK"

install: check uninstall
	@echo "Install ..."
	@for LANG in $$(find ./locale -iname "*.po" -print | sed -En "s/.+_([[:alpha:]]+)\.po/\1/p" | sort -u); do \
	    install -dm755 "${DESTDIR}${PREFIX}/share/locale/$${LANG}/LC_MESSAGES"; \
#	    PATH_FILE_MO="${DESTDIR}${PREFIX}/share/locale/$${LANG}/LC_MESSAGES/${PKGNAME}.mo"; \
	    PATH_FILE_MO="${DESTDIR}/usr/share/locale/$${LANG}/LC_MESSAGES/${PKGNAME}.mo"; \
	    PKGNAME_PO="./locale/${PKGNAME}_$${LANG}.po"; [[ -f "$${PKGNAME_PO}" ]] || PKGNAME_PO= ; \
	    msgfmt --verbose --use-fuzzy --output-file "$${PATH_FILE_MO}" - < <(msgcat --use-first --no-wrap $${PKGNAME_PO} ./locale/*_$${LANG}.po); \
	done
	@for SIZE in 16 32 48; do \
	    install -dm755 "${DESTDIR}${PREFIX}/share/icons/hicolor/$${SIZE}x$${SIZE}/apps"; \
	    rsvg-convert -w $${SIZE} -h $${SIZE} -f svg --keep-image-data "icons/apps/com.ublinux.${PKGNAME}.svg" -o "${DESTDIR}${PREFIX}/share/icons/hicolor/$${SIZE}x$${SIZE}/apps/com.ublinux.${PKGNAME}.svg"; \
	done
	@for FILE_ICON in $(wildcard icons/*/*.svg); do \
	    SUB_NAME=$${FILE_ICON#*/}; SUB_NAME=$${SUB_NAME%/*}; \
	    install -Dm644 -t "${DESTDIR}${PREFIX}/share/icons/hicolor/scalable/$${SUB_NAME}" $${FILE_ICON}; \
	done
	@install -Dm755 -t "${DESTDIR}${PREFIX}/bin/" "${CMAKE_BUILD_DIR}/${PKGNAME}"
	@install -Dm644 -t "${DESTDIR}${PREFIX}/share/applications/" "${PKGNAME}.desktop"
	@if [[ "${PREFIX}" == @("/usr"|"/usr/") ]]; then \
	    install -Dm644 -t "${DESTDIR}${PREFIX}/share/polkit-1/actions/" "com.ublinux.${PKGNAME}.policy"; \
	else \
#	    install -Dm644 "com.ublinux.${PKGNAME}.policy" "${DESTDIR}${PREFIX}/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
	    install -Dm644 "com.ublinux.${PKGNAME}.policy" "${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
#	    sed -e "s+/usr/bin+${PREFIX}/bin+" -e "s+\.run+$${PREFIX//\//-}\.run+g" -i "${DESTDIR}${PREFIX}/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
	    sed -e "s+/usr/bin+${PREFIX}/bin+" -e "s+\.run+$${PREFIX//\//-}\.run+g" -i "${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}$${PREFIX//\//-}.policy"; \
	fi
	@if [[ -z "${DESTDIR}" ]]; then \
	    ldconfig -n ${DESTDIR}${PREFIX}/lib; \
	    [[ -d "${DESTDIR}${PREFIX}/share/icons/hicolor/" ]] && gtk-update-icon-cache -fiq "${DESTDIR}${PREFIX}/share/icons/hicolor/" &>/dev/null || true; \
	    update-desktop-database --quiet &>/dev/null || true; \
	    [[ -d "${DESTDIR}${PREFIX}/share/applications" ]] && touch "${DESTDIR}${PREFIX}/share/applications" &>/dev/null || true; \
	fi
	@echo "Install: OK"

clean:
	@echo "Clean ..."
	@$(RM) -rd ${CMAKE_BUILD_DIR}
	@if [[ -d "${CMAKE_BUILD_DIR}" ]]; then \
	    echo "Clean: error, compile directory exist ${CMAKE_BUILD_DIR}"; \
	else \
	    echo "Clean: OK"; \
	fi

help:
	@echo "The following are some of the valid targets for this Makefile:"; \
	echo "... all (the default if no target is provided)"; \
	echo "... init"; \
	echo "... debug"; \
	echo "... prepare"; \
	echo "... compile"; \
	echo "... install"; \
	echo "... uninstall"; \
	echo "... clean"