#   Zerocat Chipflasher --- Flash free firmware, kick the Management Engine.
#
#   Copyright (C) 2016  kai <kmx@posteo.net>
#   Copyright (C) 2016, 2017, 2018, 2020, 2021, 2022, 2023, 2024  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
#   =============
#
#
#   FIXME: Those steps missing:
#
#   * propeller-load -s cmm/libfdserial.elf
#   * propeller-elf-objdump -h cmm/libfdserial.elf
#
#
#   Usage
#   -----
#
#
#       $ make help


#   Workaround for 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                            := ccccc
TAB1                          := $(SPACE)$(SPACE)$(SPACE)$(SPACE)


# Root Paths
ROOT_DOC                      := ../../doc/
ROOT_HOST_START               := ../../host/start/
ROOT_FW1_SRC                  := ../src/
ROOT_FW1_START                := $(EMPTY)


# Utilities
LS                            := $(shell command -v ls;)
LS_FLAGS                      := -l
$(if $(strip $(LS)),,         $(error Cannot find ls utility))

CAT                           := $(shell command -v cat;)
CAT_FLAGS                     :=
$(if $(strip $(CAT)),,        $(error Cannot find cat 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                     := -E
$(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 command -v echo;)
ECHO_FLAGS                    :=
$(if $(strip $(ECHO)),,       $(error Cannot find echo utility))

SLEEP                         := $(shell command -v sleep;)
SLEEP_FLAGS                   :=
$(if $(strip $(SLEEP)),,      $(error Cannot find sleep utility))

PROPLOAD                      := $(shell command -v propeller-load;)
PROPLOAD_FLAGS                := -i -v -v -v -m
$(if $(strip $(PROPLOAD)),,   $(error Cannot find propeller-load utility))


# Start Options (propeller-load)
KICK_LMM_ELF                  := $(ROOT_FW1_SRC)kick.lmm.elf
BOARD_CONFIG                  := $(ROOT_FW1_START)board.cfg
PTR_TTY_PORT                  := $(ROOT_HOST_START)tty_port_pointer
TTY_PORT                      = $(strip\
	$(shell\
		$(LS) $(LS_FLAGS) $(PTR_TTY_PORT)\
		| $(SED) $(SED_FLAGS) -e's/(.*-> )(.*)/\2/' -\
	)\
)
TTY_BAUDRATE                  = $(strip\
	$(shell\
		$(SED) $(SED_FLAGS)\
			-e '/^baudrate:/!d; s/^.*\: //;'\
				$(BOARD_CONFIG);\
	)\
)


# Project Settings
include $(ROOT_DOC)project-numbers.mk


# Settings
MAKEFILE_TITLE                := Load ‘kick’ to Chipflasher’s RAM or EEPROM
PROJECT_TITLE                 := Zerocat Chipflasher
PROJECT_BRIEF                 := Flash free firmware to BIOS chips, kick the Management Engine.
RESET_DELAY                   := 5s


# Suffixes, to be checked for old fashioned suffix rules
.SUFFIXES :


# Intermediate Targets
#~ .INTERMEDIATE :


# Secondary Targets, enable empty list for debugging
#~ .SECONDARY :


# Phony Targets
.PHONY:\
	all\
	\
	hello\
	help\
	\
	config-baud115200\
	config-baud57600\
	config-baud38400\
	\
	reset\
	kick-eeprom\
	kick-ram\
	kick-ram-terminal\
	\
	clean-config\
	clean


# Tinned Cans: HEADER, HEADLINE, DONE
include $(ROOT_DOC)tinned-headers.mk


# Tinned Can: TRIGGER_RESET
define TRIGGER_RESET
	$(strip\
		$(PROPLOAD) $(PROPLOAD_FLAGS)\
			-p $(TTY_PORT)\
			-b $(basename $(BOARD_CONFIG))\
			-o -r\
	)
endef


# Targets
#   all: same as kick-ram-terminal, with greeter
all : hello kick-ram-terminal
	@$(HEADLINE)\
	&& $(DONE);

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

#   display help information
help : $(ROOT_FW1_START)help.txt hello
	@$(HEADLINE)\
	&& $(CAT) $(CAT_FLAGS)\
		$<\
	&& $(DONE);

config-baud115200 : $(BOARD_CONFIG)
	@$(HEADLINE)\
	&& $(SED) $(SED_FLAGS)\
		-i\
		-e 's/^baudrate/#~ baudrate/;'\
		-e's/(\#~ )(baudrate: 115200)$$/\2/;'\
			$<\
	&& $(DONE);

config-baud57600 : $(BOARD_CONFIG)
	@$(HEADLINE)\
	&& $(SED) $(SED_FLAGS)\
		-i\
		-e 's/^baudrate/#~ baudrate/;'\
		-e's/(\#~ )(baudrate: 57600)$$/\2/;'\
			$<\
	&& $(DONE);

config-baud38400 : $(BOARD_CONFIG)
	@$(HEADLINE)\
	&& $(SED) $(SED_FLAGS)\
		-i\
		-e 's/^baudrate/#~ baudrate/;'\
		-e's/(\#~ )(baudrate: 38400)$$/\2/;'\
			$<\
	&& $(DONE);

#   reset chipflasher device, trigger up to three times
reset : $(BOARD_CONFIG) | $(TTY_PORT)
	@$(HEADLINE)\
	&& $(TRIGGER_RESET)\
	&& $(DONE);

#   send kick to EEPROM, don't run
kick-eeprom : $(BOARD_CONFIG) $(KICK_LMM_ELF) | $(TTY_PORT)
	@$(HEADLINE)\
	&& $(PROPLOAD) $(PROPLOAD_FLAGS)\
		-p $(TTY_PORT)\
		-b $(basename $(BOARD_CONFIG)):default\
		$(KICK_LMM_ELF)\
		-e\
	&& $(DONE);

#   send kick to RAM and run
kick-ram : $(BOARD_CONFIG) $(KICK_LMM_ELF) | $(TTY_PORT)
	@$(HEADLINE)\
	&& $(PROPLOAD) $(PROPLOAD_FLAGS)\
		-p $(TTY_PORT)\
		-b $(basename $(BOARD_CONFIG)):RAMstart\
		$(KICK_LMM_ELF)\
		-r\
	&& $(DONE);

#   send kick to RAM, run and start a propeller-terminal
kick-ram-terminal : $(BOARD_CONFIG) $(KICK_LMM_ELF) | $(TTY_PORT)
	@$(HEADLINE)\
	&& $(PROPLOAD) $(PROPLOAD_FLAGS)\
		-p $(TTY_PORT)\
		-b $(basename $(BOARD_CONFIG)):terminal\
		$(KICK_LMM_ELF)\
		-r\
		-t\
		-q\
	&& $(DONE);

clean-config : config-baud115200
	@$(HEADLINE)\
	&& $(DONE);

clean : clean-config
	@$(HEADLINE)\
	&& $(DONE);
