
export LC_ALL=C
ifeq ($(HOME),)
	export HOME=/
endif
ifeq ($(PLUGINDIR),)
	export PLUGINDIR=..
endif
CONFIGFILE= ../../config

include $(CONFIGFILE)
DEFINES_H=../defines.h
ifeq ($(CONFIG_STATIC),y)
CFLAGS := $(CFLAGS) -DSTATIC=1
endif
ifeq ($(CONFIG_LARGEFILES),y)
CFLAGS := $(CFLAGS) -DLARGEFILES=1
endif
ifeq ($(CONFIG_THREADS),y)
CFLAGS := $(CFLAGS) -DUSETHREADS=1  -D_REENTRANT=1
endif
EXAMPLEDIR=$(PLUGINDIR)/examples
GENERAL=$(PLUGINDIR)/plugin.cpp
HEADERS=$(PLUGINDIR)/faked.h  $(PLUGINDIR)/mainprocedures.h

LDRELOC=-Ur -d


#CFLAGS=
BIN2BYTE= ../../utils/bin2byte
PLUGINCFLAGSALL=-DPLUGINSRC=1
ifeq ($(CONFIG_DEBUG),n)
PLUGINLDFLAGS= -O3 -s
PLUGINCFLAGS= $(CFLAGS) $(OPTS) -O3 $(PLUGINCFLAGSALL)
else
ifeq ($(CONFIG_DEBUG),o)
PLUGINLDFLAGS= -O2 -g
PLUGINCFLAGS= $(CFLAGS) $(OPTS) -g -O2 $(PLUGINCFLAGSALL)

else
PLUGINCFLAGS= $(CFLAGS) $(OPTS) -g $(PLUGINCFLAGSALL)
PLUGINLDFLAGS= -g

endif
endif
#BEDSRCDIR=\"../../src\"
PLUGINCFLAGS := -I../ -I../../src/ -I../../src/empty  $(X_CFLAGS)   $(PLUGINCFLAGS) -DNEWTERM     -DBEDSRCDIR=$(BEDSRCDIR)
#PLUGINCFLAGS=-g
#PLUGINLDFLAGS=-s -O6
#PLUGINLDFLAGS= -O6
#PLUGINLDFLAGS= -g
SRC= ascii2ebcdic.h  bitcontains.cc bitcpy.cc bitfields.cc bitfields.h bitflags.cc char2.cc chars.c crc.c  duplicate.cpp ebcdic2ascii.cc  edit.cc  file.cpp filter.cpp float.cc disass.cc hash.h disass-use.c nextitem.cpp subtract.cc  signed.cc skip.cpp time.cpp translate.cc undoall.cpp unsigned.cpp  backward.cc oldsigned.cc bitfields.h hash.h oldsigned.h signed.h index2table.h title.cc helpf1.cc searchclip.cc newdata.cc install.cc datatypeinfo.cc cut.cc
TESTSRC= testall.cpp testbed.cpp testmode.cpp testsave.cpp testsearch.cpp  

DATATYPES=  char2.plug float.plug subtract.plug signed.plug time.plug unsigned.plug ebcdic2ascii.plug bitcontains.plug bitflags.plug index2table.plug elftables.plug oldsigned.plug newdata.plug
MISC= file.plug filter.plug disass.plug nextitem.plug undoall.plug  skip.plug duplicate.plug edit.plug  translate.plug backward.plug title.plug rxvt.plug helpf1.plug install.plug searchclip.plug datatypeinfo.plug cut.cc

ifeq ($(HAS_X),y)
MISC+= switchsize.plug putselect.plug
endif


TESTPLUG=  testall.plug testbed.plug testmode.plug testsave.plug testsearch.plug
PLUGINS= $(DATATYPES)  $(MISC)
GIVEOLD= time.plug ebcdic2ascii.plug skip.plug file.plug  duplicate.plug nextitem.plug 
GIVENEW= float.plug signed.plug  edit.plug  bitflags.plug bitcontains.plug translate.plug  backward.plug title.plug helpf1.plug searchclip.plug datatypeinfo.plug cut.plug


ifeq ($(USES_ELF),y)
GIVENEW+=index2table.plug elftables.plug rxvt.plug
SRC+=index2table.cc elftables.cc index2table.h rxvt.cc
endif
ifeq ($(HAS_X),y)
SRC+= switchsize.cc
GIVENEW+= switchsize.plug
	ifeq ($(CONFIG_THREADS),y)
	SRC+= putselect.cc
	GIVENEW+=putselect.plug 
	endif
endif

ifeq ($(CYGWIN),yes)
SRC+=clipboard.cc switchsize.cc
endif

PREGIVE= $(GIVEOLD) $(GIVENEW)
ifeq ($(CONFIG_PLUGIN),y)
GIVE= $(filter-out $(LINKIN) $(LINKINDYN),$(PREGIVE))
else
GIVE=
endif
#GIVE= $(filter signed.plug,$(PREGIVE))
INLINK=$(LINKIN:%.plug=%.oo)
INLINKSRC=$(filter $(wildcard *.cc *.cpp *.CPP *.CC), $(wildcard $(LINKIN:%.plug=%.c[cp]*)) )
INLINKDYN=$(LINKINDYN:%.plug=%.ooo)
INLINKDYNSRC=$(filter $(wildcard *.cc *.cpp), $(wildcard $(LINKINDYN:%.plug=%.c[cp]*)) )

#INLINKSRC=$(patsubst %.plug,%.cc %.cpp,$(LINKIN))
EXAMPLES=  $(filter-out $(LINKIN) $(LINKINDYN),$(PLUGINS))
INSTALL=install
GENERATED= ascii2ebcdic.h ebcdic2ascii.h ebcdic2ascii.cc

ALLSRC=Makefile $(SRC) $(GENERATED) encodechar.sh macro2plug.sh

ALLFILES=$(ALLSRC) $(PLUGINS)
GIVEALL=$(ALLSRC) $(GIVE) 
INLINKALL= $(patsubst bitcontains.%,bitcontains.% bitcpy.o bitfields.o, $(INLINK) $(INLINKDYN) )
all:   $(EXAMPLES)
give:  $(GIVE)  
data:$(DATATYPES)
INBEDRC=$(filter-out switchsize.plug rxvt.plug putselect.plug,$(GIVE))
try:
	@echo EXAMPLES: $(EXAMPLES)
	@echo GIVENEW: $(GIVENEW)	
	@echo give: $(GIVE)	
	@echo linkin: $(LINKIN)
	@echo LINKIN: $(INLINK)
	@echo LINKINDYN: $(INLINKDYN)
	@echo INLINKSRC: $(INLINKSRC)
	@echo INLINKSRC: $(INLINKDYNSRC)
ingive: Makefile
	@echo -e " $(INBEDRC:%.plug=plugin %.plug\n)" > ingive
#	@echo -e " $(GIVE:%.plug=plugin %.plug\n)" > ingive
	@echo -e "\nkeys 27 91 49 49 126 = f1\nkeys 27 91 91 65 = f1" >> ingive

#	sed --silent -e "{s/^[ 	]*call([ 	]*\([^),; 	.*/\\]*\)[ 	]*)/prodef(\1)/p; s/^[ 	]*callname([ 	]*\([^),; 	.*/\\]*\)[ 	]*)/prodefname(\1)/p; }" $(INLINKSRC) >  $@

../../src/inlink.a: $(INLINKALL) 
	-rm $@
	ar rsc $@ $(INLINKALL)   

../../src/inlink.o: $(INLINKALL) $(CONFIGFILE)
	ld -o $@ $(INLINKALL)   $(LDRELOC)
#grep 'will_be_mentioned_in_procedures_h_.*_var_to_export_it'|
LINKINCCCPP= $(patsubst %.cc,%.ii, $(filter %.cc,$(INLINKSRC)  ))
LINKINCPPCPP= $(patsubst %.cpp,%.ii, $(filter %.cpp,$(INLINKSRC)  ))
LINKINCPP=$(LINKINCCCPP) $(LINKINCPPCPP)


#	grep 'int (Editor::\*will_be_mentioned_in_procedures_h_\([^)]*\)_var_to_export_it)(void)=\&Editor::' $(LINKINCPP)> uitfile

# int (Editor::*for_procedure_h_##proc##_prodefname)(void)=&Editor::proc;char name_part_##proc##_end[]=name;

#	grep -h 'for_procedures_h_\([^)]*\)_prodef' $(LINKINCPP)
tmpmainprocedures.h:
	cp ../../src/procedures.h
inlink.h: $(LINKINCPP)  $(CONFIGFILE) 
	grep -h 'int (Editor::\*for_procedures_h_\([^)]*\)_prodef.*)(void)=\&Editor::\([^;]*\);' $(LINKINCPP)|sed '{s/int (Editor::\*for_procedures_h_\([^)]*\)_prodef)(void)=\&Editor::\([^;]*\);/prodef(\1)/g;s/int (Editor::\*for_procedures_h_\([^)]*\)_prodefname)(void)=\&Editor::\([^;]*\);char name_part_\([^)]*\)_end\[\]=\([^;]*\);/prodefname(\1,\4)/g;}'  > inlink.h

##define callname(name,proc)  int (Editor::*for_procedures_h_##proc##_prodefname)(void)=&Editor::proc;char name_part_##proc##_end[]=name;
#../../src/inlink.h: $(INLINKIN)  $(CONFIGFILE) 
#	sed  '{s/^[ 	]*call([ 	]*\([^),; 	.*/\\]*\)[ 	]*)/prodef(\1)/p; s/^[ 	]*callname([ 	]*\([^),; 	.*/\\]*\)[ 	]*)/prodefname(\1)/p; s/^.*//p; }' $(INLINKSRC) >  $@
#	$(GCC) $(PLUGINCFLAGS) -DPLUGIN -x c   -E -P -D"call(x)=uniekvoorinlinkhprodef(x)" -D"callname(name,x)=uniekvoorinlinkhprodefname(name,x)"   $(INLINKSRC)> tussengegs

bitfields.plug: bitfields.o bitcpy.o
	echo hello
	$(GCC) $(PLUGINLDFLAGS) -shared  $^ -o $@ 
	chmod a-x $@
bitfields.o: bitfields.cc
	$(GCC) -DNOEDITOR -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@

bitcontains.plug: bitcontains.o bitcpy.o bitfields.o
	$(GCC) $(PLUGINLDFLAGS) -shared  $^ -o $@ 
	chmod a-x $@


%.plug: %.o
	$(GCC) $(PLUGINLDFLAGS) -shared $< -o $@
	chmod a-x $@
	rm $<

%.o: %.cpp  $(GENERAL) $(HEADERS) absolute 
	$(GCC) $(PLUGINCFLAGS) -DPLUGIN=1 -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $@

%.o: %.cc $(HEADERS) absolute 
	$(GCC) -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@

ifneq ($(CYGWIN),yes)
XINCL=$(X_CFLAGS)
endif
switchsize.o: switchsize.cc $(HEADERS) absolute
	$(GCC) $(XINCL) -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@
%.i: %.cc $(HEADERS) absolute
	$(GCC)  -DGLOBAL -DUNDER -DPLUGIN $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E -P $< -o $@

%.oo: %.cc  $(HEADERS) absolute 
	$(GCC)  -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@

#bitcontains.oo: bitcontains.oo bitcpy.oo bitfields.oo
#	ld -o $@ $^   $(LDRELOC)

#bitcontains.ooo: bitcontains.ooo bitcpy.ooo bitfields.ooo
#	ld -o $@ $^ $(LDRELOC)


%.ooo: %.cc  $(HEADERS) absolute 
	$(GCC) -DFAKE_DYNAMIC=1  -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@
%.ooo: %.cpp  $(GENERAL) $(HEADERS) absolute
	$(GCC) -DFAKE_DYNAMIC $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $@
%.oo: %.cpp  $(GENERAL) $(HEADERS) absolute
	$(GCC) $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $@

%.iii: %.cc  $(HEADERS) absolute $(CONFIGFILE)
	$(GCC) -P  -DFAKE_DYNAMIC  -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $< -o $@
%.iii: %.cpp  $(GENERAL) $(HEADERS) absolute
	echo hallo
	$(GCC) -P -DFAKE_DYNAMIC $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $(GENERAL) -o $@
	$(GCC) -DFAKE_DYNAMIC $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $*.ooo


%.ii: %.cc  $(HEADERS) absolute $(CONFIGFILE)
	$(GCC)  -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $< -o $@
#	$(GCC) '-Dwill_be_used_to_get_prodefname_in_inlink_h(x,y)=prodefname(x,y)' '-Uwill_be_used_to_get_prodef_in_inlink_h(x)=prodef(x)' -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $< -o $@
#	$(GCC) '-Uwill_be_used_to_get_prodefname_in_inlink_h' '-Uwill_be_used_to_get_prodef_in_inlink_h' -DGLOBAL -DUNDER $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $< -o $@

%.ii: %.cpp  $(GENERAL) $(HEADERS) absolute
	$(GCC)  -P $(PLUGINCFLAGS)  -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E $(GENERAL) -o $@
%.s: %.cpp  $(GENERAL) $(HEADERS) absolute
	$(GCC)  $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -S $(GENERAL) -o $@
disass-use: disass-use.c absolute
	$(CC) -g disass-use.c -o disass-use
absolute: 
	cd $(EXAMPLEDIR) ;pwd>absolute;cd $(CURRENT)
install: $(CONFIGFILE)
	-mkdir -p $(ROOTDIR)$(EXAMPLEDIR)
	cp $(GIVEALL) $(ROOTDIR)$(EXAMPLEDIR)
	(CURDIR=`pwd`;cd $(ROOTDIR)$(EXAMPLEDIR);chmod 644 $(ROOTDIR)$(GIVEALL); cd $$CURDIR)

#	-rm $(EXAMPLEDIR)/disass-use
#	$(MAKE) -C  $(EXAMPLEDIR) disass-use
#	-rm $(EXAMPLEDIR)/disass.plug 
#	$(MAKE) -C  $(EXAMPLEDIR) disass.plug

%.i: %.cpp  $(GENERAL) $(HEADERS) absolute
	$(GCC) -DPLUGIN $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -E -P $(GENERAL) -o $@

clean:
	-rm *.plug *.o *.oo *.ooo disass-use$(EXEEXT) absolute core ingive *.ii *.i *.iii

realclean: clean
	-rm  chars.bin  mkhelp$(EXEEXT) mkman$(EXEEXT)
	-rm plugman plughelp inlinklist inlink.h mkhelp.h mkman.h
ifneq ($(CYGWIN),yes)
	-rm chars
else
	-rm chars.exe
endif

switchsize.plug: switchsize.o
	$(GCC) $(PLUGINLDFLAGS) -shared $< -o $@ $(X_LIBS) -lX11 $(X_EXTRA_LIBS)
	chmod a-x $@
putselect.plug: putselect.o
	$(GCC) $(PLUGINLDFLAGS) -shared $< -o $@  $(X_LIBS) -lX11 $(X_EXTRA_LIBS)
	chmod a-x $@
switchsize: switchsize.cc
	echo $(CYGWIN)
ifneq ($(CYGWIN),yes)
	$(GCC) -g -DTEST  $< -o $@ $(X_LIBS) -lX11 $(X_EXTRA_LIBS)
else
	$(GCC) -D_WIN32 -g -DTEST  $< -o $@ 
endif

disass.plug: disass.o disass-use
	$(GCC) $(PLUGINLDFLAGS) -shared $< -o $@
	chmod a-x $@
time: time.cpp
	$(GCC) -g -DTEST -DGLOBAL $< -o $@  $(X_LIBS)  -lX11  $(X_EXTRA_LIBS)


chars.bin: chars.c
	$(CC)  chars.c -o chars
	./chars > chars.bin
ifneq ($(CYGWIN),yes)
	rm chars
else
	rm chars.exe
endif

ebcdic2ascii.cc: ascii2ebcdic.h ebcdic2ascii.h ./encodechar.sh 
	sh ./encodechar.sh ebcdic ascii
ebcdic2ascii.h: 
	dd if=chars.bin of=ebcdic2a.sci bs=256 count=1 conv=ascii 
	$(BIN2BYTE) ebcdic2a.sci ebcdic2ascii ebcdic2ascii.h 
	rm ebcdic2a.sci
ascii2ebcdic.h:
	dd if=chars.bin of=ascii2eb.cdi bs=256 count=1 conv=ebcdic 
	$(BIN2BYTE) ascii2eb.cdi ascii2ebcdic  ascii2ebcdic.h 
	rm  ascii2eb.cdi


MACROFUNCTION=MacroFuctionNameShouldBeLongerThenEveryUsedFunctioName
MACRODATA=12345678BeginMacroKeyStrokes
MACROFLAGS=-DMACROFUNCTION=\"$(MACROFUNCTION)\" -DMACRODATA=\"$(MACRODATA)\"
#macrotemplate.plug: macrotemplate.cpp Makefile ./fsize $(CONFIGFILE)
#	$(GCC) $(PLUGINLDFLAGS) -DPLUGIN -shared $(MACROFLAGS) $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\"  $(GENERAL) -o $@

#macrotemplate.o: macrotemplate.cc Makefile ./fsize $(CONFIGFILE)
#	$(GCC)  -DGLOBAL -DUNDER $(MACROFLAGS) $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $< -o $@ 
#macrotemplate.plug: macrotemplate.o
#	ld -shared  $< -o $@
macrotemplate.plug: macrotemplate.cc Makefile ./fsize $(CONFIGFILE)
	$(GCC) -shared -DGLOBAL -DUNDER $(MACROFLAGS) $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\"  $< -o $@
 
#	$(GCC) $(PLUGINLDFLAGS) -DPLUGIN -shared $(MACROFLAGS) $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" $< -o $@


macrotemplate.i: macrotemplate.cpp Makefile 
	$(GCC) $(PLUGINLDFLAGS)  -DPLUGIN -E -shared $(MACROFLAGS) $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\"  $(GENERAL) -o $@

places.h: macrotemplate.plug pos
	echo -n "int macrofunctionpos=" > places.h
	./pos macrotemplate.plug $(MACROFUNCTION) >>places.h
	echo ";" >>places.h
	echo -n "int macrodatapos=" >> places.h
	./pos macrotemplate.plug $(MACRODATA) >>places.h
	echo ";" >>places.h


macroplug.o: macroplug.cpp  $(GENERAL) $(HEADERS) absolute places.h $(CONFIGFILE)
	$(GCC) $(PLUGINCFLAGS) -DPLUGIN -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $@
macroplug.oo: macroplug.cpp  $(GENERAL) $(HEADERS) absolute places.h $(CONFIGFILE)
	$(GCC) $(PLUGINCFLAGS) -DEXAMPLEDIR=\"`cat absolute`\" -DSOURCEFILENAME=\"examples/$<\" -c $(GENERAL) -o $@

../mainprocedures.h:
	$(MAKE) -C .. mainprocedures.h

plughelp: mkhelp 
	./mkhelp > plughelp
mkhelp: mkhelp.h mkhelp.c
	gcc -DHELP_H='"mkhelp.h"' mkhelp.c -o mkhelp
mkhelp.h: $(INLINKSRC)  mkhelp.sh $(CONFIG)
	sh ./mkhelp.sh  $(INLINKSRC)  > mkhelp.h

plugman: mkman 
	./mkman > plugman
mkman: mkman.h mkhelp.c
	gcc -DHELP_H='"mkman.h"' mkhelp.c -o mkman
mkman.h: $(SRC) mkhelp.sh 
	sh ./mkhelp.sh $(filter-out $(INLINKSRC),$(SRC)) > mkman.h

#|sed -e 's/helpteksten(\([^ 	,]*\),[^"]*"\([^"]*\)".*/\1 \2/g'|sed -e 's/\\n/newline\/g' > mkhelp.h
head:	
	echo $(PLUGINDIR)
	echo $(CFLAGS)
	echo $(LDFLAGS)
	echo $(HEADERS)
	echo $(PLUGINCFLAGS) 

.PHONY: compileinlink makeinlinklist mkinlink_h
compileinlink: $(INLINKALL)
mkinlink_h:  absolute inlink.h
inlinklist: $(CONFIG) inlink.h
	echo $(INLINKALL) > inlinklist
makeinlinklist:
	echo $(INLINKALL) > inlinklist
dep: mkinlink_h makeinlinklist
cds:
	(CURDIR=`pwd`;cd $(ROOTDIR)$(CONFDIR);pwd; cd $$CURDIR;pwd)
