###############################
# Makefile for IDO
###############################


# Source code directories
CORE_INCLUDE=../../../include
SRC_INCLUDE=../include

prefix=@prefix@
exec_prefix=@exec_prefix@
LOGDIR=@LOGDIR@
CFGDIR=@sysconfdir@
BINDIR=@bindir@
LIBDIR=@libdir@
LIBEXECDIR=@libexecdir@
CGIDIR=@sbindir@
INSTALL=@INSTALL@
INSTALL_OPTS=@INSTALL_OPTS@

CC=@CC@

CFLAGS=@CFLAGS@ @DEFS@ 

###############################
# Debug
###############################
ENABLE_DEBUG=@ENABLE_DEBUG@

# Compiler flags for use with Valgrind - set when debug is enabled
ifeq ('$(ENABLE_DEBUG)', 'yes')
        CFLAGS+=-O0 -g -DHAVE_CONFIG_H
endif

# We don't like ANSI because ANSI doesn't like us! phhht!
#CFLAGS=-g -ansi -pedantic -DHAVE_CONFIG_H

MOD_CFLAGS=@MOD_CFLAGS@
LDFLAGS=@LDFLAGS@
MOD_LDFLAGS=@MOD_LDFLAGS@
LIBS=@LIBS@
SOCKETLIBS=@SOCKETLIBS@
#DBCFLAGS=@DBCFLAGS@ 
#DBLDFLAGS=@DBLDFLAGS@
DBLIBS=@DBLIBS@
MATHLIBS=-lm
OTHERLIBS=@OTHERLIBS@
THREADLIBS=@THREADLIBS@

# asprintf for Solaris friends ;) 
SNPRINTF_O_IDO=@SNPRINTF_O_IDO@
SNPRINTF_C=../../../common/snprintf.c


COMMON_INC=$(CORE_INCLUDE)/config.h $(SRC_INCLUDE)/common.h $(SRC_INCLUDE)/io.h $(SRC_INCLUDE)/protoapi.h $(SRC_INCLUDE)/utils.h $(SRC_INCLUDE)/logging.h
COMMON_SRC=io.c utils.c
COMMON_OBJS=io.o utils.o ${SNPRINTF_O_IDO}

IDO2DB_OBJS=dbhandlers.o dbqueries.o logging.o

IDO_INC=$(SRC_INCLUDE)/ido2db.h $(SRC_INCLUDE)/db.h
IDO_SRC=db.c
IDO_OBJS=db.o

CP=@CP@


all: file2sock log2ido ido2db idomod.so sockdebug

file2sock: file2sock.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ file2sock.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

log2ido: log2ido.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ log2ido.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

ido2db: ido2db.c $(IDO_INC) $(IDO_OBJS) $(COMMON_INC) $(COMMON_OBJS) $(IDO2DB_OBJS)
	$(CC) $(CFLAGS) $(DBCFLAGS) -o ido2db ido2db.c $(IDO2DB_OBJS) $(COMMON_OBJS) $(IDO_OBJS) $(LDFLAGS) $(DBLDFLAGS) $(LIBS) $(SOCKETLIBS) $(DBLIBS) $(THREADLIBS) $(MATHLIBS) $(OTHERLIBS)

idomod.so: idomod.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -o idomod.so idomod.c $(COMMON_OBJS) $(MOD_LDFLAGS) $(LDFLAGS) $(LIBS) $(SOCKETLIBS) $(OTHERLIBS)

sockdebug: sockdebug.c $(COMMON_INC) $(COMMON_OBJS)
	$(CC) $(CFLAGS) -o $@ sockdebug.c $(COMMON_OBJS) $(LDFLAGS) $(LIBS) $(MATHLIBS) $(SOCKETLIBS) $(OTHERLIBS)

io.o: io.c $(SRC_INCLUDE)/io.h
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -c -o $@ io.c $(MOD_LDFLAGS)

utils.o: utils.c $(SRC_INCLUDE)/utils.h
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -c -o $@ utils.c $(MOD_LDFLAGS)

db.o: db.c $(SRC_INCLUDE)/db.h
	$(CC) $(CFLAGS) -c -o $@ db.c

dbhandlers.o: dbhandlers.c $(SRC_INCLUDE)/dbhandlers.h
	$(CC) $(CFLAGS) -c -o $@ dbhandlers.c

dbqueries.o: dbqueries.c $(SRC_INCLUDE)/dbqueries.h
	$(CC) $(CFLAGS) -c -o $@ dbqueries.c

logging.o: logging.c $(COMMON_INC)
	$(CC) $(CFLAGS) -c -o $@ logging.c

snprintf.o: $(SNPRINTF_C) $(CORE_INCLUDE)/snprintf.h
	$(CC) $(MOD_CFLAGS) $(CFLAGS) -c -o $@ $(SNPRINTF_C) $(MOD_LDFLAGS)

clean:
	rm -f core file2sock log2ido ido2db sockdebug *.o *.so
	rm -f *~ */*~

distclean: clean
	rm -f Makefile

devclean: distclean

install:
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 775 $(INSTALL_OPTS) -d $(DESTDIR)$(LIBDIR)
	$(INSTALL) -m 755 $(INSTALL_OPTS) ido2db $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 $(INSTALL_OPTS) log2ido $(DESTDIR)$(BINDIR)
	$(INSTALL) -m 755 $(INSTALL_OPTS) idomod.so $(DESTDIR)$(LIBDIR)

