################################################################################
# CMD Builder
################################################################################

#CMD Builder tools
AI_CMD_BUILDER := $(TOOLS_DIR)/cmdbuilder.c
AI_CMD_LIST_DEFINITIONS := src/aicommands.def
AI_CMD_LIST_TEMP := $(BUILD_DIR)/aicommands.temp
AI_CMD_LIST_H2 := src/aicommands2.h
#Pre-Format encoding newlines and tags
AI_CMD_BUILDER_PRECONVERT := sed -E
AI_CMD_BUILDER_PRECONVERT += -e 's/\x27\\n\x27/\x27\/n\x27/g;' #                                                                     encode '\n' as '/n' (TEXT PRINT command)
AI_CMD_BUILDER_PRECONVERT += -e '/^\s*\*/ s/\x27/\?\?x27/g;' #                                                                       encode DocBlock(/**...**/) quotes '' as x27
AI_CMD_BUILDER_PRECONVERT += -e '/\/\*\*/ , /\*\// s/([^/])$$/\1 \\n\\/g;' #                                                         encode DocBlock(/**...**/) Newlines as \n\ .
#AI_CMD_BUILDER_PRECONVERT += -e '/^\s*\*/ s/([^\/])$$/\1 \\n\\/g;' #                                                                encode DocBlock(/**...**/) Newlines as \n\ .
AI_CMD_BUILDER_PRECONVERT += -e 's/[^/\*\S]\*$$/\\n\\/g; s/^(\s*)\*[^/\*]/\1  /g;' #                                                 remove DocBlock sides (causes problems with tooltips) fixed to check for ordinary comment
#AI_CMD_BUILDER_PRECONVERT += -e '/^_AI_CMD_POLYMORPH\(CMDNAME,\n(.|\n)*?\s{19}DESCRIPTION(\)| DESCRIPTION2\))/ s/[^,]\n/\\n\\/g;' # Newlines in POLYMORPHS
AI_CMD_BUILDER_PRECONVERT += -e 's/\/\*\*/\?\?\\\*\(\*\*/g;' #                                                                       encode /** as ??\*(**
AI_CMD_BUILDER_PRECONVERT += -e 's/\*\*\//\*\*\)\*\?\?\\ /g;' #                                                                      encode **/ as **)*??\

#Format Newlines and Comment tags
AI_CMD_BUILDER_CONVERT := sed -E
AI_CMD_BUILDER_CONVERT += -e 's/\\n/\n/g;' #                                                                                         add newlines
AI_CMD_BUILDER_CONVERT += -e 's/\?{2}\=/\#/g;' #                                                                                     replace ??= with hash for defines
AI_CMD_BUILDER_CONVERT += -e 's/\?{2}\\/\//g;' #                                                                                     replace ??\ with /
AI_CMD_BUILDER_CONVERT += -e 's/DEFINE([^D])/\#define\1/g;' #                                                                        replace DEFINE with #define
AI_CMD_BUILDER_CONVERT += -e 's/\\.*/\\/g;' #                                                                                        replace \... with \ (line continuation)
AI_CMD_BUILDER_CONVERT += -e 's/\*\(\*/\*/g;' #                                                                                      replace *(* with *
AI_CMD_BUILDER_CONVERT += -e 's/\*\)\*/\*/g;' #                                                                                      replace *)* with *
AI_CMD_BUILDER_CONVERT += -e 's/MAKE_EXPAND\(([^\n]*)MAKE_EXPAND_END\)/\1/g;' #                                                      replace MAKE_EXPAND(CONTENTS) with CONTENTS
AI_CMD_BUILDER_CONVERT += -e 's/\?{2}x27/\x27/g;' #                                                                                  replace ??x27 with '
AI_CMD_BUILDER_CONVERT += -e 's/\x27\/n\x27/\x27\\n\x27/g;' #                                                                        replace '/n' with '\n'
AI_CMD_BUILDER_CONVERT += -e 's/AI_EndList\s*,/AI_EndList/g;' #                                                                      replace AI_EndList , with AI_EndList
AI_CMD_BUILDER_CONVERT += -e 's/ *(,|;) */\1 /g; ' #                                                                                 remove spaces
AI_CMD_BUILDER_CONVERT += -e '/define (AICMDSIZE)|(AI.*_LENGTH)/! s/define ([^ ]*) *\( *([^ ]*)/define \1(\2/g; ' #                  for each command, remove the space so macro is read as a function
AI_CMD_BUILDER_CONVERT += -e '/^typedef/ s/\}/\n\}/g; /^typedef/ s/(; |\{)/\1\n     /g;' #                                           for each typedef, break into ALLMAN
AI_CMD_BUILDER_CONVERT += -e '/^typedef/ s/(\{)/\n\1/g; /^\s*AI_/ s/,/,\\\n                   /g' #                                  for each command, break into a list
AI_CMD_BUILDER_CONVERT += -e '/^ *$$/d; s/ *\n/\n/g;' #                                                                              remove surplus newlines

AI_CMD_LIST_H2_HEADER := \
"/******************************************************************************\n\
*                                                                            *\n\
*                                                                            *\n\
*  Do not edit this file.  It was automatically generated by \"cmdbuilder\"    *\n\
*  from the file \"$(AI_CMD_LIST_DEFINITIONS)\".                                       *\n\
*  To Add/Remove/Modify AI Commands please edit \"$(AI_CMD_LIST_DEFINITIONS)\"         *\n\
*  and then run                                                              *\n\
*     make cmdbuilder                                                        *\n\
*                                                                            *\n\
*                                                                            *\n\
*****************************************************************************/\n\n"


cmdbuilder:
	@clear
	@echo
	@echo Building AI Command Macros...
	@echo
	@$(call SetupProgressBar)
	@$(call DrawProgressBar,0)
	@ # copy command definitions to temp
	@cp $(AI_CMD_LIST_DEFINITIONS) $(AI_CMD_LIST_TEMP)
	@$(call DrawProgressBar,5)
	@ # Preformat Definitions for builder (encode documentation tags)
	@$(call PRINT_STATUS,"Pre Formatting",$(AI_CMD_LIST_DEFINITIONS))
	@$(AI_CMD_BUILDER_PRECONVERT) $(AI_CMD_LIST_TEMP) > $(AI_CMD_LIST_DEFINITIONS)
	@$(call DrawProgressBar,10)
	@ # Print Header
	@echo $(AI_CMD_LIST_H2_HEADER) > $(AI_CMD_LIST_H2)
	@$(call DrawProgressBar,11)
	@ # Execute Builder and format (re-add newlines, documentation tags etc) -C keeps /**/ comments
	@$(call PRINT_STATUS,"Processing",$(AI_CMD_LIST_DEFINITIONS))
	@echo This might take some time...
	@$(CC) -Xcpluscomm -c $(AI_CMD_BUILDER) $(INCLUDE) -w 581 -E | $(AI_CMD_BUILDER_CONVERT) >> $(AI_CMD_LIST_H2) || (cp $(AI_CMD_LIST_TEMP) $(AI_CMD_LIST_DEFINITIONS) ; echo Error with cmdbuilder ; false) & $(call IncrementProgressBarFromAtRate,12,0.5)
	@$(call DrawProgressBar,98)
	@ # restore command def from temp (no encoding)
	@cp $(AI_CMD_LIST_TEMP) $(AI_CMD_LIST_DEFINITIONS)
	@$(call DrawProgressBar,99)
	@rm $(AI_CMD_LIST_TEMP)
	@$(call DrawProgressBar,100)
	@echo
	@echo Done!
	@echo
	@echo Rebuild AI Command Macros whenever changing aicommands.def.
	@echo "\n$(BELL)$(RESTORESCROLLREGION2)"

