# Change these if you need to.
BINDIR =	/usr/local/bin
MANDIR =	/usr/local/man

# If you need to change this to gcc or pcc or whatever, go ahead.
CC =		cc

# Uncommenting DEBUG makes e generate an ELOG file with debugging messages.
#DEBUG =	-DDEBUG

# Uncommenting NO_LEAKFINDER makes the program *not* check for memory leaks.
# The default, once leakfinder is installed, is to check.
NO_LEAKFINDER =	-DNO_LEAKFINDER

# Most of these compiler flags are for adding extra checks. If your
# compiler doesn't support them, feel free to get rid of them.
CFLAGS =	-O -ansi -pedantic -U__STRICT_ANSI__ -Wall -Wpointer-arith -Wshadow -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wno-long-long $(DEBUG) $(NO_LEAKFINDER)

# If ncurses isn't found on your system, try regular curses.
CURSES_LIB =	-lncurses

LDFLAGS =	-g $(CURSES_LIB)

all:		e

e:		e.c version.h buffer.h undo.h str.h port.h popen2.h leakfinder.h key_bindings.h teache.h enews.h buffer.o undo.o str.o port.o popen2.o leakfinder.o
	$(CC) $(CFLAGS) e.c $(LDFLAGS) buffer.o undo.o str.o port.o popen2.o leakfinder.o -o e

buffer.o:	buffer.c buffer.h leakfinder.h
	$(CC) $(CFLAGS) -c buffer.c

undo.o:		undo.c undo.h leakfinder.h
	$(CC) $(CFLAGS) -c undo.c

str.o:		str.c str.h leakfinder.h
	$(CC) $(CFLAGS) -c str.c

port.o:		port.c port.h
	$(CC) $(CFLAGS) -c port.c

popen2.o:	popen2.c popen2.h
	$(CC) $(CFLAGS) -c popen2.c

leakfinder.o:	leakfinder.c leakfinder.h
	$(CC) $(CFLAGS) -c leakfinder.c

key_bindings.h:	key_bindings.txt bindings_to_h
	./bindings_to_h key_bindings.txt > key_bindings.h

teache.h:	teache.txt txt_to_h
	./txt_to_h teache.txt > teache.h

enews.h:		enews.txt txt_to_h
	./txt_to_h enews.txt > enews.h

getch:		getch.c
	$(CC) $(CFLAGS) getch.c $(LDFLAGS) -o getch

clean:
	rm -f e key_bindings.h teache.h enews.h getch *.o core core.* *.core

install:	all
	rm -f $(BINDIR)/e
	-mkdir -p $(BINDIR)
	cp e $(BINDIR)
	rm -f $(MANDIR)/man1/e.1
	-mkdir -p $(MANDIR)/man1
	cp e.1 $(MANDIR)/man1

tar:
	@name=`sed -n -e '/#define E_VERSION /!d' -e 's/.* "/e-/' -e 's/ .*//p' version.h` ; \
	  rm -rf $$name ; \
	  mkdir $$name ; \
	  tar cf - `cat FILES` | ( cd $$name ; tar xfBp - ) ; \
	  chmod 644 $$name/Makefile ; \
	  tar cf $$name.tar $$name ; \
	  rm -rf $$name ; \
	  gzip $$name.tar
