#!/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
PKGIDENT = $(subst /,-,$(subst /usr,,${PREFIX}))

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"; \
            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 FILE_PO in $(wildcard *.po); do \
	    LANG=$${FILE_PO##*_};LANG=$${LANG%.*}; \
	    FILE_MO=$${FILE_PO##*/}; FILE_MO="$${FILE_MO%_*.po}.mo"; \
	    PATH_FILE_MO="${DESTDIR}/usr/share/locale/$${LANG}/LC_MESSAGES/$${FILE_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
	@$(RM) "${DESTDIR}${PREFIX}/bin/${PKGNAME}"
	@$(RM) "${DESTDIR}${PREFIX}/share/applications/${PKGNAME}.desktop"
	@$(RM) "${DESTDIR}${PREFIX}/share/icons/hicolor/scalable/apps/com.ublinux.${PKGNAME}.svg"
	@$(RM) "${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}${PKGIDENT}.policy"
	@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 FILE_PO in $(wildcard *.po); do \
		LANG=$${FILE_PO##*_};LANG=$${LANG%.*}; \
		install -dm755 "${DESTDIR}/usr/share/locale/$${LANG}/LC_MESSAGES"; \
		FILE_MO=$${FILE_PO##*/}; FILE_MO="$${FILE_MO%_*.po}.mo"; \
		PATH_FILE_MO="${DESTDIR}/usr/share/locale/$${LANG}/LC_MESSAGES/$${FILE_MO}"; \
		echo "$${FILE_PO}"; \
		msgfmt "$${FILE_PO}" -v -f -o "$${PATH_FILE_MO}"; \
	done
	@for SIZE in 16 32 48; do \
		install -dm755 "${DESTDIR}/usr/share/icons/hicolor/$${SIZE}x$${SIZE}/apps"; \
		rsvg-convert -w $${SIZE} -h $${SIZE} -f svg --keep-image-data "com.ublinux.${PKGNAME}.svg" -o "${DESTDIR}/usr/share/icons/hicolor/$${SIZE}x$${SIZE}/apps/com.ublinux.${PKGNAME}.svg"; \
	done
	@install -Dm644 -t "${DESTDIR}/usr/share/icons/hicolor/scalable/apps/" "com.ublinux.${PKGNAME}.svg"
	@install -Dm644 -t "${DESTDIR}/usr/share/${PKGNAME}/csv" "locales.csv"
	@for FILE_ICON in $(wildcard icons/*/*.svg); do \
	    SUB_NAME=$${FILE_ICON#*/}; SUB_NAME=$${SUB_NAME%/*}; \
	    install -Dm644 -t "${DESTDIR}/usr/share/icons/hicolor/scalable/$${SUB_NAME}" $${FILE_ICON}; \
	done
	@install -Dm644 -t "${DESTDIR}${PREFIX}/share/applications/" "${PKGNAME}.desktop"
	@install -Dm755 -t "${DESTDIR}${PREFIX}/bin/" "${CMAKE_BUILD_DIR}/${PKGNAME}"
	@install -Dm644  "com.ublinux.${PKGNAME}.policy" "${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}${PKGIDENT}.policy"
	@sed -e '\|annotate key=|s|/usr/bin|${PREFIX}/bin|' -e '/action id=/s/\.run/${PKGIDENT}\.run/' -i ${DESTDIR}/usr/share/polkit-1/actions/com.ublinux.${PKGNAME}${PKGIDENT}.policy
	@if [ -z ${DESTDIR} ]; then \
	    [ -d "${DESTDIR}/usr/share/icons/hicolor/" ] && gtk-update-icon-cache -fiq "${DESTDIR}/usr/share/icons/hicolor/" &>/dev/null || true; \
	    update-desktop-database --quiet &>/dev/null || true; \
	    [ -d "${DESTDIR}/usr/share/applications" ] && touch "${DESTDIR}/usr/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 "... build"; \
	echo "... install"; \
	echo "... uninstall"; \
	echo "... clean"