# configure daapd here

HOWL_ENABLE = 1
ZLIB_ENABLE = 1
MPEG4_ENABLE = 0


# no need to touch anything below this line
# -----------------------------------------

CC = g++
MAKE = $(MAKE_COMMAND)
TARGET = daapd
DEPS = daaplib_ libhttpd_
OBJS = daapd.o db.o dboutput.o songcache.o parsemp3.o util.o
LIBS = -ldaaplib -lhttpd-persistent -lid3tag -lz
LIBPATH = -L. -L./daaplib/src -L./libhttpd/src  -L/usr/local/lib
INCPATH = -I. -I./daaplib/include -I./libhttpd/src  -I/usr/local/include
DEPLOY = /usr/local
CFLAGS = -Wall -Wno-multichar

# HOWL
ifeq ($(HOWL_ENABLE),1)
	HOWLDIRS := $(sort $(wildcard /usr/local/include/howl*) )
ifeq ($(words $(HOWLDIRS) ), 0)
$(error howl not found in /usr/local/include. Install howl or disable it in the makefile)
endif
	HOWLRECENT := $(word $(words $(HOWLDIRS)),$(HOWLDIRS) )
	INCPATH := $(INCPATH) -I$(HOWLRECENT)
	LIBS := $(LIBS) -lhowl
	CFLAGS := $(CFLAGS) -DHOWL_ENABLE
	ifneq ($(shell $(CC) -E -dM - < /dev/null | grep __APPLE__),)
		LIBS := $(LIBS) -framework CoreFoundation
	endif
endif

# MPEG4IP
ifeq ($(MPEG4_ENABLE),1)
	INCPATH := $(INCPATH)
	LIBS := $(LIBS) -lmp4v2
	CFLAGS := $(CFLAGS) -DMPEG4_ENABLE
endif

# platform specific stuff
ifneq ($(shell $(CC) -E -dM - < /dev/null | grep __sun__),)
	LIBS := $(LIBS) -lsocket -lnsl
endif

ifneq ($(shell $(CC) -E -dM - < /dev/null | grep __FreeBSD__),)
	LIBS := $(LIBS) -pthread
else
	LIBS := $(LIBS) -lpthread
endif

ifneq ($(shell $(CC) -E -dM - < /dev/null | grep __sgi__),)
	LIBS := $(LIBS) /usr/lib/snprintf.o
endif


.cc.o:
	$(CC) $(CFLAGS) $(INCPATH) -c $<

all: $(DEPS) $(TARGET)

$(TARGET): $(OBJS)
	$(CC) $(CFLAGS) $(LIBPATH) -o $(TARGET) $(OBJS) $(LIBS)

$(OBJS): types.h dboutput.h songcache.h parsemp3.h

daaplib_:
	cd daaplib/src && $(MAKE)

libhttpd_:
	if test ! -e libhttpd/Site.mm; then cd libhttpd && ./configure; fi
	cd libhttpd && $(MAKE)

clean:
	if test -e libhttpd/Site.mm; then cd libhttpd && $(MAKE) very-clean; fi
	cd daaplib/src && $(MAKE) clean
	rm $(OBJS) $(TARGET)

install: $(DEPS) $(TARGET)
	cd libhttpd && $(MAKE) install
	cd daaplib/src && $(MAKE) install
	cp $(TARGET) $(DEPLOY)/bin
	chmod a+rx $(DEPLOY)/bin/$(TARGET)
	cp $(TARGET).8 $(DEPLOY)/man/man8
