include rules.mk
ifeq ($(PDCURSES)$(CYGWIN),1yes)
WINPD=y
endif
LIBS+= $(CURSESLIB) $(EXTRALIBS) $(OTHERLIBS)# -L/mnt/chicken/lib -L/mnt/chicken/usr/lib
CFLAGS+=  -D_GNU_SOURCE -DNEWTERM #-DUSE_NEW # -DMEMORY_TRACE	#-DREAD0x2x4DATATYPE #-DSEARCHTIME #-DUSE_NEW #-DMEMORY_TRACE	

OPTS:= $(OPTS) 
LIBPATH=-L. 
INLINKLISTER= ../plugins/examples/inlinklist
INLINKLIST= compileinlink
MKINLINK_H=mkinlink_h
ININLINKLIB= $(patsubst %,../plugins/examples/%,$(shell cat $(INLINKLISTER) ))
LIBS:= $(LIBS)
CLEANOBJECTS=$(OBJECTS)
OBJECTS+=$(ININLINKLIB)
SORT=sort 
CP=cp -f -b
SHELL=bash
BIN2BYTE= ../utils/bin2byte
MV=mv

CFLAGS:= -Wall $(CFLAGS) -DSETSCRREGBUG # -DALWAYSEXPLORESIZE 
ifeq ($(SYSTEM),Linux)
CFLAGS+= -DPROCLOCKSBUG  -DATOIBUG
endif

#-DENDOFLINEBUG

CFLAGS:=$(OPTS) $(CFLAGS) 
LDFLAGS:=$(OPTS) $(LDFLAGS)
CPPFLAGS=$(CFLAGS)
# *** NORMALLY NOTHING HAS TO BE CHANGED BELOW THIS LINE
OBJ := repeat.o rbtree.o memory.o editor.o   getfile.o glob.o expandfilename.o readcolors.o  getfree.o getlabel.o  screenpart.o  marks.o digitstring.o   parts.o main.o  strtoint.o  keyproc.o marken.o procview.o menus.o inout.o move.o window.o edit.o file.o select.o undo.o screen.o search.o onlykey.o  editup.o  cursesinit.o input.o contain.o withcontain.o filter.o  ascii.o  multidigit.o macros.o editmacros.o typelist.o $(OBJ) keyinput.o  hassub.o savedatatype.o menufuncs.o support.o desturbe.o gethelp.o disk.o

ifneq ($(WINPD),y)
OBJ+=keyconfig.o 
endif
CPPFILES=  debug.cpp editlimits.cpp editor.cpp getfile.cpp getfree.cpp getlabel.cpp digitstring.cpp main.cpp marks.cpp memory.cpp   parts.cpp rbtree.cpp readcolors.cpp screenpart.cpp  strtoint.cpp trace.cpp keyproc.cpp marken.cpp procview.cpp menus.cpp inout.cpp move.cpp window.cpp edit.cpp file.cpp select.cpp undo.cpp screen.cpp search.cpp onlykey.cpp editup.cpp myalloc.cpp cursesinit.cpp input.cpp contain.cpp withcontain.cpp filter.cpp  ascii.cpp  multidigit.cpp macros.cpp editmacros.cpp typelist.cc keyconfig.cpp keyinput.cpp repeat.cpp hassub.cpp savedatatype.cpp menufuncs.cpp  support.cpp bedrcproc.cpp desturbe.cpp gethelp.cpp disk.cpp
CFILES=expandfilename.c glob.c
PRESRC=$(CFILES) $(CPPFILES)
SRC=$(PRESRC) version.cpp
HEADERS = builtinkeys.h  config.h convert.h copy.h cursesinit.h datatypes.h debug.h editor.h getfile.h getlabel.h glob.h head.h help.h digitstring.h inout.h keyinput.h keylist.h main.h marks.h memory.h myalloc.h offtypes.h onlykey.h  procedures.h rbtree.h readcolors.h screen.h screenpart.h system.h trace.h type.h views.h trace.cpp menu.h multidigit.h ascii.h  contain.h  filter.h  reverse.h macros.h keystrokes.h typelist.h visuals.h keyproc.h repeat.h hassub.h varmacro.h execproc.h desturbe.h shell.h termkeys.h


OTHERFILES=preprocedures head.h copy help
OLDFILES= oldundo.cpp
SCRIPTS=mkversion rules.mk
UPDATES=sourcenumber
GENERATED=help.h copy.h  md5sums.cpp  md5file date.c    md5file procedures.h  mainprocedures.h extrakeys.h helpdata helpdata.h helpindex.h helptext.h chhelp
PROGRAMS=extramenunr rxvtmenu $(PROGRAM) readhelp addmenu
EXEPROGRAMS= $(patsubst %,%$(EXEEXT),$(PROGRAMS))

ifneq ($(origin USE_NEW),undefined)
OBJ := $(OBJ) myalloc.o
endif


ALLEXCEPTVERSION=$(SRC) $(HEADERS) $(OTHERFILES) $(SCRIPTS)

ALLSOURCES=$(ALLEXCEPTVERSION) $(UPDATED)
ALLES= $(ALLSOURCES) $(CONFFILE) $(GENERATED)


all: procedures.h $(PROGRAM)
	@echo done
bash:
	bash
tags: $(CFILES) $(CPPFILES) Makefile
	ctags    $(CFILES) $(CPPFILES) $(HEADERS)
#	ctags  --sort  --c-types=cdefgmnstuvxAC $(CFILES) $(CPPFILES)



.PHONY: back
back: bak $(ALLES)
	cp $(ALLES) bak
bak:
	mkdir bak


.PHONY: src

src:
	@echo $(ALLES)

dep: $(PRESRC) ../config Makefile extrakeys.h dataprocer.h dataproc.h procedures.h mainprocedures.h readhelp.cpp  
	$(MAKE) -C ../plugins/examples makeinlinklist
	$(GCC) -MM -MG $(CPPFLAGS) $(CFILE) $(CPPFILES) readhelp.cpp > depend

empty:
	@echo done
KEYCONFIG= ../utils/keyconfig

keyconfig:$(KEYCONFIG)

$(KEYCONFIG): bed	
	-rm $(KEYCONFIG)
	ln $(PROGRAM) $(KEYCONFIG)
PROGCC=$(CXX)
#PROGCC=gcc
$(PROGRAM):   $(OBJ)  version.o $(INLINKLIST) $(OBJECTS) 
	$(PROGCC)  $(LDFLAGS) -o $(PROGRAM)  version.o $(OBJ) $(OBJECTS) $(LIBPATH) $(LIBS)

quick:  $(OBJ)  sourcenumber
	$(GCC) $(LDFLAGS) -o $(PROGRAM)  version.o $(OBJ)  $(OBJECTS) $(LIBPATH) $(LIBS)
sourcenumber: $(SRC)  $(BIN2BYTE)
	bash ./mkversion $(ALLEXCEPTVERSION)
md5file: $(SRC)  $(BIN2BYTE)
	bash ./mkversion $(ALLEXCEPTVERSION)

link:  dataprocer.h menufuncs.o procview.o menus.o  $(INLINKLIST) $(ININLINKLIB)
	$(GCC) $(LDFLAGS) -o $(PROGRAM)  version.o $(OBJ)  $(OBJECTS) $(LIBPATH) $(LIBS)
linkonly:   
	$(GCC)  $(LDFLAGS) -o $(PROGRAM)  version.o $(OBJ)  $(OBJECTS) $(LIBPATH)  $(LIBS)

lines: 
	-rm screenpart.o editup.o editor.o
	$(MAKE) bed
$(BIN2BYTE):
	$(MAKE) -C ../utils bin2byte
mainprocedures.h: preprocedures  dataproc.h ../config
	 $(GCC) -DNOINLINK $(CFLAGS) -x c   -E -P  $<  | $(SORT) -b -t '(' -k 2 > $@

procedures.h: preprocedures  ../plugins/examples/inlink.h dataproc.h ../config
	 $(GCC) $(CFLAGS) -x c   -E -P  $<  | $(SORT) -b -t '(' -k 2 > newprocedures.h
	(diff -q procedures.h newprocedures.h	&& echo no change &&rm newprocedures.h)||mv newprocedures.h procedures.h
	
help: prehelp helpoldterm.c Makefile ../config
	$(GCC)   $(CFLAGS) -x c -E -P prehelp|sed -e s/\"//g > help
ifneq ($(WINPD),y)
	echo '	-k: Configure key bindings for current terminal' >> help
endif
ifeq ($(CONFIG_COLOR),y)
	echo '	-B: black-white' >>help
else
ifeq ($(CONFIG_COLOR),o)
	echo '	-C: color' >>help
endif
endif
ifneq ($(filter -DNEWTERM,$(CFLAGS)),-DNEWTERM)
	$(GCC)   -imacros config.h $(CFLAGS) -E -P helpoldterm.c >> help
endif
ifeq ($(CYGWIN),yes)
	cat cygwinhelp >> help
endif
	cat helptext >> help

help.h:help $(BIN2BYTE)
	$(BIN2BYTE) help help help.h
copy.h:copy $(BIN2BYTE)
	$(BIN2BYTE) copy copy copy.h
compileinfo:../config Makefile rules.mk
	echo -e  CFLAGS=$(CFLAGS)\\nLDFLAGS=$(LDFLAGS)\\nLIBS=$(LIBS)\\n > compileinfo
md5sums.cpp:  machine  md5file compileinfo  $(BIN2BYTE)
	-rm md5file.pgp
	cat machine compileinfo md5file> compinfo
	 pgps -u 0x94BA03AE -a -t -o md5file.pgp compinfo||  gpg -u 0x94BA03AE -s -a -t  -o md5file.pgp compinfo ||cp compinfo md5file.pgp
	 $(BIN2BYTE) md5file.pgp md5sums md5sums.cpp

#	 $(BIN2BYTE) md5file md5sums md5sums.cpp

#version.o:  version.cpp sourcenumber md5sums.cpp help.h copy.h
#	$(GCC) $(CFLAGS) -c version.cpp
debug:
	-rm main.o trace.o debug.o editor.o myalloc.o
	$(MAKE)
mmap:
	-rm memory.o
	$(MAKE)
clean: 
	-rm    $(OBJ) version.o core  debug.o trace.o myalloc.o inlink.a  depend.bak $(CLEANOBJECTS) 
	-rm $(EXEPROGRAMS)
	
#	-rm rxvtmenu $(PROGRAM) core  inlink.a *.o*
realclean: clean
	-rm $(GENERATED) readhelp.o
	-rm md5sums.cpp tags 
	-rm depend

version.o: version.cpp sourcenumber md5sums.cpp copy.h help.h  date.c  ../config config.h
	$(GCC) $(CFLAGS) -c $<

digitstring: digitstring.cpp
	$(GCC) -g digitstring.cpp -DTEST -o digitstring 


%.i:%.cpp
	$(GCC) -E -P $(CPPFLAGS) $< -o $@
%.s:%.cpp
	$(GCC) -S  $(CPPFLAGS) $< -o $@
%.i:%.c
	$(GCC) -E $(CPPFLAGS) $< -o $@


stupid: inlink.h
	touch stupid
.PHONY: plugs $(INLINKLIST) mkinlink_h
mkinlink_h:
	$(MAKE) -C ../plugins/examples/ mkinlink_h
$(INLINKLIST):
	$(MAKE) -C ../plugins/examples/ $(@F)
	
#plugs:
#	for plug in $(ININLINKLIB); do \
#	$(MAKE) -C ../plugins/examples/ $(@F); \
#	done

plugs:	$(ININLINKLIB)
../plugins/examples/%: 
	$(MAKE) -C ../plugins/examples/ $(@F)
../plugins/examples/inlink.h: 
	$(MAKE) -C ../plugins/examples/ inlink.h

$(ININLINKLIB):
	$(MAKE) -C ../plugins/examples/ $(@F)

grep.%:
	grep $* *.c* *.h

include dataproc.mk


getch:getch.c 
	$(GCC) -g $(CFLAGS) -UHAS_BOOL getch.c -o getch $(LIBS)
getcher:getcher.c 
	$(GCC) -g $(CFLAGS) -UHAS_BOOL getcher.c -o getcher $(LIBS)


#memory.o:memory.i
#	$(GCC) -c $(CFLAGS) -x c++ memory.i
regex.o: regex.c regex.h
        $(CC) -O6 -DHAVE_STRING_H=1 -I.  -c $<

support: support.cpp expandfilename.c
	$(CC) -g -DTEST $^ -o support

keyproc.o: builtinkeys.h
keyproc.o: extrakeys.h
readcolors.o:bedrcproc.h
bedrcproc.h: bedrcproc.cpp
	echo '/*WARNING automatically generated  from bedrcproc.cpp */' > bedrcproc.h
	grep '^initword' bedrcproc.cpp|sed -e 's/^initword\([^{]*\).*/initword\1/'|$(SORT) -b -t '(' -k 2 >> bedrcproc.h
extramenunr$(EXEEXT):menus.cpp editor.h menu.h
	$(CC) -DPRINTMAXSHOWMENUS=1 $(CFLAGS) menus.cpp -o extramenunr
extramenunr.i:menus.cpp editor.h menu.h
	$(CC) -E -P -DPRINTMAXSHOWMENUS=1 $(CFLAGS) menus.cpp -o extramenunr.i
mkmenus.h: extramenunr$(EXEEXT)
#	extramenus=`./extramenunr`; for ((i=0;$$i<$$extramenus;i=$$[$$i+1])); do echo  "mkmenunum($$i)";done >mkmenus.h
	extramenus=`./extramenunr`;i=0; while test $$i -lt $$extramenus; do echo  "mkmenunum($$i)";i=$$[$$i+1];done >mkmenus.h
#	havemenus=`gcc -E -P '-Dmenumake(x...)=xoneitem' menu.h|grep xoneitem|wc -w`; for ((i=0;$$i<$$[30-$$havemenus];i=$$[$$i+1])); do echo  "mkmenunum($$i)";done >mkmenus.h
-include depend	

exes:
	echo $(EXEPROGRAMS)

Bed:   
	$(GCC) $(LDFLAGS) -o Bed  version.o $(OBJ)  $(OBJECTS)  $(ININLINKLIB) $(LIBPATH) /home/jka/self/projects/c/bedall/libs/libc.so.6 /home/jka/self/projects/c/bedall/libs/libpthread.so.0 /home/jka/self/projects/c/bedall/libs/libdl.so.2  /home/jka/self/projects/c/bedall/libs/libm.so.6  /home/jka/self/projects/c/bedall/libs/libncurses.so.4   /home/jka/self/projects/c/bedall/libs/ld-linux.so.2


beds:   
	$(GCC) $(LDFLAGS) -o $(PROGRAM)s  version.o $(OBJ) $(OBJECTS) $(LIBPATH) $(LIBS) -L/usr/X11R6/lib -lX11

readhelp.o: readhelp.cpp procedures.h

readhelp: readhelp.o getlabel.o 
	$(CXX) readhelp.o getlabel.o -o readhelp
helpdata.h: helpdata readhelp
	./readhelp < helpdata
helptext.h: helpdata readhelp
	./readhelp < helpdata
helpindex.h: helpdata readhelp
	./readhelp < helpdata
helpdata: prehelpdata chhelp ../plugins/examples/plughelp
	cat prehelpdata ../plugins/examples/plughelp|sed -f chhelp  > helpdata


.PHONY: ../plugins/examples/plughelp
../plugins/examples/plughelp:
	$(MAKE) -C ../plugins/examples/ plughelp
chhelp: addmenu
	./addmenu > chhelp
addmenu: addmenu.cpp menu.h
	$(GCC) addmenu.cpp -o addmenu
showopts:
	echo OPTS: $(OPTS)
	echo CFLAGS: $(CFLAGS)
	echo LDFLAGS: $(LDFLAGS)
inin:../plugins/examples/inlinklist showargs
	echo $(ININLINKLIB)
.PHONY: showargs
showargs:
	@echo obj: $(OBJ)
	@echo objects: $(OBJECTS) 
	@echo libpath $(LIBPATH)
	@echo libs: $(LIBS)


