##
##  Makefile -- Build procedure for mod_musicindex Apache module
##  Autogenerated via ``apxs -n musicindex -g''.
##  Heavily tweaked by Thibaut VARENE.
##  $Id: Makefile,v 1.39 2004/05/27 12:25:14 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
APRINCDIR = $(shell $(APRCONFIG) --includes)
else
APXS = apxs
APACHECTL = apachectl
endif

#   additional user defines, includes and libraries
#DEF = 
#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`" ]; then \
		echo Y; else echo N; fi)
INCLUDEDIR = $(shell $(APXS) -q INCLUDEDIR)
LIBEXECDIR = $(shell $(APXS) -q LIBEXECDIR)
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-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) $(DEF) $(LIB) -o $@ $^
else
ifneq ($(LIBS_SHLIB),)
	@$(APXS) -S CC="$(CC)" -S LIBS_SHLIB="$(LIB)" -S CFLAGS="$(CFLAGS)" $(INC) $(DEF) -o $@ -c $^
else
	@$(APXS) -S CC="$(CC)" -S CFLAGS="$(CFLAGS)" $(INC) $(LIB) $(DEF) -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) $(DEF) $(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) $(DEF) -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
DEF = -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 += $(shell $(APXS) -q CFLAGS_SHLIB | sed -e "s/-g //" -e "s/-O.//" -e "s/-W[a-z]*//") -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 += -std=gnu99 -fPIC
endif

override INC += -I$(INCLUDEDIR) $(APRINCDIR)
override LIB += -lvorbis -lvorbisfile -lid3tag -lmad

-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
