# Makefile for NeoStats
include Makefile.inc 

NEOCFLAGS = $(CFLAGS)
EXTRA_LDFLAGS = `curl/mycurl-config --libs`

PROGS = neostats cronchk makeconf
CONF = neostats.motd
DOCS = doc/FAQ doc/USERMAN README BUGS AUTHORS COPYING CREDITS TODO \
		ChangeLog doc/README.* dl/modules.txt RELNOTES INSTNOTES doc/old/*
DOCS_PROGS = doc/read-faq doc/read-userman
DATA = data/tlds.nfo

PCRE_OBJS = pcre/pcre.o pcre/get.o pcre/study.o
ADNS_OBJS =	adns/check.o adns/event.o adns/general.o adns/parse.o adns/query.o \
		adns/reply.o adns/setup.o adns/transmit.o adns/types.o
KEEPER_OBJS = keeper/kp_cache.o keeper/kp_dir.o keeper/kp_enum.o keeper/kp_get.o \
		keeper/kp_iface.o keeper/kp_recur.o keeper/kp_set.o keeper/kp_sort.o \
		keeper/kp_util.o
OBJS = ${SRCS:.c=.o}
SRCS =	dns.c chans.c dotconf.c services.c main.c sock.c conf.c ircd.c timer.c \
		users.c ns_help.c dl.c list.c hash.c server.c keeper.c log.c misc.c \
		support.c ircstring.c commands.c transfer.c exclude.c match.c bans.c \
		Unreal.c
IRCDSRC = Ultimate.c Unreal.c hybrid7.c neoircd.c Bahamut.c \
		Ircu.c mystic.c QuantumIRCd.c liquidircd.c Viagra.c
IRCDINC = ${IRCDSRC:.c=.h}

INCLUDES = config.h dl.h dotconf.h hash.h list.h stats.h neostats.h \
		conf.h log.h support.h ircstring.h events.h numeric.h pcre.h \
		transfer.h
COREINCS = dns.h services.h sock.h ircd.h exclude.h \
		ns_help.h timer.h users.h chans.h server.h bans.h
BUILDFILES = configure config.sub config.guess *.in \
		dl/Makefile dl/Makefile.inc.in install-sh Config makeconf cronchk

DISTFILES = $(INCLUDES) $(COREINCS) $(SRCS) $(IRCDSRC) $(IRCDINC) \
		$(BUILDFILES) $(DATA) $(DOCS) $(DOCS_PROGS) $(CONF)	
 
SUBDIRS = doc doc/old data logs tools 
DISTLIBS = adns pcre curl sqlsrv keeper
DISTMOD = cs extauth hostserv loveserv ms statserv template

all:  libadns libkeeper libpcre libcurl.a neostats modules utils 

modules:
	(cd dl; $(MAKE) $@)

libadns:
	(cd adns; $(MAKE))

libkeeper:
	(cd keeper; $(MAKE))

libpcre:
	(cd pcre; $(MAKE))

libcurl.a:
	(cd curl; $(MAKE))

sqlsrvlib:
	(cd sqlsrv; $(MAKE))

utils:
	(cd tools; $(MAKE) $@)

buildversion: 
	@(if test -f version.sh ; then $(SHELL) version.sh; else echo > version.h; fi)

neostats: buildversion $(OBJS)
	$(CC) $(LDFLAGS) $(OBJS)  $(ADNS_OBJS) $(KEEPER_OBJS) $(PCRE_OBJS) curl/libcurl.a  $(EXTRA_LDFLAGS) -o $@

# include dependency info 


.c.o:
	$(CC) $(NEOCFLAGS) $(NEOINCLUDES) -c $< 
	$(CC) -MM $(NEOINCLUDES) -c $< > $*.d

clean:
	(cd dl; $(MAKE) $@)
	(cd adns; $(MAKE) $@)
	(cd keeper; $(MAKE) $@)
	(cd tools; $(MAKE) $@)
	(cd pcre; $(MAKE) $@)
	(cd curl; $(MAKE) $@)
	(cd sqlsrv; $(MAKE) $@)
	$(RM) *.o neostats Makefile config.h Makefile.inc dl/Makefile.inc *.log *.a *.d *.exe version.h

distclean:
	(cd dl; $(MAKE) $@)
	(cd adns; $(MAKE) $@)
	(cd keeper; $(MAKE) $@)
	(cd tools; $(MAKE) $@)
	(cd pcre; $(MAKE) $@)
	(cd curl; $(MAKE) $@)
	(cd sqlsrv; $(MAKE) $@)
	$(RM) *.o neostats *.cache Makefile config.h Makefile.inc dl/Makefile.inc *.log *.a *.d *.exe config.status version.h

install: neostats
	@echo "Installing ..."
	@$(RM) /home/ircd/NeoStats/dl/cs.so
	@$(RM) /home/ircd/NeoStats/dl/ms.so
	@$(RM) /home/ircd/NeoStats/dl/SecureServ.so
	@$(RM) /home/ircd/NeoStats/include/*.h
	$(INSTALL) -m 755 -d /home/ircd/NeoStats
	$(INSTALL) -m 755 -d /home/ircd/NeoStats/dl
	$(INSTALL) -m 755 -d /home/ircd/NeoStats/include
	$(INSTALL) -m 755 -d /home/ircd/NeoStats/doc
	$(INSTALL) -m 755 -d /home/ircd/NeoStats/data
	$(INSTALL) -m 750 -d /home/ircd/NeoStats/logs
	$(INSTALL) -m 755 $(PROGS) /home/ircd/NeoStats
	$(INSTALL_DATA) $(CONF) /home/ircd/NeoStats
	$(INSTALL_DATA) $(DOCS) /home/ircd/NeoStats/doc
	$(INSTALL) -m 755 $(DOCS_PROGS) /home/ircd/NeoStats/doc
	$(INSTALL_DATA) $(DATA) /home/ircd/NeoStats/data
	$(INSTALL_DATA) $(INCLUDES) /home/ircd/NeoStats/include
	$(INSTALL_DATA) version.h /home/ircd/NeoStats/include
	$(INSTALL_DATA) Unreal.h /home/ircd/NeoStats/include
	(cd adns; $(MAKE) $@)
	(cd sqlsrv; $(MAKE) $@)
	(cd dl; $(MAKE) $@)
	(cd tools; $(MAKE) $@)
	@echo "Done."
	@cat INSTNOTES 
	@if test -f INSTNOTES.svn ; then cat INSTNOTES.svn ; fi 

dist: 	
	@echo -n "Creating Directories"
	@$(RM) $(DISTDIR)
	@echo -n "."
	@mkdir $(DISTDIR)
	@echo -n "."
	@mkdir $(DISTDIR)/dl
	@echo -n "."
	@for subdir in $(SUBDIRS); do \
		echo -n "."; \
		mkdir $(DISTDIR)/$$subdir; \
		chmod 777 $(DISTDIR)/$$subdir; \
	done
	@echo "Done"
	@echo -n "Copying Core Distribution files"
	@for file in $(DISTFILES); do \
		echo -n "."; \
		cp -pr $$file $(DISTDIR)/$$file; \
	done
	@$(RM) $(DISTDIR)/config.h
	@$(RM) $(DISTDIR)/configure.in
	@echo "Done"
	@echo "Copying Library Distribution files"
	@for subdir in $(DISTLIBS); do \
		mkdir $(DISTDIR)/$$subdir; \
		chmod 777 $(DISTDIR)/$$subdir; \
		(cd $$subdir && $(MAKE) dist DISTDIR=../$(DISTDIR)/$$subdir && cd ..) || exit 1; \
	done
	@echo "Done"
	@echo "Copying Tools Distribution files"
	@(cd tools && $(MAKE) dist DISTDIR=../$(DISTDIR)/tools && cd ..) || exit 1;
	@echo "Done"
	@echo "Copying Module Distribution files"
	@for mod in $(DISTMOD); do \
		mkdir $(DISTDIR)/dl/$$mod; \
		(cd dl/$$mod && $(MAKE) dist DISTDIR=../../$(DISTDIR)/dl/$$mod && cd ../..) || exit 1; \
	done
	@echo "Done"
	@tar -czf $(DISTDIR).tar.gz $(DISTDIR)/*
#	@$(RM) $(DISTDIR)
	@echo "Tar file $(DISTDIR).tar.gz created. Freshmeat time!"

# Catch any changes in compilation options at the top of this file
$(OBJS): Makefile Makefile.inc
