prefix := /usr/local

bindir := $(prefix)/bin
sharedir := $(prefix)/share
docdir := $(sharedir)/doc
mandir := $(sharedir)/man

# Prefer XULRunner to the old Mozilla
ifeq ($(shell pkg-config --exists xulrunner-gtkmozembed && echo yes),yes)
    mozilla := xulrunner
else
    mozilla := mozilla
endif

moz_prefix := $(shell pkg-config --variable=prefix $(mozilla)-gtkmozembed)
moz_include_dir := $(moz_prefix)/include/$(mozilla)
moz_lib_dir := $(moz_prefix)/lib/$(mozilla)

moz_version := \
    $(shell sed 's/\#define MOZILLA_VERSION "\(.*\)"/\1/; t; d' \
            < $(moz_include_dir)/mozilla-config.h)
moz_version_major := $(word 1,$(subst ., ,$(moz_version)))
moz_version_minor := $(word 2,$(subst ., ,$(moz_version)))
moz_version_patchlevel := $(word 3,$(subst ., ,$(moz_version)))

CFLAGS := -ansi -Wall -Wunused -Wno-unused-parameter
CPPFLAGS := -D_REENTRANT
CXXFLAGS := -ansi -Wall -Wunused
LDFLAGS := -lpthread                                                   \
           $(shell pkg-config --libs gtkmm-2.0 $(mozilla)-gtkmozembed) \
           -Wl,-rpath -Wl,$(moz_lib_dir) -lexpat

ifdef NDEBUG
    CPPFLAGS += -DNDEBUG
else
    CFLAGS += -g
    CXXFLAGS += -g
    LDFLAGS += -g
endif

cxxsources := \
    auto_proc.cpp browser_widget.cpp child_iterator.cpp generate_dvd.cpp   \
    link_iterator.cpp null_prompt_service.cpp pixbufs.cpp style_sheets.cpp \
    temp_file.cpp video.cpp vob_list.cpp videolink.cpp x_frame_buffer.cpp  \
    xml_utils.cpp xpcom_support.cpp
csources := jquant2.c

videolink : $(cxxsources:%.cpp=.objs/%.o) $(csources:%.c=.objs/%.o)
	$(CXX) $(LDFLAGS) -o $@ $^

clean :
	rm -rf .objs
	rm -f videolink *~ .\#* *.orig *.rej svn-commit*.tmp

install :
	mkdir -p -m 755 $(DESTDIR)$(bindir)
	install -m 755 -s videolink $(DESTDIR)$(bindir)
	mkdir -p -m 755 $(DESTDIR)$(docdir)/videolink
	gzip -c9 README >$(DESTDIR)$(docdir)/videolink/README.gz
	gzip -c9 ChangeLog >$(DESTDIR)$(docdir)/videolink/ChangeLog.gz
	chmod 644 $(DESTDIR)$(docdir)/videolink/*.gz
	mkdir -p -m 755 $(DESTDIR)$(mandir)/man1
	gzip -c9 videolink.1 >$(DESTDIR)$(mandir)/man1/videolink.1.gz
	chmod 644 $(DESTDIR)$(mandir)/man1/videolink.1.gz
	mkdir -p -m 755 $(DESTDIR)$(sharedir)/videolink
	install -m 644 videolink.css $(DESTDIR)$(sharedir)/videolink

.PHONY : clean install

.objs/browser_widget.% : CPPFLAGS += -DMOZ_LIB_DIR='"$(moz_lib_dir)"'

.objs/videolink.% \
    : CPPFLAGS += -DVIDEOLINK_SHARE_DIR='"$(sharedir)/videolink"'

.objs/browser_widget.% .objs/generate_dvd.% .objs/pixbufs.% \
.objs/temp_file.% .objs/vob_list.% .objs/videolink.%        \
    : CPPFLAGS += $(shell pkg-config --cflags gtkmm-2.0)

.objs/browser_widget.% .objs/child_iterator.% .objs/link_iterator.%   \
.objs/null_prompt_service.% .objs/style_sheets.% .objs/videolink.%    \
.objs/xpcom_support.%                                                 \
    : CPPFLAGS += $(shell pkg-config --cflags $(mozilla)-gtkmozembed)
# Non-virtual destructors are fine in XPCOM interface classes since
# instances are only ever called by the Release function which is virtual.
.objs/browser_widget.% .objs/child_iterator.% .objs/link_iterator.% \
.objs/null_prompt_service.% .objs/style_sheets.% .objs/videolink.%  \
.objs/xpcom_support.%                                               \
    : CXXFLAGS += -Wno-non-virtual-dtor

# These dig a bit deeper into Mozilla
.objs/link_iterator.% .objs/null_prompt_service.% .objs/style_sheets.% \
.objs/videolink.%                                                      \
    : CPPFLAGS += $(addprefix -I$(moz_include_dir)/,                   \
                    content docshell dom gfx layout locale necko uconv \
                    webshell widget)                                   \
		  -DMOZILLA_INTERNAL_API                               \
                  -DMOZ_VERSION_MAJOR=$(moz_version_major)             \
                  -DMOZ_VERSION_MINOR=$(moz_version_minor)             \
                  -DMOZ_VERSION_PATCHLEVEL=$(moz_version_patchlevel)

.objs/%.d : %.cpp .objs/.created
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -MM -MF $@ $<

.objs/%.d : %.c .objs/.created
	$(CC) $(CFLAGS) $(CPPFLAGS) -MM -MF $@ $<

.objs/%.o : %.cpp .objs/.created
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ -c $<

.objs/%.o : %.c .objs/.created
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<

%/.created :
	mkdir -p $*
	touch $@

ifneq ($(MAKECMDGOALS),clean)
    include $(cxxsources:%.cpp=.objs/%.d) $(csources:%.c=.objs/%.d)
endif

.PRECIOUS : %/.created
