#include $(COREPATH)/Makefile.defs


# output directory for generated txt files
# output directory for generated docbook xml files

# list of files containing cfg defs in the following format:
# <filename>:<cfg_grp_name> 
# can be easily updated by adding the output of:
#   make diff-list   (which obeys grp_exclude and file_exclude)
# or completely regenerated by replacing files_list with the output of:
#   make gen-files-list
files_list= \
	$(COREPATH)/cfg_core.c:core \
	$(COREPATH)/sctp_options.c:sctp \
	$(COREPATH)/tcp_options.c:tcp \
	$(COREPATH)/modules/tm/config.c:tm \
	$(COREPATH)/modules/registrar/config.c:registrar \
	$(COREPATH)/modules/siputils/config.c:siputils \
	$(COREPATH)/modules/maxfwd/maxfwd.c:maxfwd \
	$(COREPATH)/modules/carrierroute/config.c:carrierroute \
	$(COREPATH)/modules/malloc_test/malloc_test.c:malloc_test \
	$(COREPATH)/modules/tls/tls_cfg.c:tls \
	$(COREPATH)/modules/dispatcher/config.c:dispatcher \
	$(COREPATH)/modules/websocket/config.c:websocket \
	$(COREPATH)/modules/outbound/config.c:outbound \

# list of excluded groups
# list of file prefixes to exclude (full path needed)
file_exclude= $(COREPATH)/modules/tls/

# special per file group overrides
# format= grp_filename=... ,where filename does not contain the extension
# e.g.:
#     grp_f_tcp_options=tcp
#     grp_f_sctp_options=sctp

# special per group group name overrides
# e.g.:
#      grp_g_maxfwd=mf

# override auto-detected group if set to 1 (else the group is used inside the
# file only if it cannot be aut-odetected)
ifeq ($(group_override),1)
override force_grp=force-
override force_grp=

# command used for gcc (contains extra includes)
#-I$(COREPATH)/lib -I$(COREPATH) -I/usr/include/libxml2

# defines used by gcc
c_defs=-D__CPU_i386 -D__OS_linux -DSER_VER=2099099 -DPKG_MALLOC -DSHM_MEM  \
		-DVERSION='\"2.99.99-pre3\"' -DARCH='\"i386\"' -DOS_QUOTED='\"linux\"'

# common makefile vars used in defs

filter_files=$(filter-out $(addsuffix %,$(file_exclude)),\
				$(filter-out $(addprefix %:,$(grp_exclude)),$(1)))

#filtered files list
flist=$(call filter_files,$(files_list))

# throws an error if input is not in the format filename:grp
check_fname_grp=$(if $(filter-out 2,$(words $(subst :, ,$(1)))),\
					$(error bad format "$(1)", it should be filename:grp))

# get prereq from file:grp (get_prereq(file:grp) => file)
get_prereq=$(firstword $(subst :, ,$(1)))

# get grp from file:grp (get_grp(file:grp) => grp)
get_listed_grp=$(word 2, $(subst :, ,$(1)))

# get base file name from file:grp: get_bname(file:grp) 
# => basename(file) without extension (e.g. get_bname(foo/bar.c:x) => bar)
get_bname=$(basename $(notdir $(call get_prereq,$(1))))

#get grp from file:grp, using the overrides
get_grp=$(strip $(if $(grp_f_$(call get_bname,$(1))), \
					$(grp_f_$(call get_bname,$(1))),\
					$(if $(grp_g_$(call get_listed_grp,$(1))),\
						$(grp_g_$(call get_listed_grp,$(1))),\
						$(call get_listed_grp,$(1))) ) )

# get target from file:grp (get_target(file:grp) => cfg_grp.txt)
get_target=cfg_$(call get_grp,$(1))

# $(LF) definition (do not remove)
define LF


# get all the lines containing DEFS or INCLUDES definitions from the Makefile.
# WARNING: does not work with all sed implementation (tested with GNU sed).
# It uses a hack to restore the LFs (LFs are removed by $(shell)): LFs are
# replaced with '^LF^' and then ^LF^ is subst'ed back to a real LF.
get_make_idefs=$(subst ^LF^,$(LF),$(shell sed \
 -ne '/^[\t ]*\(DEFS\|INCLUDES\)[\t ]*[+:]\?=.*[^\]$$/H'\
 -ne '/^[\t ]*\(DEFS\|INCLUDES\)[\t ]*[+:]\?=.*\\$$/,/\(^$$\)\|\([^\]$$\)/H'\
 -ne '$${g;s/\n/^LF^/g;p}'\
< $(1)/Makefile ))

# get all the lines from the makefile containing variable definitions.
# It will also return conditionals and try to filter out possible rules.
# WARNING: does not work with all sed implementation (tested with GNU sed).
# It uses a hack to restore the LFs (LFs are removed by $(shell)): LFs are
# replaced with '^LF^' and then ^LF^ is subst'ed back to a real LF.
get_make_vars=$(subst ^LF^,$(LF),$(shell sed -n \
 -e ': start' \
 -e '/^\(ifeq\|ifneq\|else\|endif\)[\t ]*\($$\|.*[^\]$$\)/{H;b end}' \
 -e '/^\(ifeq\|ifneq\|else\|endif\)[\t ]\+.*[\]$$/,/[^\]$$/{H;b end}' \
 -e '/^[a-zA-Z._/$$][a-zA-Z0-9._()/$$ \t-]*:\([^=]\|$$\)/b eat_rule' \
 -e '/^[\t ]*[A-Za-z._][A-Za-z0-9._-]*[\t ]*[+:]\?=.*[^\]$$/{H;b end}' \
 -e '/^[\t ]*[A-Za-z._][A-Za-z0-9._-]*[\t ]*[+:]\?=.*\\$$/,/\(^$$\)\|\([^\]$$\)/{H;b end}' \
 -e ': end' \
 -e '$${g;s/\n/^LF^/g;p}'\
 -e 'b' \
 -e ': eat_rule' \
 -e '$$b end' \
 -e 'n' \
 -e '/^[a-zA-Z._/$$][a-zA-Z0-9._()/$$ \t-]*:\([^=]\|$$\)/b eat_rule' \
 -e '/^[\t]/b eat_rule' \
 -e 'b start' \
< $(1)/Makefile ))

define  mk_rules

$(call check_fname_grp, $(1))

#$$(info generating cfg_$$(call get_grp,$(1)).txt: $$(call get_prereq,$(1)))

# extract all the includes and defs from the module makefile and
# evaluate them
$$(eval $$(call get_make_vars,$$(dir $$(call get_prereq,$(1)))))
# override COREPATH (we know better)
# save the result in a per group e_idefs_<grp_name> var
$$(eval e_idefs_$$(call get_grp,$(1)):=$$(DEFS) $$(INCLUDES))

# debugging:
#$$(info eval: $$(call get_make_vars,$$(dir $$(call get_prereq,$(1)))))
#$$(info e_idefs_$$(call get_grp,$(1))=$$(e_idefs_$$(call get_grp,$(1))))

$(txt_output_dir)/$$(call get_target,$(1)).txt: \
								$$(call get_prereq,$(1)) Makefile $(CFG2TXT)
	$(CFG2TXT) --file $$< --$(force_grp)grp=$$(call get_grp,$(1)) \
		--gcc="$(gcc)" --txt \
		--defs="$(c_defs) $$(e_idefs_$$(call get_grp,$(1)))" \
		> "$$@" || (rm -f "$$@"; exit 1)

$(docbook_output_dir)/$$(call get_target,$(1)).xml: \
								$$(call get_prereq,$(1)) Makefile $(CFG2TXT)
	$(CFG2DOCBOOK) --file $$< --$(force_grp)grp=$$(call get_grp,$(1)) \
		--gcc="$(gcc)" --docbook \
		--defs="$(c_defs) $$(e_idefs_$$(call get_grp,$(1)))" \
		> "$$@" || (rm -f "$$@"; exit 1)

clean_$$(call get_target,$(1)).txt:
	rm -f "$(txt_output_dir)/$$(call get_target,$(1)).txt"

clean_$$(call get_target,$(1)).xml:
	rm -f "$(docbook_output_dir)/$$(call get_target,$(1)).xml"

txt: $(txt_output_dir)/$$(call get_target,$(1)).txt

docbook: $(docbook_output_dir)/$$(call get_target,$(1)).xml

clean_txt: clean_$$(call get_target,$(1)).txt

clean_docbook: clean_$$(call get_target,$(1)).xml


find_cfg_files_cmd= find $(COREPATH) -type f -name "*.c" \
		-exec grep "cfg_def_t[	 ][a-zA-Z0-9_]*\[\][	 ]*=" /dev/null {} \; \
		| cut -d: -f1

# shell command to generate a file:grp list from a list of files
# grp will be the modulename if the file is in a module directory or
# the file name with the extension and _cfg, cfg_ or _options stripped out of
# it.
# output: list of  " "filename":"grpname
sed -e "s!\(.*/modules[^/]*/\([^/][^/]*\)/.*\)! \1:\2!" \
    -e "s!^\([^ ].*/\([^/.]*\)[^/]*$$\)!\1:\2!" \
    -e "s!^\([^ :]*\):\(.*\)_cfg[_]*!\1:\2!" \
    -e "s!^\([^ :]*\):\(.*\)cfg[_]*!\1:\2!" \
    -e "s!^\([^ :]*\):\(.*\)_options[_]*!\1:\2!"

# special vars for generating the list of files or updates
found_lst=$(shell $(find_cfg_files_cmd) | $(gen_file_grp_cmd))
# filtered found lst
f_found_lst=$(call filter_files,$(found_lst))
diff_lst=$(filter-out $(flist),$(f_found_lst))

get_core_files=$(filter-out $(COREPATH)/modules% $(COREPATH)/lib%,$(1))
sort_files=$(sort $(call get_core_files,$(1)))\
			$(sort $(filter-out $(call get_core_files,$(1)),$(1)))

# replace $(COREPATH) with the text "$(COREPATH)"
subst_corepath=$(patsubst $(patsubst %/,%,$(COREPATH))/%,\$$(COREPATH)/%,$(1))

# help will be the default rule (on-purpose since without having a patched
# GCC:TranslationUnit module, make all won't work)
.PHONY: help
	@echo "To regenerate $(foreach f,$(flist),$(call get_target,$f).{txt,xml})"
	@echo "type: $(MAKE) all ."
	@echo "or to regenerate all the cfg documentation by searching all"
	@echo " the source files for definitions, type: $(MAKE) autogen ."
	@echo "NOTE: you need the GCC:TranslationUnit perl module with an "
	@echo "extra patch applied (see $(CFG2TXT) --patch)."

.PHONY: txt

.PHONY: docbook

.PHONY: clean_txt

.PHONY: clean_docbook

.PHONY: all
all: txt $(docbook_output_dir)/cfg_var_list.xml

.PHONY: clean
clean: clean_txt clean_docbook
	@rm -f $(docbook_output_dir)/cfg_var_list.xml

.PHONY: proper
	@rm -f $(txt_output_dir)/cfg_*.txt
	@rm -f $(docbook_output_dir)/cfg_*.xml

	"git-$(shell  git rev-parse --verify --short=6 HEAD 2>/dev/null)"
ifeq ($(repo_ver),git-)
repo_ver="sip-router unknown"

$(docbook_output_dir)/cfg_var_list.xml: Makefile \
		$(foreach f,$(flist),$(docbook_output_dir)/$(call get_target,$f).xml)
	@echo '<?xml version="1.0" encoding="UTF-8"?>' >$@
	@echo '<!-- this file is autogenerated, do not edit! -->' >>$@
	@echo '<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"' >>$@
	@echo '	""' >>$@
	@echo '	[ <!ENTITY % local.common.attrib' >>$@
	@echo "	 \"xmlns:xi CDATA #FIXED ''\">]">>$@
	@echo '>' >>$@
	@echo '<book id="cfg_var_list"'\
		'xmlns:xi="">' >>$@
	@echo '	<title>Runtime Configuration Variables List</title>' >>$@
	@echo '	<bookinfo><revhistory><revision>' >>$@
	@echo '		<revnumber>'$(repo_ver)'</revnumber>' >>$@
	@echo '		<date>'`date -R`'</date>' >>$@
	@echo '		<revremark>' >>$@
	@echo "			Automatically generated by:">>$@
	@echo "			$(MAKE) -C doc/cfg_list $(MAKECMDGOALS)" >>$@
	@echo '		</revremark>' >>$@
	@echo '	</revision></revhistory></bookinfo>' >>$@
	@$(foreach f,$(flist),\
		echo '		<xi:include'\
			'href="'$(call get_target,$f).xml'"/>' \
			>>$@ ; )
	@echo '</book>' >>$@

# finds all the files containing cfg defs
.PHONY: find

# print the list of the autogenerated files
.PHONY: print-lst
	@$(find_cfg_files_cmd) | $(gen_file_grp_cmd)

.PHONY: gen-file-list
.PHONY: gen-files_list
.PHONY: gen_files_list
gen-file-list gen-files-list gen_files_list:
	@$(foreach f,$(call subst_corepath,$(call sort_files,$(f_found_lst))),\
		echo "$f \\";)

.PHONY: check-list
.PHONY: update-list
.PHONY: diff-list
check-list update-list diff-list:
	@$(foreach f,$(call subst_corepath,$(call sort_files,$(diff_lst))),\
		echo "$f \\";)

# try to generate the docs from all the sources
.PHONY: autogen
	@$(MAKE) all files_list="$(call sort_files,$(f_found_lst))"

$(foreach f,$(flist),$(eval $(call mk_rules,$(f))))