.PHONY: help all umk theide install clean extra
SHELL = /bin/sh

# Build variables

BUILD_METHOD = GCC.bm
OPTIONS = -bsv
FLAGS = 
OUTPUT = $(PWD)/theide
UPPOUT = $(PWD)/cache/upp

ASSEMBLY=
PACKAGE=

USE_NPROC = true
NPROC = $(shell nproc)

POSIX_DISTRIBUTOR_ID = $(shell lsb_release -si | tr '[:upper:]' '[:lower:]')

# Install variables

DESTDIR = 
prefix = $(HOME)
# prefix = /usr/local
# prefix = /usr
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
datadir = $(prefix)/share
docdir = $(datadir)/doc
mandir = $(datadir)/man
mandir1 = $(mandir)/man1

package_name = upp

userconfdir = $(prefix)/.upp/theide
userspellerdir = $(userconfdir)/speller
sysspellerdir = $(datadir)/$(package_name)/speller

installdoc = true
installdic = true
installman = true

# Command
MAKE = make
CXX = c++
PKGCONFIG = pkg-config
CP = cp -p
RM = rm -f
MKDIR = mkdir -p

# Exit errors

ERROR_CXX_OR_MAKE_MISSING = 1
ERROR_PKGCONFIG_MISSING = 2
ERROR_DEVEL_PKGS_MISSING = 3
ERROR_ASSEMBLY_OR_PACKAGE_NOT_DEFINED = 4

# Targets

help:
	@echo "Usage:"
	@echo "  make [theide|umk] [BUILD_METHOD=<file>|BUILD_METHOD=<method>] [OPTIONS=<options>] [FLAGS=<flags>]"
	@echo "  make [install|uninstall] [DESTDIR=<dir>] [prefix=<dir>] [exec_prefix=<dir>] [bindir=<dir>] [datadir=<dir>] [docdir=<dir>] [mandir=<dir>] [mandir1=<dir>] [userconfdir=<dir>]"
	@echo "  make [clean]"
	@echo ""
	@echo "Example:"
	@echo "  make                                        : show this help message"
	@echo "  make theide                                 : build umk then use it to build theide with default parameters"
	@echo "  make theide OPTIONS=-rbsv                   : build umk then use it to build theide as release binary"
	@echo "  make theide BUILD_METHOD=CLANG FLAGS=GUI,MT : build umk then use it to build theide using CLANG and specific FLAGS"
	@echo "  make umk                                    : build umk only"
	@echo "  make umk MAKE=gmake CXX=g++                 : build umk using gmake and g++ (see Makefile and uppsrc/uMakefile.in for other possible options)"
	@echo "  make install                                : install umk and theide in user HOME directories (see install variables for other possible options)"
	@echo "  make install DESTDIR=/home/user/upp         : install umk and theide in DESTDIR (see install variables for other possible options)"
	@echo "  make install prefix=/usr/local              : install umk and theide in /usr/local POSIX directories (see install variables for other possible options)"
	@echo "  make clean                                  : delete build directory and binaries"
	@echo "  make extra ASSEMBLY=examples PACKAGE=Button : build examples Button package from extra directory"

all: umk theide

umk-dependencies:
	@if [ "$(shell which '$(CXX)')" = "" -o "$(shell which '$(MAKE)')" = "" ] ; then \
		echo "" ; \
		echo "ERROR: C++ compiler and/or make command not found!" ; \
		echo "" ; \
		echo "Please install umk dependencies:" ; \
		if [ "$(shell egrep '^$(POSIX_DISTRIBUTOR_ID).umk.dependencies' buildrequires.posix)" != "" ] ; then \
			echo -n "$(shell egrep '^$(POSIX_DISTRIBUTOR_ID).install.command' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') "  ; \
			echo     $(shell egrep '^$(POSIX_DISTRIBUTOR_ID).umk.dependencies' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; \
		else \
			echo -n  "$(shell egrep '^debian.install.command' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') " ; \
			echo      $(shell egrep '^debian.umk.dependencies' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; \
		fi ; \
		echo "" ; \
		exit $(ERROR_CXX_OR_MAKE_MISSING) ; \
	fi

theide-dependencies: umk-dependencies
	@if [ "$(shell which '$(PKGCONFIG)')" = "" ] ; then \
		echo "" ; \
		echo "ERROR: $(PKGCONFIG) command not found!" ; \
		echo "Please install $(PKGCONFIG) before continuing." ; \
		echo "" ; \
		exit $(ERROR_PKGCONFIG_MISSING) ; \
	fi
	
	@echo "Using pkg-config to find available libraries"
	
	@pkg_missing=false ; \
	if [ "$(shell egrep '^$(POSIX_DISTRIBUTOR_ID).theide.pkg-config' buildrequires.posix)" != "" ] ; then \
		distribution_found=true ; \
		for i in $(shell egrep '^$(POSIX_DISTRIBUTOR_ID).theide.pkg-config' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; do \
			if ! pkg-config --exists $$i ; then \
				pkg_missing="true" ; \
				echo "" ; \
				echo "ERROR: Package $$i.pc not found" ; \
				echo "" ; \
			fi ; \
		done ; \
	else \
		distribution_found=false ; \
		echo "" ; \
		for i in $(shell egrep '^$(POSIX_DISTRIBUTOR_ID).theide.pkg-config' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; do \
			if ! pkg-config --exists $$i ; then \
				pkg_missing="true" ; \
				echo "ERROR: Package $$i.pc not found" ; \
			fi ; \
		done ; \
		echo "" ; \
	fi ; \
	if [ "$$pkg_missing" = "true" ] ; then \
		echo "Please be sure to install theide required dependencies:" ; \
		if [ "$$distribution_found" = "true" ] ; then \
			echo -n "$(shell egrep '^$(POSIX_DISTRIBUTOR_ID).install.command' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') "  ; \
			echo     $(shell egrep '^$(POSIX_DISTRIBUTOR_ID).theide.dependencies' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; \
		else \
			echo -n  "$(shell egrep '^debian.install.command' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') " ; \
			echo      $(shell egrep '^debian.theide.dependencies' buildrequires.posix | sed -e 's|^[[:print:]]\+:[[:space:]]*||g') ; \
		fi ; \
		echo "" ; \
		exit $(ERROR_DEVEL_PKGS_MISSING) ; \
	fi

umk: umk-dependencies
	+@if [ "$(USE_NPROC)" = "true" ] ; then \
		$(MAKE) -C uppsrc -f uMakefile.in UPPOUT="$(UPPOUT)/" OutFile="$(PWD)/umk" -j$(NPROC) ; \
	else \
		$(MAKE) -C uppsrc -f uMakefile.in UPPOUT="$(UPPOUT)/" OutFile="$(PWD)/umk" ; \
	fi

theide: umk theide-dependencies

	+@if [ "$(FLAGS)" != "" ] ; then \
		HOME="$(UPPOUT)/" ./umk uppsrc ide $(BUILD_METHOD) "$(OPTIONS)" "$(FLAGS)" "$(OUTPUT)" ; \
	else \
		HOME="$(UPPOUT)/" ./umk uppsrc ide $(BUILD_METHOD) "$(OPTIONS)" "$(OUTPUT)" ; \
	fi

extra:
	+@if [ "$(ASSEMBLY)" = "" -o "$(PACKAGE)" = "" ] ; then \
		echo "ERROR: ASSEMBLY and PACKAGE variables must be defined!" ; \
		exit $(ERROR_ASSEMBLY_OR_PACKAGE_NOT_DEFINED) ; \
	fi
	
	+@if [ "$(FLAGS)" != "" ] ; then \
		HOME="$(UPPOUT)/" ./umk extra/$(ASSEMBLY),uppsrc $(PACKAGE) $(BUILD_METHOD) "$(OPTIONS)" "$(FLAGS)" "$(UPPOUT)/$(PACKAGE)" ; \
	else \
		HOME="$(UPPOUT)/" ./umk extra/$(ASSEMBLY),uppsrc $(PACKAGE) $(BUILD_METHOD) "$(OPTIONS)" "$(UPPOUT)/$(PACKAGE)" ; \
	fi

install: # TODO
	@# ifeq ("$(prefix)" , "$(HOME)")
	@if [ "$(prefix)" = "$(HOME)" ] ; then                                          \
		echo ">> Starting installation in HOME directory (U++ style)"         ; \
	else                                                                            \
		echo ">> Starting standard installation for POSIX operating systems"  ; \
	fi
	
	@echo ""
	@echo "Variables:"
	@echo ""
	@echo "DESTDIR         = $(DESTDIR)"
	@echo "prefix          = $(prefix)"
	@echo "exec_prefix     = $(exec_prefix)"
	@echo "bindir          = $(bindir)"
	@echo "datadir         = $(datadir)"
	@echo "docdir          = $(docdir)"
	@echo "mandir          = $(mandir)"
	@echo "mandir1         = $(mandir1)"
	@echo "userconfdir     = $(userconfdir)"
	@echo ""
	
# 	@echo "Copying theide and umk"
# 	@$(MKDIR) "$(DESTDIR)/$(prefix)"
# 	@$(MKDIR) "$(DESTDIR)/$(bindir)"
# 	@$(CP) ./theide "$(DESTDIR)/$(bindir)"
# 	@$(CP) ./umk    "$(DESTDIR)/$(bindir)"
# 	
# 	@if [ "$(installdoc)" = "true" ] ; then                                         \
# 		echo "Copying readme.md and license"                                  ; \
# 		$(MKDIR) "$(DESTDIR)/$(docdir)/$(package_name)"                       ; \
# 		$(CP) readme.md          "$(DESTDIR)/$(docdir)/$(package_name)/"      ; \
# 		$(CP) uppsrc/ide/Copying "$(DESTDIR)/$(docdir)/$(package_name)/"      ; \
# 	fi
# 	
# 	@if [ "$(installdic)" = "true" ] ; then                                         \
# 		if [ "$(shell ls *.udc 2>/dev/null)" != "" ] ; then                     \
# 			echo "Copying" *.udc                                          ; \
# 			if [ "$(prefix)" = "$(HOME)" ] ; then                           \
# 				$(MKDIR) "$(DESTDIR)/$(userspellerdir)"               ; \
# 				$(CP) *.udc "$(DESTDIR)/$(userspellerdir)/"           ; \
# 			else                                                            \
# 				$(MKDIR) "$(DESTDIR)/$(sysspellerdir)"                ; \
# 				$(CP) *.udc "$(DESTDIR)/$(sysspellerdir)/"            ; \
# 			fi                                                            ; \
# 		fi                                                                    ; \
# 	fi
# 	
# 	@if [ "$(installman)" = "true" ] ; then                                         \
# 		echo "Copying man pages"                                              ; \
# 		$(MKDIR) "$(DESTDIR)/$(mandir1)"                                      ; \
# 		$(CP) uppsrc/umk/umk.1 "$(DESTDIR)/$(mandir1)/"                       ; \
# 		$(CP) uppsrc/ide/theide.1 "$(DESTDIR)/$(mandir1)/"                    ; \
# 	fi
# 	
# 	@if [ "$(prefix)" = "$(HOME)" -a "$(DESTDIR)" = "" ] ; then                      \
# 		echo "Creating user menu entry"                                       ; \
# 		pushd uppsrc/ide/                                                     ; \
# 		chmod +x "theide.desktop"                                             ; \
# 		xdg-desktop-menu install --mode user --novendor "theide.desktop"      ; \
# 		popd                                                                  ; \
# 	else                                                                            \
# 		echo "Copying icons and desktop file"                                 ; \
# 		$(MKDIR) "$(DESTDIR)/$(datadir)/applications"                         ; \
# 		$(MKDIR) "$(DESTDIR)/$(datadir)/icons/hicolor/48x48/apps"             ; \
# 		$(MKDIR) "$(DESTDIR)/$(datadir)/pixmaps"                              ; \
# 		$(CP) "uppsrc/ide/theide.desktop" "$(DESTDIR)/$(datadir)/applications/theide.desktop"          ; \
# 		$(CP) "uppsrc/ide/theide-48.png"  "$(DESTDIR)/$(datadir)/icons/hicolor/48x48/apps/theide.png"  ; \
# 		$(CP) "uppsrc/ide/theide-48.png"  "$(DESTDIR)/$(datadir)/pixmaps/theide.png"                   ; \
# 	fi
# 	
# 	@echo ""
# 	@echo "<< End of installation"

uninstall: # TODO
	xdg-desktop-menu uninstall --mode user --novendor theide.desktop

clean:
	if [ -d "$(UPPOUT)" ] ; then \
		$(RM) -r "$(UPPOUT)" ; \
	fi
	if [ -d "uppsrc/_out" ] ; then \
		$(RM) -r uppsrc/_out ; \
	fi
	$(RM) uppsrc/build_info.h
	$(RM) uppsrc/ide.out
	$(RM) uppsrc/umk.out
	$(RM) theide
	$(RM) umk
	$(RM) theide.desktop
