#
#  This is the Makefile for EGGDROP (the IRC bot)
#  You should never need to edit this.

SHELL = @SHELL@
top_srcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@

@SET_MAKE@
prefix = @prefix@
DEST = @DEST@
# GNU Make < 3.82 ignores these != statements, they are for bsd make and gnu make >= 4.0.
# gnu make 3.82 interprets a!= b as setting the variable a!.
# it seems to interpret != otherwise and throws an error.
DEST_PARENT_DIR!= dirname $(DEST)
DEST_PARENT!= readlink -f $(DEST_PARENT_DIR)
DEST_DIR!= basename $(DEST)
ABSDEST!= echo $(DEST_PARENT)/$(DEST_DIR)
# GNU Make
ABSDEST ?= $(abspath $(DEST))
EGGEXEC = @EGGEXEC@
EGG_CROSS_COMPILING = @EGG_CROSS_COMPILING@
EGGVERSION = @EGGVERSION@

# Extra compiler flags
#
# Things you can put here:
#
#   -Wall            if you're using gcc and it supports it
#                    (configure usually detects this anyway now)
#
#   -DDEBUG          generic debugging code
#   -DDEBUG_ASSERT   to enable assert debugging
#   -DDEBUG_MEM      to be able to debug memory allocation (.debug)
#   -DDEBUG_DNS      to enable dns.mod extra debugging information
#
# Debug defines can be set with configure now.
# See ./configure --help for more information.

CFLGS = @CFLGS@ @TCL_INCLUDE_SPEC@
DEBCFLGS = @DEBCFLGS@

# ./configure SHOULD set these; however you may need to tweak them to get
# modules to compile. If you do, PLEASE let the development team know so
# we can incorporate any required changes into the next release. You can
# contact us via eggdev@eggheads.org

# Defaults
CC = @CC@
LD = @CC@
STRIP = @STRIP@
RANLIB = @RANLIB@

# make 'modegg'
MOD_CC = @MOD_CC@
MOD_LD = @MOD_LD@
MOD_STRIP = @MOD_STRIP@

# make 'modules'
SHLIB_CC = @SHLIB_CC@
SHLIB_LD = @SHLIB_LD@
SHLIB_STRIP = @SHLIB_STRIP@
MOD_EXT = @MOD_EXT@

# Programs make install uses
LN_S = @LN_S@
INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@

# Stuff for Tcl
TCLLIB = @TCL_LIB_SPEC@

# Extra libraries
# XLIBS will be linked with everything
# MODULE_XLIBS will only be linked with the module objects
XLIBS = @SSL_LIBS@ @TCL_LIB_SPEC@ @LIBS@
MODULE_XLIBS = @MODULE_XLIBS@

# You shouldn't need to edit anything below this line.

modconf = $(top_srcdir)/misc/modconfig --top_srcdir=$(top_srcdir)

egg_test_run = if [ '$(EGG_CROSS_COMPILING)' = 'no' ]; \
               	then ./$(EGGEXEC) -v; \
               	else echo 'This build is a cross-compilation, skipping test run...'; \
               fi

post_config  =  echo "" && \
		echo "You can now compile the bot, using \"make\"." && \
		echo ""

post_iconfig =  $(modconf) update-depends && \
		$(modconf) Makefile && \
		(cd src/mod && $(MAKE_CONFIG) config) && \
		$(modconf) Makefile

run_config   =  $(modconf) modules-still-exist && \
		$(modconf) detect-modules && \
		$(modconf) update-depends && \
		$(modconf) Makefile && \
		cd src/mod && $(MAKE_CONFIG) config && cd ../../ && \
		$(modconf) Makefile && \
		$(post_config)

egg_install_msg =  echo "" && \
		   echo "Now run \"make install\" to install your bot." && \
		   echo ""

ls_exe =  echo "" && \
          echo "Eggdrop successfully compiled:" && ls -l $(EGGEXEC) && \
          echo ""

ls_mods = $(ls_exe) && echo "" && \
          echo "Modules successfully compiled:" && ls -l *.$(MOD_EXT) && \
          echo ""

show_test_run = echo "" && \
                echo "Test run of ./eggdrop -v:" && \
                $(egg_test_run) && \
                echo ""

# Equal for all build types
SMAKE_GENERAL_ARGS = 'MAKE=$(MAKE)' 'RANLIB=$(RANLIB)' 'XLIBS=$(XLIBS)' \
                     'TCLLIB=$(TCLLIB)' 'TCLINC=$(TCLINC)'

# Equal for each builds category (static, shlib, modules)
SMAKE_BUILD_STATIC_ARGS = 'CC=$(CC)' 'LD=$(LD)' 'MODOBJS=mod/*.o' \
                          'EGGEXEC=$(EGGEXEC)'
SMAKE_BUILD_SHLIB_ARGS = 'CC=$(MOD_CC)' 'LD=$(MOD_LD)' 'MODOBJS=' \
                         'EGGEXEC=$(EGGEXEC)'
SMAKE_BUILD_MODULES_ARGS = 'CC=$(SHLIB_CC)' 'LD=$(SHLIB_LD)' \
                           'MOD_EXT=$(MOD_EXT)' 'MODULE_XLIBS=$(MODULE_XLIBS)'

# Equal for each build type (standard, debug)
SMAKE_NO_DEBUG_ARGS = 'STRIP=$(STRIP)'
SMAKE_WITH_DEBUG_ARGS = 'STRIP=touch'

# Combined
MAKE_STATIC_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_STATIC_ARGS) \
                   $(SMAKE_NO_DEBUG_ARGS) 'EGGBUILD=(static version)' \
		   'CFLGS=$(CFLGS) -DSTATIC'
MAKE_SDEBUG_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_STATIC_ARGS) \
                   $(SMAKE_WITH_DEBUG_ARGS) 'EGGBUILD=(static debug version)' \
		   'CFLGS=$(CFLGS) $(DEBCFLGS) -DSTATIC'
MAKE_MODEGG_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_SHLIB_ARGS) \
                   $(SMAKE_NO_DEBUG_ARGS) 'EGGBUILD=(standard build)' \
		   'CFLGS=$(CFLGS)'
MAKE_DEBEGG_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_SHLIB_ARGS) \
                   $(SMAKE_WITH_DEBUG_ARGS) 'EGGBUILD=(debug build)' \
		   'CFLGS=$(CFLGS) $(DEBCFLGS)'
MAKE_MODULES_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_MODULES_ARGS) \
		    $(SMAKE_NO_DEBUG_ARGS) 'CFLGS=$(CFLGS)'
MAKE_DEBMOD_ARGS = $(SMAKE_GENERAL_ARGS) $(SMAKE_BUILD_MODULES_ARGS) \
		   $(SMAKE_WITH_DEBUG_ARGS) 'CFLGS=$(CFLGS) $(DEBCFLGS)'

# Special target types
MAKE_CONFIG = $(MAKE) 'MAKE=$(MAKE)'

MAKE_INSTALL = $(MAKE) 'MAKE=$(MAKE)' 'DEST=$(ABSDEST)'

MAKE_DEPEND = $(MAKE) 'MAKE=$(MAKE)' 'CC=$(CC)'

all: @DEFAULT_MAKE@

eggclean:
	@rm -f $(EGGEXEC) *.$(MOD_EXT) *.stamp core DEBUG *~
	@cd doc && $(MAKE) clean
	@cd scripts && $(MAKE) clean
	@cd src && $(MAKE) clean
	@cd src/md5 && $(MAKE) clean
	@cd src/compat && $(MAKE) clean

clean: eggclean
	@cd src/mod && $(MAKE) clean

distclean: eggclean clean-modconfig
	@cd src/mod && $(MAKE) distclean
	@rm -f Makefile doc/Makefile scripts/Makefile src/Makefile src/md5/Makefile src/compat/Makefile src/mod/Makefile
	@rm -f config.cache config.log config.status config.h lush.h eggint.h
	@rm -rf autom4te.cache

distrib:
	misc/releaseprep

depend:
	@cat /dev/null > lush.h
	+@cd src && $(MAKE_DEPEND) depend
	+@cd src/md5 && $(MAKE_DEPEND) depend
	+@cd src/mod && $(MAKE_DEPEND) depend
	+@cd src/compat && $(MAKE_DEPEND) depend

config:
	+@$(run_config)

new-iconfig:
	+@$(modconf) modules-still-exist && \
	$(modconf) update-depends && \
	$(modconf) -n configure && \
	$(post_iconfig) && \
	$(post_config)

iconfig:
	+@$(modconf) modules-still-exist && \
	$(modconf) detect-modules && \
	$(modconf) update-depends && \
	$(modconf) configure && \
	$(post_iconfig) && \
	$(post_config)

clean-modconfig:
	@rm -f .modules .known_modules

conftest:
	@if test ! -f .modules; then \
		echo ""; \
		echo "You have NOT configured modules yet. This has to be done before you"; \
		echo "can start compiling."; \
		echo ""; \
		echo "   Run \"make config\" or \"make iconfig\" now."; \
		echo ""; \
		exit 1; \
	fi

reconfig: clean-modconfig
	+@$(run_config)

# Modular builds

modtest: conftest
	@if [ -f EGGDROP.stamp ]; then \
		echo "You're trying to do a MODULE build of eggdrop when you've";\
		echo "already run 'make' for a static build.";\
		echo "You must first type \"make clean\" before you can build";\
		echo "a module version.";\
		exit 1;\
	fi && \
	echo "stamp" >EGGMOD.stamp

eggdrop: modtest
	@echo "" && echo "Making core eggdrop files..." && echo "" && \
	rm -f src/mod/mod.xlibs && \
	(cd src && $(MAKE) $(MAKE_MODEGG_ARGS) $(EGGEXEC)) && \
	echo "" && echo "Making modules..." && echo "" && \
	(cd src/mod && $(MAKE) $(MAKE_MODULES_ARGS) modules) && \
	$(show_test_run) && $(ls_mods) && $(egg_install_msg)

debug: modtest
	@echo "" && echo "Making core eggdrop files..." && echo "" && \
	rm -f src/mod/mod.xlibs && \
	(cd src && $(MAKE) $(MAKE_DEBEGG_ARGS) $(EGGEXEC)) && \
	echo "" && echo "Making modules..." && echo "" && \
	(cd src/mod && $(MAKE) $(MAKE_DEBMOD_ARGS) modules) && \
	$(show_test_run) && $(ls_mods) && $(egg_install_msg)

# Static builds

eggtest: conftest
	@if test -f EGGMOD.stamp; then \
		echo "You're trying to do a STATIC build of eggdrop when you've";\
		echo "already run 'make' for a module build.";\
		echo "You must first type \"make clean\" before you can build";\
		echo "a static version.";\
		exit 1;\
	fi && \
	echo "stamp" >EGGDROP.stamp

static: eggtest
	@echo "" && echo "Making module objects for static linking..." && \
	echo "" && rm -f src/mod/mod.xlibs && \
	(cd src/mod && $(MAKE) $(MAKE_STATIC_ARGS) static) && \
	echo "" && echo "Making core eggdrop for static linking..." && \
	echo "" && \
	(cd src && $(MAKE) $(MAKE_STATIC_ARGS) $(EGGEXEC)) && \
	$(show_test_run) && $(ls_exe) && $(egg_install_msg)

sdebug: eggtest
	@echo "" && echo "Making module objects for static linking..." && \
	echo "" && rm -f src/mod/mod.xlibs && \
	(cd src/mod && $(MAKE) $(MAKE_SDEBUG_ARGS) static) && \
	echo "" && echo "Making core eggdrop for static linking..." && \
	echo "" && \
	(cd src && $(MAKE) $(MAKE_SDEBUG_ARGS) $(EGGEXEC)) && \
	$(show_test_run) && $(ls_exe) && $(egg_install_msg)

# Install

install-end =	echo "" && echo "Installation completed." && echo "" && \
		echo "If you intend to use this bot as a botnet hub for other 1.8 bots using SSL," && \
		echo "please run \"make sslcert\" now. If you installed eggdrop to a custom path" && \
		echo "during the make install process, use the same DEST= option here." && \
		echo "" && \
		echo "You MUST ensure that you edit/verify your configuration file." && \
		echo "An example configuration file, eggdrop.conf, is distributed with Eggdrop." && \
		echo "" && \
		echo "Remember to change directory to $(DEST) before you proceed." && \
		echo ""

sslcert:
	@if test ! -d $(DEST); then \
		echo "You haven't installed eggdrop yet, or you installed using the DEST= flag.";\
		echo "Please run \"make install\" first.";\
		echo "If you have already run \"make install\" and used the DEST= option, please use it again here";\
		exit 1;\
	fi && \
	if test -f $(DEST)/eggdrop.key; then \
		cp $(DEST)/eggdrop.key $(DEST)/eggdrop.key~old; \
	fi && \
	if test -f $(DEST)/eggdrop.crt; then \
		cp $(DEST)/eggdrop.crt $(DEST)/eggdrop.crt~old; \
	fi && \
	openssl req -new -x509 -nodes -days 365 -keyout $(DEST)/eggdrop.key -out $(DEST)/eggdrop.crt -config ssl.conf; \

install: ainstall

dinstall: eggdrop
	@$(MAKE) ainstall

sinstall: static
	@$(MAKE) ainstall

ainstall: install-bin install-modules install-data install-text \
install-help install-language install-filesys install-doc install-scripts
	@$(install-end)

install-start:
	@if test ! -f $(EGGEXEC); then \
		echo ""; \
		echo "You haven't compiled Eggdrop yet."; \
		echo "To compile Eggdrop, use:"; \
		echo ""; \
		echo "  make [target]"; \
		echo ""; \
		echo "Valid targets: 'eggdrop', 'static', 'debug', 'sdebug'."; \
		echo "Default target: '@DEFAULT_MAKE@'."; \
		echo ""; \
		exit 1; \
	fi && \
	if test "x$(DEST)" = "x"; then \
		echo "You must specify a destination directory."; \
		echo "Example:"; \
		echo ""; \
		echo "  make install DEST=\"/home/wcc/mybot\""; \
		echo ""; \
		exit 1; \
	fi && \
	if test "$(DEST)" -ef .; then \
		echo "You are trying to install into the source directory."; \
		echo "That will not work. Please specify a different"; \
		echo "install directory with:"; \
		echo ""; \
		echo "make install DEST=\"/home/wcc/mybot\""; \
		echo ""; \
		exit 1; \
	fi && \
	echo "" && \
	$(egg_test_run) && \
	echo "" && echo "Installing in directory: '$(ABSDEST)'." && \
	echo "" && \
	if test ! -d $(DEST); then \
		echo "Creating directory '$(DEST)'."; \
		$(top_srcdir)/misc/mkinstalldirs $(DEST) >/dev/null; \
	fi

install-bin: install-start
	@if test -f $(DEST)/o$(EGGEXEC); then \
		rm -f $(DEST)/o$(EGGEXEC); \
	fi && \
	if test -h $(DEST)/$(EGGEXEC); then \
		echo "Removing symlink to archival eggdrop binary."; \
		rm -f $(DEST)/$(EGGEXEC); \
	fi && \
	if test -f $(DEST)/$(EGGEXEC); then \
		echo "Renaming old '$(EGGEXEC)' executable to 'o$(EGGEXEC)'."; \
		mv -f $(DEST)/$(EGGEXEC) $(DEST)/o$(EGGEXEC); \
	fi && \
	echo "Copying new '$(EGGEXEC)' executable and creating symlink." && \
	$(INSTALL_PROGRAM) $(EGGEXEC) $(DEST)/$(EGGEXEC)-$(EGGVERSION) && \
	(cd $(DEST) && $(LN_S) $(EGGEXEC)-$(EGGVERSION) $(EGGEXEC))

install-modules: install-start
	@if test -h $(DEST)/modules; then \
		echo "Removing symlink to archival modules subdirectory."; \
		rm -f $(DEST)/modules; \
	fi && \
	if test -d $(DEST)/modules; then \
		echo "Moving old modules into 'modules.old' subdirectory."; \
		rm -rf $(DEST)/modules.old; \
		mv -f $(DEST)/modules $(DEST)/modules.old; \
	fi && \
	if test ! "x`echo *.$(MOD_EXT)`" = "x*.$(MOD_EXT)"; then \
		if test ! -d $(DEST)/modules-$(EGGVERSION); then \
			echo "Creating 'modules-$(EGGVERSION)' subdirectory and symlink."; \
			$(top_srcdir)/misc/mkinstalldirs $(DEST)/modules-$(EGGVERSION) >/dev/null; \
		fi; \
		(cd $(DEST) && $(LN_S) modules-$(EGGVERSION) modules); \
		echo "Copying new modules."; \
		for i in *.$(MOD_EXT); do \
			$(INSTALL_PROGRAM) $$i $(DEST)/modules-$(EGGVERSION)/; \
		done; \
	fi

install-data: install-start
	@if test ! -f $(DEST)/eggdrop.conf; then \
		$(INSTALL_DATA) $(srcdir)/eggdrop.conf $(DEST)/; \
	fi
	@if test ! -f $(DEST)/eggdrop-basic.conf; then \
		$(INSTALL_DATA) $(srcdir)/eggdrop-basic.conf $(DEST)/; \
	fi
	@if test ! -d $(DEST)/logs; then \
		echo "Creating 'logs' subdirectory."; \
		$(top_srcdir)/misc/mkinstalldirs $(DEST)/logs >/dev/null; \
		$(INSTALL_DATA) $(srcdir)/logs/CONTENTS $(DEST)/logs/; \
	fi

install-text: install-start
	@if test ! -d $(DEST)/text; then \
		echo "Creating 'text' subdirectory."; \
		$(top_srcdir)/misc/mkinstalldirs $(DEST)/text >/dev/null; \
	fi && \
	if test ! -f $(DEST)/text/motd; then \
		$(INSTALL_DATA) $(srcdir)/text/motd $(DEST)/text/; \
	fi && \
	if test ! -f $(DEST)/text/banner; then \
		$(INSTALL_DATA) $(srcdir)/text/banner $(DEST)/text/; \
	fi

install-help: install-start
	+@echo "Copying help files." && \
	if test ! "x`echo $(srcdir)/help/*.help`" = "x$(srcdir)/help/*.help"; then \
		if test ! -d $(DEST)/help; then \
			echo "Creating 'help' subdirectory."; \
			$(top_srcdir)/misc/mkinstalldirs $(DEST)/help >/dev/null; \
		fi; \
		for i in $(srcdir)/help/*.help; do \
			$(INSTALL_DATA) $$i $(DEST)/help/; \
		done; \
	fi && \
	if test ! "x`echo $(srcdir)/help/msg/*.help`" = "x$(srcdir)/help/msg/*.help"; then \
		if test ! -d $(DEST)/help/msg; then \
			echo "Creating 'help/msg' subdirectory."; \
			$(top_srcdir)/misc/mkinstalldirs $(DEST)/help/msg >/dev/null; \
		fi; \
		for i in $(srcdir)/help/msg/*.help; do \
			$(INSTALL_DATA) $$i $(DEST)/help/msg/; \
		done; \
	fi && \
	if test ! "x`echo $(srcdir)/help/set/*.help`" = "x$(srcdir)/help/set/*.help"; then \
		if test ! -d $(DEST)/help/set; then \
			echo "Creating 'help/set' subdirectory."; \
			$(top_srcdir)/misc/mkinstalldirs $(DEST)/help/set >/dev/null; \
		fi; \
		for i in $(srcdir)/help/set/*.help; do \
			$(INSTALL_DATA) $$i $(DEST)/help/set/; \
		done; \
	fi && \
	cd src/mod/ && $(MAKE_INSTALL) install-help

install-language: install-start
	+@echo "Copying language files." && \
	if test ! "x`echo $(srcdir)/language/*.lang`" = "x$(srcdir)/language/*.lang"; then \
		if test ! -d $(DEST)/language; then \
			echo "Creating 'language' subdirectory."; \
			$(top_srcdir)/misc/mkinstalldirs $(DEST)/language >/dev/null; \
		fi; \
		for i in $(srcdir)/language/*.lang; do \
			$(INSTALL_DATA) $$i $(DEST)/language/; \
		done; \
	fi && \
	cd src/mod && $(MAKE_INSTALL) install-language

install-filesys: install-start
	@if test ! -d $(DEST)/filesys; then \
		echo "Creating skeletal filesystem subdirectories."; \
		$(top_srcdir)/misc/mkinstalldirs $(DEST)/filesys >/dev/null; \
		$(top_srcdir)/misc/mkinstalldirs $(DEST)/filesys/incoming >/dev/null; \
	fi

install-doc: install-start
	+@$(INSTALL_DATA) $(srcdir)/README $(DEST) && \
	cd doc/ && $(MAKE_INSTALL) install

install-scripts: install-start
	+@cd scripts/ && $(MAKE_INSTALL) install

#safety hash
