# Folders
SOURCES  := src
TARGET   := target
BUILDDIR := $(TARGET)/build

# Compiler stuff
LIBS     := gstreamer-0.10
CPPFLAGS := -Isrc -g -Wall -Werror $(shell pkg-config --cflags $(LIBS))
LDFLAGS  := -lid3 $(shell pkg-config --libs $(LIBS))

# Project's stuff
PLUGIN   := id3v23mux
SVN_REPO := $(shell svn info | grep -E '^URL: ' | cut -f2 -d' ' | sed -e 's%/trunk%%')
VERSION  := $(shell head -n1 CHANGELOG.txt)


SAMPLE := samples/a.mp3

.PHONY: plugin
plugin: $(BUILDDIR) $(BUILDDIR)/libgst$(PLUGIN).so


$(BUILDDIR)/libgst$(PLUGIN).so: $(BUILDDIR)/gst$(PLUGIN).o $(BUILDDIR)/gsttaglibmux.o
	g++ -shared $(LDFLAGS) -o $@ $(BUILDDIR)/gst$(PLUGIN).o $(BUILDDIR)/gsttaglibmux.o


$(BUILDDIR)/gst$(PLUGIN).o: $(SOURCES)/gst$(PLUGIN).cc $(SOURCES)/gst$(PLUGIN).h $(SOURCES)/gsttaglibmux.c $(SOURCES)/gsttaglibmux.h
	g++ -DHAVE_CONFIG_H -fPIC -c $(CPPFLAGS) -o $@ $<


$(BUILDDIR)/gsttaglibmux.o: $(SOURCES)/gsttaglibmux.c $(SOURCES)/gsttaglibmux.h
	g++ -DHAVE_CONFIG_H -fPIC -c $(CPPFLAGS) -o $@ $<


.PHONY: test
test: plugin
	rm -f ~/.gstreamer-0.10/registry.*.xml || true
	gst-launch --gst-debug=$(PLUGIN):5 --gst-plugin-path=$(BUILDDIR) filesrc location=$(SAMPLE) ! id3demux ! $(PLUGIN) ! fakesink


.PHONY: test-write
test-write: $(TARGET) plugin
	rm -f ~/.gstreamer-0.10/registry.*.xml || true
	gst-launch --gst-debug=$(PLUGIN):5 --gst-plugin-path=$(BUILDDIR) filesrc location=$(SAMPLE) ! id3demux ! $(PLUGIN) ! filesink location=$(TARGET)/copy.mp3


.PHONY: install
install: plugin
	mkdir -p ~/.gstreamer-0.10/plugins/
	cp $(BUILDDIR)/libgst$(PLUGIN).so ~/.gstreamer-0.10/plugins/
	rm  ~/.gstreamer-0.10/registry.*.xml || true


.PHONY: tag
tag:
	-svn rm -m "Retagging version $(VERSION)" $(SVN_REPO)/tags/$(VERSION)
	svn cp -m "Tag for version $(VERSION)" $(SVN_REPO)/trunk $(SVN_REPO)/tags/$(VERSION)


.PHONY: clean
clean:
	rm -rf $(TARGET) > /dev/null 2>&1 || true


$(TARGET):
	mkdir -p $(TARGET) 2> /dev/null || true


$(BUILDDIR):
	mkdir -p $(BUILDDIR) 2> /dev/null || true
