#############################################################################################
#############################################################################################
##
## PROJECT-DEFINITIONS
##
#############################################################################################
#############################################################################################

COPYRIGHT_TEXT       := © schukai GmbH, Released under the AGPL 3.0 License.
COMPONENTNAME        := RequirementManager
COMPONENTSLUG        := $(shell echo "$COMPONENTNAME" | tr '[:upper:]' '[:lower:]')

#############################################################################################
#############################################################################################
##
## more general block with standard definitions
##
#############################################################################################
#############################################################################################

# get Makefile directory name: http://stackoverflow.com/a/5982798/376773
THIS_MAKEFILE_PATH:=$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
THIS_DIR:=$(shell cd $(dir $(THIS_MAKEFILE_PATH));pwd)/
THIS_MAKEFILE:=$(THIS_DIR)$(THIS_MAKEFILE_PATH)

# colors
BLACK        := $(shell tput -Txterm setaf 0)
RED          := $(shell tput -Txterm setaf 1)
GREEN        := $(shell tput -Txterm setaf 2)
YELLOW       := $(shell tput -Txterm setaf 3)
LIGHTPURPLE  := $(shell tput -Txterm setaf 4)
PURPLE       := $(shell tput -Txterm setaf 5)
BLUE         := $(shell tput -Txterm setaf 6)
WHITE        := $(shell tput -Txterm setaf 7)
RESET        := $(shell tput -Txterm sgr0)

INFO    := $(GREEN)
COMMENT := $(YELLOW)

# Output control and standard outputs
MARKER           := $(BLUE)[+]$(RESET)
ERRORMARKER      := $(RED)[-]$(RESET)
## Deactivate the quite mode by overwriting the value with space

ifndef DEBUG
    QUIET = @
else
    QUIET = 
endif

ifndef DONTOPENBROWSER
    OPENBROWSER = false
else
    OPENBROWSER = true
endif

ECHO             := @echo
ECHOMARKER       := @echo "$(MARKER) $0"
ECHOERRORMARKER  := @echo "$(ERRORMARKER) $0"

# Use bash instead of sh
## Sets the shell used
SHELL            =  bash

# path and binaries
AWK              := awk
GO               := go
CP               := cp
CD               := cd
KILL             := /bin/kill
M4               := m4
MV               := mv
RM               := rm -f
MKDIR            := mkdir -p
SED              := sed
FIND             := find
SORT             := sort
TOUCH            := touch
WGET             := wget
CHMOD            := chmod
RSYNC            := rsync
DOCKER           := docker
NPX              := npx
AWS              := aws
XARGS            := xargs
GREP             := grep
NPM              := npm
make             := make
GIT              := git
NPX              := npx
NODE             := node
LN               := ln -s

# Executable Programs the Installed be have to
EXECUTABLES = $(AWK) $(CP) $(KILL) $(M4) $(MV) rm mkdir $(SED) $(SORT) $(TOUCH) $(WGET) $(CHMOD) $(NPX) $(FIND) $(XARGS) $(GREP) $(NPM) $(GIT) $(NPX) $(NODE) $(GO) ln; 
K := $(foreach exec,$(EXECUTABLES),\
        $(if $(shell which $(exec)),some string,$(error "Missing $(exec) in PATH; please install")))

check_defined = \
    $(strip $(foreach 1,$1, \
        $(call __check_defined,$1,$(strip $(value 2)),$3)))
__check_defined = \
    $(if $(value $1),, \
      $(ECHOERRORMARKER) $(if $2, $2, $1))

#############################################################################################
#############################################################################################
##
## DEFAULT-TARGETS
##
#############################################################################################
#############################################################################################

# @see .PHONY https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html#Phony-Targets

.DEFAULT_GOAL := help

.PHONY: print
print:
	$(ECHO) "THIS_MAKEFILE:      $(THIS_MAKEFILE)"
	$(ECHO) "THIS_MAKEFILE_PATH: $(THIS_MAKEFILE_PATH)"
	$(ECHO) "THIS_DIR:           $(THIS_DIR)"

# Add a comment to the public targets so that it appears
# in this help Use two # characters for a help comment
.PHONY: help
help:
	@printf "${COMMENT}Usage:${RESET}\n"
	@printf " make [target] [arg=\"val\"...]\n\n"
	@printf "${COMMENT}Available targets:${RESET}\n"
	@awk '/^[a-zA-Z\-\_0-9\.@]+:/ { \
		helpMessage = match(lastLine, /^## (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")); \
			helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
			printf " ${INFO}%-22s${RESET} %s\n", helpCommand, helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)
	@printf "\n${COMMENT}Available arguments:${RESET}\n\n"
	@awk '/^(([a-zA-Z\-\_0-9\.@]+)\s=)/ { \
		helpMessage = match(lastLine, /^## (.*)/); \
		if (helpMessage) { \
			helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
			printf " ${INFO}%-22s${RESET} %s (Default: %s)\n", $$1, helpMessage, $$3; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.PHONY: variables
## Print all Variables
variables:
	@$(foreach v, $(.VARIABLES), $(if $(filter file,$(origin $(v))), $(info $(INFO)$(v)$(RESET)=$($(v))$(RESET))))

check_defined = \
    $(strip $(foreach 1,$1, \
        $(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
    $(if $(value $1),, \
      $(error Undefined $1$(if $2, ($2))))
      


#############################################################################################
#############################################################################################
##
## DIRECTORIES
##
#############################################################################################
#############################################################################################

APPLICATION_PATH     :=   $(THIS_DIR)application/
RESOURCE_PATH        :=   $(THIS_DIR)application/resource/
SOURCE_PATH          :=   $(THIS_DIR)application/source/
WEB_PATH             :=   $(THIS_DIR)application/web/
DEPLOYMENT_PATH      :=   $(THIS_DIR)deployment/
VENDOR_PATH          :=   $(THIS_DIR)deployment/vendor/
DEVELOPMENT_PATH     :=   $(THIS_DIR)development/
SCRIPTS_PATH         :=   $(THIS_DIR)development/scripts/
DOCUMENTATION_PATH   :=   $(THIS_DIR)documentation/

## Build path
BUILD_PATH            =   $(DEPLOYMENT_PATH)build/



#############################################################################################
#############################################################################################
##
## DEPENDENCIES (JEKYLL, MINERVA, ...)
##
#############################################################################################
#############################################################################################


#############################################################################################
#############################################################################################
##
## PROJECT-DEFINITIONS
##
#############################################################################################
#############################################################################################



#############################################################################################
#############################################################################################
##
## PROJECT-TARGETS
##
#############################################################################################
#############################################################################################

#############################################################################################
#############################################################################################
##
## PROJECT-TARGETS
##
#############################################################################################
#############################################################################################

## Compiling for every OS and Platform
compile: 
	echo "Compiling for every OS and Platform"

	cd $(SOURCE_PATH); GOOS=linux GOARCH=arm $(GO) build -o $(BUILD_PATH)reqman-linux-arm; cd -
	cd $(SOURCE_PATH); GOOS=linux GOARCH=amd64 $(GO) build -o $(BUILD_PATH)reqman-linux-amd64; cd -
	cd $(SOURCE_PATH); GOOS=linux GOARCH=arm64 $(GO) build -o $(BUILD_PATH)reqman-linux-arm64; cd -
	cd $(SOURCE_PATH); GOOS=linux GOARCH=386 $(GO) build -o $(BUILD_PATH)reqman-linux-386; cd -
	cd $(SOURCE_PATH); GOOS=windows GOARCH=amd64 $(GO) build -o $(BUILD_PATH)reqman-windows; cd -

## compile and deplay to aws
deploy: compile
	find $(BUILD_PATH) -iname reqman-* -exec aws s3 cp {} s3://download.schukai.com/tools/reqman/ \;

## overview-to-aws
overview-to-aws:
	aws s3 cp $(WEB_PATH)/index.html s3://download.schukai.com/tools/reqman/