#
# Makefile for the Video Disk Recorder plugin bitstreamout
#

#
# The bitstreamout stream plugin.
#
PLUGIN = bitstreamout

### The version number of this plugin (taken from the main source file):

VERSION = $(shell grep '^static const char \*VERSION' bitstreamout.c | awk '{ print $$6 }' | sed -e 's/[";]//g')

### The optimization valid for the used CPU

CXXARCH  ?= $(shell make -sf Make.arch|grep -v 'make') -funroll-loops

### The C++ compiler and options:

CXX      ?= g++
CXXFLAGS ?= -O2 $(CXXARCH) -Wall -Woverloaded-virtual -pthread -fPIC

### The directory environment:

DVBDIR = ../../../../DVB
VDRDIR = ../../..
LIBDIR = ../../lib
ALSAIN =
TMPDIR = /tmp
VIDEODIR = /video
VIDEOLIB = /usr/local/lib/vdr
MANDIR   = /usr/local/man

### Allow user defined options to overwrite defaults:

-include $(VDRDIR)/Make.config

### The version number of VDR (taken from VDR's "config.h"):

VDRVERSION = $(shell grep 'define VDRVERSION ' $(VDRDIR)/config.h | awk '{ print $$3 }' | sed -e 's/"//g')

### The name of the distribution archive:

ARCHIVE = $(PLUGIN)-$(VERSION)
PACKAGE = vdr-$(ARCHIVE)

### Includes and Defines (add further entries here):

INCLUDES += $(EXTRA_INCLUDES) -I$(VDRDIR)/include -I$(DVBDIR)/include
ifneq ($(ALSAIN),)
INCLUDES += -I$(ALSAIN)
endif

DEFINES += -DPLUGIN_NAME_I18N='"$(PLUGIN)"'
DEFINES += -D_GNU_SOURCE

### The object files (add further files here):

OBJS = $(PLUGIN).o iec60958.o ac3.o dts.o lpcm.o channel.o replay.o spdif.o \
	shm_memory_tool.o mp2.o

### Data files like manual page and sample configuration

MANPAGE = vdr-bitstreamout.5

# Start

all: echo libvdr-$(PLUGIN).so

### Implicit rules:

%.o: %.c
	$(CXX) $(CXXFLAGS) -fPIC -DPIC -c $(DEFINES) $(INCLUDES) $<

# Dependencies:

MAKEDEP = g++ -MM -MG
DEPFILE = .dependencies
$(DEPFILE): Makefile
	@$(MAKEDEP) $(DEFINES) $(INCLUDES) $(OBJS:%.o=%.c) > $@

-include $(DEPFILE)

### Targets:
echo:
	@echo $(PLUGIN) Version $(VERSION)

libvdr-$(PLUGIN).so: $(OBJS)
	$(CXX) $(CXXFLAGS) -shared $(OBJS) -Wl,-soname -Wl,$@.$(VDRVERSION) -o $@ -lasound -lrt -lmad
	@cp $@ $(LIBDIR)/$@.$(VDRVERSION)
	@-strip --strip-unneeded $(LIBDIR)/$@.$(VDRVERSION)

install:  $(LIBDIR)/libvdr-$(PLUGIN).so.$(VDRVERSION)
	install -d -m 0755 $(DESTDIR)$(VIDEOLIB)/PLUGINS/lib
	install -m 0755 $< $(DESTDIR)$(VIDEOLIB)/PLUGINS/lib/
	install -d -m 0755 $(DESTDIR)$(MANDIR)/man5
	sed 's|@@VIDEODIR@@|$(VIDEODIR)|' < $(MANPAGE) > $(DESTDIR)$(MANDIR)/man5/$(MANPAGE)

dist: clean
	@-rm -rf $(TMPDIR)/$(ARCHIVE)
	@mkdir $(TMPDIR)/$(ARCHIVE)
	@cp -a * $(TMPDIR)/$(ARCHIVE)
	@-rm -rf $(TMPDIR)/$(PLUGIN)
	@ln -sf $(ARCHIVE) $(TMPDIR)/$(PLUGIN)
	@tar cjf $(PACKAGE).tar.bz2 -C $(TMPDIR) $(PLUGIN) $(ARCHIVE)
	@-rm -rf $(TMPDIR)/$(ARCHIVE) $(TMPDIR)/$(PLUGIN)
	@echo Distribution package created as $(PACKAGE).tar.bz2

clean:
	make -C tools clean
	@-rm -f $(OBJS) $(DEPFILE) *.so *.tar.bz2 core* *~ testt

vdrobj	=	$(shell ls $(VDRDIR)/*.o| grep -v vdr.o)
vdrlib  =	$(wildcard $(VDRDIR)/libsi/*.a $(VDRDIR)/libdtv/*/*.a)
testt:  CXXFLAGS += -DSPDIF_TEST=1 -g3
testt:	$(OBJS) testt.c shm_memory_tool.o spdif.o
	g++ $(CXXFLAGS) $(DEFINES) -o testt testt.c shm_memory_tool.o spdif.o iec60958.o $(vdrobj) \
	-I$(VDRDIR)/include \
	-lasound -ljpeg \
	$(vdrlib) -lrt
