#
# $Id$
#

include ../../Make.inc

SERVER_SRCS	= acct.c auth.c client.c conffile.c crypt.c exec.c files.c \
		  listen.c log.c mainconfig.c modules.c modcall.c \
		  radiusd.c stats.c \
		  session.c threads.c util.c valuepair.c version.c  \
		  xlat.c event.c realms.c evaluate.c vmps.c detail.c

SERVER_OBJS	+= $(SERVER_SRCS:.c=.lo)

INCLUDES	= ../include/autoconf.h ../include/conf.h    \
		  ../include/libradius.h ../include/radius.h \
		  ../include/radiusd.h ../include/ident.h ../include/stats.h

CFLAGS		+= -I$(top_builddir)/src
CFLAGS		+= -DHOSTINFO=\"${HOSTINFO}\"
CFLAGS		+= -DRADIUSD_VERSION=\"${RADIUSD_VERSION}\"
CFLAGS		+= $(SNMP_INCLUDE)
CFLAGS		+= $(OPENSSL_INCLUDE)
VFLAGS		= -DRADIUSD_MAJOR_VERSION=$(RADIUSD_MAJOR_VERSION)
VFLAGS		+= -DRADIUSD_MINOR_VERSION=$(RADIUSD_MINOR_VERSION)
MODULE_LIBS	= $(STATIC_MODULES)
BINARIES	= radiusd radwho radclient radmin radconf2xml

#
#  The RADIUS sniffer
#
PCAP_LIBS	= @PCAP_LIBS@
ifneq ($(PCAP_LIBS),)
BINARIES	+= radsniff
endif

#
#  Not using shared libraries, add in ALL known static modules
#  at build time.
#
ifneq ($(USE_SHARED_LIBS),yes)
#
#  For static linking...
#
LINK_MODE	= -static -all-static

SUB_MODULES += rlm_eap_md5 rlm_eap_leap rlm_eap_tls rlm_eap_ttls rlm_eap_sim
SUB_MODULES += rlm_eap_peap rlm_eap_mschapv2 rlm_eap_gtc rlm_eap_ikev2
SUB_MODULES += rlm_sql_db2 rlm_sql_iodbc rlm_sql_mysql
SUB_MODULES += rlm_sql_oracle rlm_sql_postgresql rlm_sql_sybase rlm_sql_unixodbc
LIBS        += $(shell test -f ../modules/rlm_eap/libeap/$(LIBPREFIX)freeradius-eap.la && echo ../modules/rlm_eap/libeap/$(LIBPREFIX)freeradius-eap.la)

MODULE_LIBS	+= $(shell for x in $(MODULES);do test -f ../modules/$$x/$$x.la && echo -dlpreopen ../modules/$$x/$$x.la;done)
MODULE_LIBS	+= $(shell for x in $(SUB_MODULES);do test -f ../modules/*/types/$$x/$$x.la && echo -dlpreopen ../modules/*/types/$$x/$$x.la;done)
MODULE_LIBS	+= $(shell for x in $(SUB_MODULES);do test -f ../modules/*/drivers/$$x/$$x.la && echo -dlpreopen ../modules/*/drivers/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(MODULES);do test -f ../modules/$$x/$$x.la && echo ../modules/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(SUB_MODULES);do test -f ../modules/*/types/$$x/$$x.la && echo ../modules/*/types/$$x/$$x.la;done)
MODULE_OBJS     += $(shell for x in $(SUB_MODULES);do test -f ../modules/*/drivers/$$x/$$x.la && echo ../modules/*/drivers/$$x/$$x.la;done)
endif

.PHONY: all clean install reconfig
all: $(BINARIES)

$(SERVER_OBJS): $(INCLUDES)

radiusd: $(SERVER_OBJS) $(MODULE_OBJS) $(LIBRADIUS)
	$(LIBTOOL) --mode=link $(CC) -export-dynamic -dlopen self \
		$(LDFLAGS) $(LINK_MODE) -o $@ $(SERVER_OBJS)      \
		$(MODULE_LIBS) $(LIBRADIUS) $(LIBS) $(SNMP_LIBS) \
		$(LCRYPT) $(PTHREADLIB) $(LIBLTDL) $(OPENSSL_LIBS)

radiusd.lo: radiusd.c  ../include/modules.h ../include/modcall.h ../include/modpriv.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c radiusd.c

%.lo: %.c
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS)  -c $<

acct.lo: acct.c ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c acct.c

auth.lo: auth.c ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c auth.c

client.lo: client.c ../include/conffile.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c client.c

conffile.lo: conffile.c ../include/conffile.h ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c conffile.c

mainconfig.lo: mainconfig.c ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c mainconfig.c

modcall.lo: modcall.c
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(INCLTDL) -c modcall.c

modules.lo: modules.c ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(VFLAGS) $(INCLTDL) -c modules.c

session.lo: session.c ../include/modules.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c session.c

# It's #include'd for simplicity.  This should be fixed...
listen.lo: listen.c dhcpd.c command.c
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(INCLTDL) -c listen.c

#
# Helper programs
#

radclient.lo: radclient.c $(INCLUDES)
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -I ../modules/rlm_mschap -c radclient.c

MSCHAP_OBJS := ../modules/rlm_mschap/smbdes.lo ../modules/rlm_mschap/mschap.lo 

radclient: radclient.lo $(MSCHAP_OBJS) $(LIBRADIUS)
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o radclient radclient.lo $(MSCHAP_OBJS) $(LIBRADIUS) $(LIBS)

# These two rules need to be specific in order to supercede the generic
# "compile C file" rules.
../modules/rlm_mschap/smbdes.lo: ../modules/rlm_mschap/smbdes.c
	${MAKE} -C ../modules/rlm_mschap/

../modules/rlm_mschap/mschap.lo: ../modules/rlm_mschap/mschap.c
	${MAKE} -C ../modules/rlm_mschap/

radsniff.lo: radsniff.c $(INCLUDES) ../include/radsniff.h
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c radsniff.c

radsniff: radsniff.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o radsniff radsniff.lo $(LIBRADIUS) $(LIBS) $(PCAP_LIBS)

radwho.lo: radwho.c $(INCLUDES)
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c radwho.c

radwho: radwho.lo util.lo log.lo conffile.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o radwho radwho.lo util.lo log.lo conffile.lo $(LIBRADIUS) $(LIBS)

radmin: radmin.lo $(LIBRADIUS) util.lo log.lo conffile.lo
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o $@ $^ $(LIBREADLINE) $(LIBS)

radconf2xml: radconf2xml.lo $(LIBRADIUS) util.lo log.lo conffile.lo
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o $@ $^ $(LIBS)

dhclient.lo: dhclient.c $(INCLUDES)
	$(LIBTOOL) --mode=compile $(CC) $(CFLAGS) -c dhclient.c

# Don't install this for now.
dhclient: dhclient.lo $(LIBRADIUS)
	$(LIBTOOL) --mode=link $(CC) $(LDFLAGS) $(LINK_MODE) -o dhclient dhclient.lo $(LIBRADIUS) $(LIBS)

clean:
	rm -rf *.o *.so *.lo *~ $(BINARIES) .libs

install:
	$(INSTALL) -d -m 755	$(R)$(sbindir)
	$(INSTALL) -d -m 755	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radiusd$(EXEEXT)	$(R)$(sbindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radmin$(EXEEXT)	$(R)$(sbindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radclient$(EXEEXT)	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radwho$(EXEEXT)	$(R)$(bindir)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radconf2xml$(EXEEXT)	$(R)$(bindir)
ifneq ($(PCAP_LIBS),)
	$(LIBTOOL) --mode=install $(INSTALL) -m 755 $(INSTALLSTRIP) radsniff$(EXEEXT)	$(R)$(bindir)
endif
	$(INSTALL) -m 755    checkrad.pl		$(R)$(sbindir)/checkrad
	$(INSTALL) -m 755    radlast			$(R)$(bindir)
	$(INSTALL) -m 755    radtest 			$(R)$(bindir)
	$(INSTALL) -m 755    radzap			$(R)$(bindir)

reconfig:
