# 
# Copyright (c) 2006 Ed Schouten <ed@fxq.nl>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# 
# $Id$
# 

PREFIX?=	/usr/local
OS?=		$(shell uname)
MANDIR=		$(PREFIX)/share/man

CFLAGS+=	$(shell pkg-config --cflags glib-2.0 gthread-2.0) \
		-I$(PREFIX)/include
LIBS=		$(shell pkg-config --libs glib-2.0 gthread-2.0) \
		-L$(PREFIX)/lib -lcurses
AUDIO_OUTPUT=	ao

# FreeBSD specific options
ifeq ($(OS),FreeBSD)
MANDIR=		$(PREFIX)/man
AUDIO_OUTPUT=	oss
endif

# NetBSD specific options
ifeq ($(OS),NetBSD)
PREFIX=		/usr/pkg
AUDIO_OUTPUT=	oss
endif

# Linux specific options
ifeq ($(OS),Linux)
CFLAGS+=	-D_GNU_SOURCE
PREFIX=		/usr
AUDIO_OUTPUT=	oss
endif

# Cygwin specific options
ifneq (,$(findstring Windows,$(OS)))
BUILD_OPTS+=	-DBUILD_STRCASESTR
BINARY_EXT=	.exe
PREFIX=		/usr
endif 

# Solaris specific options
ifeq ($(OS),SunOS)
BUILD_OPTS+=	-DBUILD_STRCASESTR
NO_GUI_RESIZE=	yes
NO_STRICT_GCC=	yes
endif

# Mac OS X specific options
ifeq ($(OS),Darwin)
BUILD_OPTS+=	-DBUILD_GUI_SIGWINCH_WRAPPER
PREFIX=		/sw
endif

# LibAO output specific options
ifeq ($(AUDIO_OUTPUT),ao)
LIBS+=		-lao
endif

# OSS output specific options
ifeq ($(AUDIO_OUTPUT),oss)
ifeq ($(OS),NetBSD)
LIBS+=		-lossaudio
endif
endif

# Tunables
ifndef NO_MP3
BUILD_OPTS+=	-DBUILD_MP3
HERRIE_OBJ+=	audio/mp3/mp3file.o \
		audio/mp3/nomad.o
LIBS+=		-lmad -lid3tag
endif
ifndef NO_VORBIS
BUILD_OPTS+=	-DBUILD_VORBIS
HERRIE_OBJ+=	audio/vorbis/vorbisfile.o
LIBS+=		-lvorbisfile -lvorbis -logg
endif
ifndef NO_SNDFILE
BUILD_OPTS+=	-DBUILD_SNDFILE
HERRIE_OBJ+=	audio/sndfile/sndfile.o
LIBS+=		-lsndfile
endif
ifndef NO_SCROBBLER
BUILD_OPTS+=	-DBUILD_SCROBBLER
HERRIE_OBJ+=	scrobbler/scrobbler.o scrobbler/scrobbler_hash.o \
		scrobbler/scrobbler_send.o
CFLAGS+=	$(shell curl-config --cflags)
LIBS+=		$(shell curl-config --libs) -lssl
endif
ifndef NO_LOCKUP
BUILD_OPTS+=	-DBUILD_LOCKUP
endif
ifndef NO_TRANS
TRANSDIR=	$(PREFIX)/share/locale

BUILD_OPTS+=	-DBUILD_TRANS -DTRANSDIR=\"$(TRANSDIR)\"
HERRIE_OBJ+=	trans.o

LANGUAGES=	lang/nl.mo
endif
ifndef NO_GUI_RESIZE
BUILD_OPTS+=	-DBUILD_GUI_RESIZE
endif

ifndef NO_STRICT_GCC
# XXX: libao dies with -Wstrict-prototypes
CFLAGS+=	-Wall -Werror -Wmissing-prototypes
endif

CFLAGS+=	-I. $(INCLUDE) $(BUILD_OPTS) \
		-DAPP_NAME=\"herrie\" -DAPP_VERSION=\"1.0\" \
		-DAUDIO_OUTPUT=\"$(AUDIO_OUTPUT)\"
LDFLAGS+=	$(LIBS)

HERRIE_OBJ+= audio/audio.o gui/gui_statbar.o gui/gui_msgbar.o \
	gui/gui_draw.o gui/gui_input.o gui/gui_browser.o \
	gui/gui_playq.o config.o playq.o subr.o \
	vfs/modules/vfs_playlist.o vfs/modules/vfs_dir.o \
	vfs/modules/vfs_file.o vfs/vfs.o herrie.o \
	\
	audio/output/$(AUDIO_OUTPUT).o

.PHONY: all install clean

all: herrie$(BINARY_EXT) herrie.1 $(LANGUAGES)

install: all
	install -s herrie$(BINARY_EXT) $(PREFIX)/bin/herrie$(BINARY_EXT)
	install -m 444 herrie.1 $(MANDIR)/man1/herrie.1
ifndef NO_TRANS
	mkdir -p $(TRANSDIR)/nl/LC_MESSAGES
	install -m 444 lang/nl.mo $(TRANSDIR)/nl/LC_MESSAGES/herrie.mo
endif

clean:
	rm -f herrie$(BINARY_EXT)
	find . \( -name '*.o' -or -name '*.mo' -or -name '*.core' \) \
	    -type f -exec rm -f {} \;

%.mo: %.po
	msgfmt -o $@ $<

herrie$(BINARY_EXT): $(HERRIE_OBJ)
	$(CC) -o herrie$(BINARY_EXT) $(HERRIE_OBJ) $(LDFLAGS)
