#   Zerocat Chipflasher --- Flash free firmware, kick the Management Engine.
#
#   Copyright (C) 2015, 2016  kai <kmx@posteo.net>
#   Copyright (C) 2016, 2017, 2018, 2020, 2021, 2022, 2023  Kai Mertens <kmx@posteo.net>
#
#   This file is part of Zerocat Chipflasher.
#
#   Zerocat Chipflasher is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by the Free
#   Software Foundation, either version 3 of the License, or (at your option) any
#   later version.
#
#   Zerocat Chipflasher is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#   details.
#
#   You should have received a copy of the GNU General Public License
#   along with Zerocat Chipflasher.
#   If not, see <http://www.gnu.org/licenses/>.


#   Documentation
#   =============
#
#
#   Usage
#   -----
#
#
#       $ make help


#   Workaround for buggy GNU Make < 4.3
#   ===================================
#
#   Finalize commands within a $(shell) function with semicolon,
#   e.g.: $(shell echo;)


# Shell
SHELL                         := /bin/sh


# White Space
EMPTY                         :=
SPACE                         := $(EMPTY) $(EMPTY)
COMMA                         := ,
M5                            := xxxxx


# Suffixes
.SUFFIXES :
.SUFFIXES : .o .c .h


# Root Paths
ROOT_FW1_SRC                  := ../../firmware1/src/


# Standard Utilities
WHEREIS                       := $(shell command -v whereis;)
WHEREIS_FLAGS                 := -b
$(if $(strip $(WHEREIS)),,    $(error Cannot find whereis utility))

CP                            := $(shell command -v cp;)
CP_FLAGS                      :=
$(if $(strip $(CP)),,         $(error Cannot find cp utility))

MV                            := $(shell command -v mv;)
MV_FLAGS                      :=
$(if $(strip $(MV)),,         $(error Cannot find mv utility))

RM                            := $(shell command -v rm;)
RM_FLAGS                      :=
$(if $(strip $(RM)),,         $(error Cannot find rm utility))

CUT                           := $(shell command -v cut;)
CUT_FLAGS                     := -d' '
$(if $(strip $(CUT)),,        $(error Cannot find cut utility))

GIT                           := $(shell command -v git;)
GIT_FLAGS                     :=
$(if $(strip $(GIT)),,        $(error Cannot find git utility))

SED                           := $(shell command -v sed;)
SED_FLAGS                     := -r
$(if $(strip $(SED)),,        $(error Cannot find sed utility))

SET                           := $(shell command -v set;)
SET_FLAGS                     := -f
$(if $(strip $(SET)),,        $(error Cannot find set utility))

ECHO                          := $(shell $(WHEREIS) $(WHEREIS_FLAGS) echo | $(CUT) $(CUT_FLAGS) -f2;)
ECHO_FLAGS                    := -e
$(if $(strip $(ECHO)),,       $(error Cannot find echo utility))

MKTEMP                        := $(shell command -v mktemp;)
MKTEMP_FLAGS                  :=
$(if $(strip $(MKTEMP)),,     $(error Cannot find mktemp utility))


# Extra Utilities
CC                            := $(shell command -v gcc;)
CFLAGS                        := -Wall
CFLAGS_DEBUG                  := -O0 -g -Wall
LIBCOMMON                     := $(wildcard $(ROOT_FW1_SRC)libcommon/*)
$(if $(strip $(CC)),,         $(error Cannot find cc utility))


# Settings
PROJECT_TITLE                 := Zerocat Chipflasher
PROJECT_BRIEF                 := Flash free firmware to BIOS chips, kick the Management Engine.
MAKEFILE_TITLE                := Build the software, host utility and device firmware.
PROJECT_NUMBER                := $(strip\
	$(shell\
		$(SET) $(SET_FLAGS);\
		$(GIT) $(GIT_FLAGS)\
			describe\
				--match v* |\
		$(SED) $(SED_FLAGS)\
			-e 's/(.*)(-g)([0-9a-f]*)/\1-\3/;'\
				-;\
	)\
)


# Tinned Cans
define HEADER
$(strip\
	$(ECHO) $(ECHO_FLAGS)\
		"\n$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)"\
		"\n$(M5) $(PROJECT_TITLE)"\
		"\n$(M5) – $(PROJECT_BRIEF)"\
		"\n$(M5) $(MAKEFILE_TITLE)"\
		"\n$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)$(M5)"\
)
endef
define HEADLINE
$(strip\
	$(ECHO) $(ECHO_FLAGS)\
		"$(M5) Process target “$@” $(M5)$(M5)"\
)
endef
define DONE
$(strip\
	$(ECHO) $(ECHO_FLAGS)\
		"... successfully processed: “$@”"\
		"\n"\
)
endef


# Built-in Special Targets
#~ .SECONDARY :                     # uncomment for debugging
.PHONY :\
	all\
	\
	hello\
	help\
	\
	clean


# Targets
#   default target
all : hello connect
	@$(HEADLINE)\
	&& $(DONE);

#   display greeting, provided with all, clean and help
hello :
	@$(HEADER)\
	&& $(DONE);

#   display help information
help : hello
	@$(HEADLINE)\
	&& $(ECHO) $(ECHO_FLAGS)\
		"\nList of Targets"\
		"\n==============="\
		"\n"\
		"\nDefault Goal"\
		"\n------------"\
		"\n"\
		"\nall\r\t\tsame as connect, with hello greeter"\
		"\n"\
		"\nHelp"\
		"\n----"\
		"\n"\
		"\nhello\r\t\tgreeter, provided with all, clean and help"\
		"\nhelp\r\t\tdisplay this help information"\
		"\n"\
		"\nCompile"\
		"\n-------"\
		"\n"\
		"\nconnect\r\t\tbuild ‘connect’"\
		"\n"\
		"\nClean"\
		"\n-----"\
		"\n"\
		"\nclean\r\t\tclean ‘connect’"\
		"\n"\
	&& $(DONE);

#   build `connect'
connect : connect.o
	@$(HEADLINE)\
	&& $(CC) $(CFLAGS)\
		-o connect\
			connect.o\
	&& $(DONE);

connect.o : connect.c connect.h $(ROOT_FW1_SRC)libcommon/common.h $(LIBCOMMON)
	@$(HEADLINE)\
	&& tmpfile=`$(MKTEMP) $(MKTEMP_FLAGS)`\
	&& $(CP) $(CP_FLAGS)\
		$(ROOT_FW1_SRC)libcommon/common.h $$tmpfile\
	&& $(SED) $(SED_FLAGS)\
		--in-place\
		-e 's/<VERSION>/$(PROJECT_NUMBER)/g;'\
			$(ROOT_FW1_SRC)libcommon/common.h\
	&& $(CC) $(CFLAGS)\
		-c\
			connect.c\
	&& $(MV) $(MV_FLAGS)\
		$$tmpfile $(ROOT_FW1_SRC)libcommon/common.h\
	&& $(DONE);

clean : hello
	@$(HEADLINE)\
	&& $(RM) $(RM_FLAGS)\
		-f\
		connect\
		connect.o\
	&& $(DONE);
