##
##  Makefile -- Build procedure for mod_musicindex Apache module
##  Autogenerated via ``apxs -n musicindex -g''.
##  Heavily tweaked by Thibaut VARENE.
##  $Id: Makefile,v 1.41 2004/07/08 18:47:23 boudinr Exp $
##
##  @todo check BSD
##  @todo autoconf/automake (i18n/apache2)

#   the tools
CC = gcc
INSTALL = install

ifeq ($(APACHE_VERSION),2)
APXS = apxs2
APACHECTL = apache2ctl
APRCONFIG = apr-config
APINCDIR = $(shell $(APRCONFIG) --includes)
APCFLAGS = $(shell $(APRCONFIG) --cflags | sed -e "s/-g //" -e "s/-O.//" -e "s/-W[a-z]*//")
APCPPFLAGS = $(shell $(APRCONFIG) --cppflags)
#APLIBXDIR = $(shell $(APRCONFIG) --installbuilddir)
APLIBXDIR = $(shell $(APXS) -q LIBEXECDIR)
else
APXS = apxs
APACHECTL = apachectl
APCFLAGS = $(shell $(APXS) -q CFLAGS_SHLIB | sed -e "s/-g //" -e "s/-O.//" -e "s/-W[a-z]*//")
APLIBXDIR = $(shell $(APXS) -q LIBEXECDIR)
endif

#   additional user defines, includes and libraries
#CPPFLAGS = 
#INC = 
#LIB = 
CFLAGS = -O2 -Wall
DOCROOT = /var/www

#   our own defines
UNAME := $(shell uname)
REGULAR := $(shell if [ ! -z "`uname | grep -i Linux`" ] || \
		[ ! -z "`uname | grep -i SunOS`" ] || \
		[ ! -z "`uname | grep -i NetBSD`" ]; then \
		echo Y; else echo N; fi)
INCLUDEDIR = -I$(shell $(APXS) -q INCLUDEDIR)
LIBEXECDIR = $(APLIBXDIR)
LIBS_SHLIB = $(shell $(APXS) -q LIBS_SHLIB)

#   the final shared object
MOD_SO := mod_musicindex.so

#   the intermediate objects
objects := config.o html.o inf.o playlist.o playlist-flac.o playlist-mp3.o playlist-ogg.o mod_musicindex.o cache.o

#   the default target
all: Makefile.dep $(MOD_SO)

#   compile the DSO file
$(MOD_SO): $(objects)
ifeq ($(REGULAR),Y)
	$(CC) -shared $(CFLAGS) $(CPPFLAGS) $(LIB) -o $@ $^
else
ifneq ($(LIBS_SHLIB),)
	@$(APXS) -S CC="$(CC)" -S LIBS_SHLIB="$(LIB)" -S CFLAGS="$(CFLAGS)" $(INC) $(CPPFLAGS) -o $@ -c $^
else
	@$(APXS) -S CC="$(CC)" -S CFLAGS="$(CFLAGS)" $(INC) $(LIB) $(CPPFLAGS) -o $@ -c $^
endif
endif

#   install the DSO and the other files where needed
install: all
	$(INSTALL) -d $(DESTDIR)$(DOCROOT)/musicindex $(DESTDIR)$(LIBEXECDIR)
	$(INSTALL) -m 0644 musicindex/*.* $(DESTDIR)$(DOCROOT)/musicindex
	$(INSTALL) -m 0644 $(MOD_SO) $(DESTDIR)$(LIBEXECDIR)
	@echo -e "\nDon't forget to edit your httpd.conf (see README for details):\n \
		LoadModule musicindex_module $(DESTDIR)$(LIBEXECDIR)/$(MOD_SO)"

#   how to compile the objects
%.o: %.c %.h mod_musicindex.h
ifeq ($(REGULAR),Y)
	$(CC) $(CPPFLAGS) $(INC) $(CFLAGS) -o $@ -c $<
else
# that's a dirty hack to avoid rebuilding everything when only one file was changed...
	@$(APXS) -S CC="$(CC)" -S CFLAGS="$(CFLAGS)" $(INC) $(CPPFLAGS) -o temp.so -c $<
	@rm temp.so
endif

#   dependencies
Makefile.dep: *.c
	@tools/mkdep.sh

#   PHONY targets
.PHONY: doc doc_clean clean dist_clean

#   documentation
doc: doc_clean
	doxygen

#   cleanup
clean: doc_clean
	-rm -f Makefile.dep $(objects) $(MOD_SO) temp.so

doc_clean:
	-rm -rf doc

distclean: clean
	-rm -f *.pws
	
#   PHONY targets
.PHONY: start restart stop

#   the general Apache start/restart/stop procedures
start:
	$(APACHECTL) start
restart:
	$(APACHECTL) graceful
stop:
	$(APACHECTL) stop

#   conditionnal assignements:

ifneq ($(UNAME),Darwin)
CFLAGS += -pedantic
endif

ifeq ($(UNAME),SunOS)
APXS = /usr/apache/bin/apxs
APACHECTL = /usr/apache/bin/apachectl
INSTALL = ginstall
CPPFLAGS = -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -DEAPI
override CFLAGS += -fPIC
DOCROOT = /var/apache/htdocs
else
ifeq ($(UNAME),Darwin)
DOCROOT = /Library/WebServer/Documents
endif
override CFLAGS += $(APCFLAGS)
override CPPFLAGS += -DEAPI
endif

ifeq ($(APACHE_VERSION),2)
# piece of shit, apxs2 doesn't declare -fPIC on GNU/Linux. Won't work with gcc < 3.X
override CFLAGS += $(APCFLAGS) -std=gnu99 -fPIC
override CPPFLAGS += $(APCPPFLAGS)
endif

override INC += $(INCLUDEDIR) $(APINCDIR)
override LIB += -lvorbis -lvorbisfile -lid3tag -lmad -lFLAC

-include Makefile.dep

# special debian target to help us prepare the stuff
debian:
	mkdir apache1 apache2
	cp -r musicindex/ tools/ Makefile *.[ch] apache1
	cp -r musicindex/ tools/ Makefile *.[ch] apache2
	
.PHONY: debian
