include config.mak

CUR_DIR=$(shell pwd)
SRC_DIR=$(CUR_DIR)/src

TARGETS = components configuration share libraries

ifeq ($(BUILD_DOC), yes)
	TARGETS += documentation
endif

.PHONY: all
all: $(TARGETS)

# ------------------------------------------------------------------
# main section
# ------------------------------------------------------------------
.PHONY: components
components:
	cd $(SRC_DIR)/lib && make; cd $(CUR_DIR)
	@for cmp in $(COMPONENTS); do\
		cd $(SRC_DIR)/$$cmp && make; cd $(CUR_DIR); \
	done

.PHONY: documentation
documentation:
	cd $(CUR_DIR)/doc && make; cd $(CUR_DIR)

.PHONY: configuration
configuration:
	cd $(CUR_DIR)/conf && make; cd $(CUR_DIR)

.PHONY: libraries
libraries:

.PHONY: share
share:

# ------------------------------------------------------------------
# cleanup section
# ------------------------------------------------------------------
.PHONY: clean
clean: clean_components clean_documentation clean_config clean_config_mak
	@echo "All files cleaned successfully!"

.PHONY: clean_components
clean_components:
	@for cmp in $(COMPONENTS); do \
		cd $(SRC_DIR)/$$cmp && make clean; cd $(CUR_DIR); \
	done
	cd $(SRC_DIR)/lib && make clean; cd $(CUR_DIR);

.PHONY: clean_documentation
clean_documentation:
	cd $(CUR_DIR)/doc && make clean; cd $(CUR_DIR)

.PHONY: clean_config
clean_config:
	cd $(CUR_DIR)/conf && make clean; cd $(CUR_DIR)

.PHONY: clean_config_mak
clean_config_mak:
	rm config.mak || true

# ------------------------------------------------------------------
# install section
# ------------------------------------------------------------------
.PHONY: install
install: all install_dirs install_components install_configuration install_libraries install_documentation install_share

.PHONY: install_dirs
install_dirs:
	install -d $(PREFIX) \
	  	$(BIN_DIR) \
	  	$(CONF_DIR) \
	 	$(LANG_DIR) \
		$(LIB_DIR) \
		$(DOC_DIR) \
		$(SHARE_DIR)/info 

.PHONY: install_components
install_components: components
	@for cmp in $(COMPONENTS); do \
	  	cd $(SRC_DIR)/$$cmp && make install; cd $(CUR_DIR); \
	done

.PHONY: install_configuration
install_configuration: configuration
	cd $(CUR_DIR)/conf && make install; cd $(CUR_DIR)

.PHONY: install_libraries
install_libraries: libraries
	cd $(CUR_DIR)/lib && make install; cd $(CUR_DIR)

.PHONY: install_documentation
install_documentation: documentation
	cd $(CUR_DIR)/doc && make install; cd $(CUR_DIR)

.PHONY: install_share
install_share: share
	cd $(CUR_DIR)/share && make install; cd $(CUR_DIR)
	echo grammar compilation...
	dgc

# ------------------------------------------------------------------
# uninstall section
# ------------------------------------------------------------------

.PHONY: uninstall
uninstall: uninstall_share uninstall_documentation uninstall_libraries uninstall_configuration uninstall_components uninstall_dirs

.PHONY: uninstall_components
uninstall_components:
	@for cmp in $(COMPONENTS); do \
	  	cd $(SRC_DIR)/$$cmp && make uninstall; cd $(CUR_DIR); \
	done

.PHONY: uninstall_configuration
uninstall_configuration:
	cd $(CUR_DIR)/conf && make uninstall; cd $(CUR_DIR)

.PHONY: uninstall_libraries
uninstall_libraries:
	cd $(CUR_DIR)/lib && make uninstall; cd $(CUR_DIR)

.PHONY: uninstall_documentation
uninstall_documentation:
	cd $(CUR_DIR)/doc && make uninstall; cd $(CUR_DIR)

.PHONY: uninstall_share
uninstall_share:
	cd $(CUR_DIR)/share && make uninstall; cd $(CUR_DIR)

.PHONY: uninstall_dirs
uninstall_dirs: uninstall_configuration uninstall_documentation uninstall_share uninstall_libraries
	rmdir $(CONF_DIR)
	rmdir $(DOC_DIR)
	rmdir $(LANG_DIR)
	rmdir $(LIB_DIR)
