# $Id$
#
# Usage: make [DEBUG=1] [NOFORK=1] [ZLIB=1] [BZLIB2=1] [ZLIBDL=1] [BW_LIM=1]
#

CC=gcc
DEFINES=-DHAVE_THREADS -DHAVE_SNPRINTF -DHAVE_VSNPRINTF -DHAVE_GETOPT -DHAVE_UNISTD -DOS2=2 -DHAVE_IO_H -DHAVE_ARPA_INET_H -DHAVE_SYS_TIME_H -DHAVE_SYS_PARAM_H -DHAVE_SYS_IOCTL_H -DHAVE_DOS_H -DHAVE_STDARG_H -DEMX -DEMXSIGNALS -DHAVE_WAITPID -DHAVE_GETTIMEOFDAY -DHTTPS -DNTLM -DAMIGADOS_4D_OUTBOUND -DTCPIPV4 -DHAVE_NETINET_IN_H -DHAVE_NETDB_H -DHAVE_RESOLV_H -DHAVE_ARPA_NAMESER_H -Du_int16_t=u_short -Du_int32_t=u_int
CFLAGS=$(DEFINES) -Wall -Zomf -Zmt -funsigned-char -Wno-char-subscripts
LFLAGS=-Zomf -Zcrtdll -Zmt -Zlinker /PM:VIO
LIBS=-lsocket
NTLM_SRC=ntlm/des_enc.c ntlm/helpers.c ntlm/ecb_enc.c ntlm/md4_dgst.c ntlm/set_key.c
SRCS=binkd.c readcfg.c tools.c ftnaddr.c ftnq.c client.c server.c protocol.c bsy.c inbound.c breaksig.c branch.c os2/gettid.c os2/sem.c ftndom.c ftnnode.c os2/getfree.c srif.c pmatch.c readflo.c prothlp.c iptools.c rfc2553.c run.c binlog.c exitproc.c getw.c xalloc.c setpttl.c https.c md5b.c crypt.c srv_gai.c os2/ns_parse.c ${NTLM_SRC}
TARGET=binkd2emo

#PERLDIR=../perl5.00553/os2
#ZLIB_DIR=../zlib-1.2.11
#BZLIB2_DIR=../bzip2-1.0.6

ifdef DEBUG
TARGET:=$(TARGET)-debug
CFLAGS+=-g -DDEBUG
LFLAGS+=-g
else
CFLAGS+=-O2 -s
LFLAGS+=-O2 -s
endif

ifdef NOFORK
TARGET:=$(TARGET)-nofork
CFLAGS+=-DDEBUGCHILD
endif

ifdef PERL
TARGET:=$(TARGET)-perl
ifndef PERLDIR
PERLDIR=$(shell perl -MConfig -e "print $$Config{archlib}")
endif
CFLAGS+= -DWITH_PERL -DPERL_MULTITHREAD -DDOSISH -DEMBED -DOS2=2 \
	 -D_EMX_CRT_REV_=64 -I$(PERLDIR)/CORE
#CFLAGS+= -D__ST_MT_ERRNO__
LFLAGS+= -L$(PERLDIR)/CORE -Zstack 16000
LIBS:= $(PERLDIR)/auto/DynaLoader/DynaLoader.lib \
       -llibperl -lm -lbsd -ldb $(LIBS)
SRCS+= perlhooks.c
endif

ifdef ZLIB
COMPRESS=1
CFLAGS+= -DWITH_ZLIB=1
ifdef ZLIB_BASE
CFLAGS+= -I$(ZLIB_BASE)
endif
ifndef ZLIBDL
TARGET:=$(TARGET)-zlib
ifndef ZLIB_LIB
ZLIB_LIB=z_s
endif
LIBS:=-l$(ZLIB_LIB) $(LIBS)
ifdef ZLIB_BASE
LFLAGS+= -L$(ZLIB_BASE)
endif
endif
endif

ifdef BZLIB2
COMPRESS=1
CFLAGS+= -DWITH_BZLIB2=1
ifdef BZLIB2_BASE
CFLAGS+= -I$(BZLIB2_BASE)
endif
ifndef ZLIBDL
TARGET:=$(TARGET)-bzlib2
ifndef BZLIB2_LIB
BZLIB2_LIB=libbz2
endif
LIBS:=-l$(BZLIB2_LIB) $(LIBS)
ifdef BZLIB2_BASE
LFLAGS+= -L$(BZLIB2_BASE)
endif
endif
endif

ifdef COMPRESS
SRCS+= compress.c
endif

ifdef ZLIBDL
SRCS+= zlibdl.c
CFLAGS+= -DZLIBDL=1
endif

ifdef BW_LIM
CFLAGS+= -DBW_LIM=1
endif

.SUFFIXES:
.SUFFIXES: .c .obj

OBJS=${SRCS:.c=.obj}

TARGET:=$(TARGET).exe

all: $(TARGET)

.c.obj:
	@echo Compiling $<...
	@$(CC) -c $(CFLAGS) -o $@ $<

.DELETE_ON_ERROR:

$(TARGET): $(OBJS)
	@echo Linking $@...
	@$(CC) -o $@ $(LFLAGS) $(OBJS) $(LIBS)

install: all clean

clean:
	-del /n *.RES *.obj *.o os2\*.o os2\*.obj ntlm\*.o ntlm\*.obj *.map *~ *.bak *.b *.ini *.err core >nul 2> nul

depend	Makefile.dep:
	@echo Build Makefile.dep...
	@gcc -MM $(CFLAGS) $(SRCS) > Makefile.dep

include Makefile.dep
