# be sure to use 'make install' so that all the necessary directory and
# file permissions are given - if you get messages about files which
# cannot be created or opened, that is probably the reason.

# compiler is g++
CC = g++

# uncomment the next line if compiling with egcs/gcc-2.9 to make
# smaller binaries
#EGCS_FLAGS = -fno-exceptions

# these are the flags for normal compiling
CFLAGS = -c $*.cpp $(EGCS_FLAGS) -fno-rtti -O2 -Wall

# these are the flags for debugging
#CFLAGS = -c $*.cpp $(EGCS_FLAGS) -g -Wall

# additional include directories
INCLUDEFLAGS = -I/usr/include/ncurses

# link options for normal compiling
LDFLAGS = -lncurses -s -o $@

# link options for debugging
#LDFLAGS = -lncurses -o $@

# this is where the program will be installed by 'make install'
INSTALLDIR = /usr/local/kamdir

# this is where the lock files are normally stored
LOCKDIR = /var/lock

# CHARSET defines whether the terminal on which the program is displayed
# shows only ASCII characters or whether it displays Latin-1 or Code
# Page 437 - the options are LATIN_1, CP437 or ASCII.  To display
# continental characters with most Unix systems use LATIN_1 with
# appropriate entries in ~/.inputrc - see file README for the
# necessary lines to put in ~./inputrc
# NOTE: transmitted characters use the 'de facto' standard of CP437
# this option is concerned with the local display, not with what is
# transmitted
CHARSET = LATIN_1

# you will probably not need to define this, but look at the README
# file for further details about the F1 to F4 keys
# F1_USER_CODE = [11~

all: kam endhost

kam: main.o tnc.o buffers.o com.o screen.o keyboard.o keyboard_funcs.o pipes.o filesend.o download.o
	$(CC) $(LDFLAGS) main.o tnc.o buffers.o com.o screen.o keyboard.o pipes.o keyboard_funcs.o filesend.o download.o

main.o: main.cpp qqueue.cpp qqueue.h dlist.h dlist.cpp prog_defs.h buffers.h com.h screen.h keyboard.h pipes.h tnc.h keyboard_funcs.h filesend.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS)

tnc.o: tnc.cpp tnc.h prog_defs.h pipes.h qqueue.cpp qqueue.h buffers.h screen.h download.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS) -DCHAR_SET=$(CHARSET)

buffers.o: buffers.cpp qqueue.cpp qqueue.h buffers.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS)

com.o: com.cpp com.h
	$(CC) $(CFLAGS) -DLOCK_DIR=\"$(LOCKDIR)\"

screen.o: screen.cpp screen.h dlist.h dlist.cpp qqueue.cpp qqueue.h prog_defs.h buffers.h tnc.h gpl.h download.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS)

keyboard.o: keyboard.cpp keyboard.h dlist.h dlist.cpp qqueue.cpp qqueue.h screen.h buffers.h prog_defs.h pipes.h tnc.h keyboard_funcs.h filesend.h download.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS) -DCHAR_SET=$(CHARSET) -DF1_USERCODE=\"$(F1_USER_CODE)\"

keyboard_funcs.o: keyboard_funcs.cpp keyboard_funcs.h dlist.h dlist.cpp qqueue.cpp qqueue.h keyboard.h screen.h buffers.h prog_defs.h pipes.h tnc.h filesend.h
	$(CC) $(INCLUDEFLAGS) $(CFLAGS) -DCHAR_SET=$(CHARSET)

pipes.o: pipes.cpp pipes.h
	$(CC) $(CFLAGS)

filesend.o: filesend.cpp filesend.h dlist.h dlist.cpp qqueue.cpp qqueue.h screen.h buffers.h prog_defs.h tnc.h
	$(CC) $(CFLAGS) -DCHAR_SET=$(CHARSET)

download.o: download.cpp download.h dlist.h dlist.cpp prog_defs.h
	$(CC) $(CFLAGS)

endhost: endhost.o com.o
	$(CC) $(LDFLAGS) endhost.o com.o

endhost.o: endhost.cpp com.cpp com.h
	$(CC) $(CFLAGS)

install: kam endhost
	install -d -gusers -m777 $(INSTALLDIR)
	install -d -gusers -m777 $(INSTALLDIR)/parms
	install -m664 -oroot -gusers kam xkam endhost cq messages helpfile $(INSTALLDIR)/
	install -m664 -oroot -gusers tnc.parms home away $(INSTALLDIR)/parms/
	chmod +x $(INSTALLDIR)/kam
	chmod +s $(INSTALLDIR)/kam
	chmod +x $(INSTALLDIR)/xkam
	chmod +x $(INSTALLDIR)/endhost
	chmod +s $(INSTALLDIR)/endhost
	install -m644 -oroot -gusers kamrc /etc

clean:
	rm -f *.o
	rm -f kam
	rm -f endhost
