#
# Makefile for a Video Disk Recorder plugin
#
# $Id: Makefile,v 1.42 2008/04/14 02:52:10 lucke Exp $

# The official name of this plugin.
# This name will be used in the '-P...' option of VDR to load the plugin.
# By default the main source file also carries this name.
#
PLUGIN = softdevice

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

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

### The C++ compiler and options:

# -----------------------------------------------------------------------------
#
#DVBDIR 	   = ../../../../dvb-kernel
#DVBDIR 	   = ../../../../DVB

DVBDIR 	   = ../../../../DVB

VDRDIR     = ../../..
LIBDIR     = ../../lib
TMPDIR     = /tmp
PLUGINLIBDIR = ./PLUGINS/lib

# for older file-utils, option -f had the same effect
CPOPTS = --remove-destination

-include config.mak

# uncomment to build the shared memory video server
#
#SHM_SUPPORT = 1


ifndef WITH_CONFIGURE

# uncomment ONE of these lines if you want to enable the support

###############################
# don't forget to "make clean" if you change something
###############################

#XV support by Stefan Lucke
XV_SUPPORT=1

# DFB is very experimental (works only with my matrox G200, comments are welcome)
DFB_SUPPORT=1

# FB is unaccellerated and should work with any FB with 16bit color depth
FB_SUPPORT=1

# Vidix support by Vadim Catana
#VIDIX_SUPPORT=1

# Set this if you want to use DPMS
LIBXDPMS_SUPPORT = 1

# Set this if you want to use alsa audio
ALSA_SUPPORT = 1

# Set this if you want to be able to toggle suspend mode by keyboard (XV only)
SUSPEND_BY_KEY = 1

# Enable the usage from some deinterlacing pp-filters of libavcodec
# ffmpeg must be compiled with the options --enable-pp --enable-gpl
# to use this feature !
# Then you have to install the header files with 'make install' in
# the directory ffmpeg/libavcodec/libpostproc .
# I case you build static libs only (default) you have to copy them manuell
# to your target directories (example with /usr/local [default] as target):
#
#   mkdir -p /usr/local/include/postproc
#   install -m 644 postprocess.h /usr/local/include/postproc/postprocess.h
#   cp libpostproc.a /usr/local/lib/
#
PP_LIBAVCODEC=1

# -----------------------------------------------------------------------------
# if you want output methods build as a single lib comment the following line
USE_SUBPLUGINS = 1

# -----------------------------------------------------------------------------
# VIDIX specific definitions
# installation directory
#
#VIDIX_DIR    = /opt/vidix
#VIDIX_DIR    = /usr/local/

VIDIX_DIR    = /usr/local

# -----------------------------------------------------------------------------
# Set up these paths to your enviroment!
#
#FFMPEGCFLAGS=-I/opt/ffmpeg
#FFMPEGCFLAGS=-I../../../../ffmpeg/libavcodec
#
# If you installed ffmpeg's lib (make installlib)
FFMPEGCFLAGS=-I/usr/include/libavcodec/
#FFMPEGCFLAGS=-I/home/extra/src/video/ffmpeg-0.4.8/libavcodec
#
#
# DON'T touch this:
FFMPEGLIBS = -lavcodec -lavformat -lz
#
# -----------------------------------------------------------------------------
# Depending on your ffmpeg lib you may need some additional libs
# uncomment one of the following when you'll get unknow symbol messages:
#
# .. undefined symbol: lame_encode_buffer_interleaved
#FFMPEGLIBS += -lmp3lame

#
# .. undefined symbol: vorbis_encode_init
#FFMPEGLIBS += -lvorbisenc -lvorbis

#tuning options

#MMX is needed for FB-output!
DEFINES	   += -DUSE_MMX

# uncomment this line if you do not have MMX2
DEFINES	   += -DUSE_MMX2

### Allow user defined options to overwrite defaults:

-include $(VDRDIR)/Make.config

# #############################################################################
# moved some defines to this section, as they are generated by
# configure too.
#

ifdef USE_SUBPLUGINS
  DEFINES += -DUSE_SUBPLUGINS
endif

ifdef DFB_SUPPORT
  DFB_LIBS  = -L/usr/local/lib -ldfb++
  DFB_CFLAGS = -I/usr/local/include/dfb++ -I/usr/local/include/directfb
  DEFINES  += -DDFB_SUPPORT
endif

ifdef CLE266_SUPPORT
  CLE266_LIBS = -L/usr/local/lib -lcle266mpegdec
  CLE266_CFLAGS = -I/usr/local/include
  DEFINES  += -DHAVE_CLE266_MPEG_DECODER
endif

ifdef XV_SUPPORT
  XV_LIBS   = -L/usr/X11R6/lib -lXi -lXext -lX11 -lm -lXv
  ifdef LIBXDPMS_SUPPORT
    DEFINES += -DLIBXDPMS_SUPPORT
  endif
  DEFINES += -DXV_SUPPORT
endif

ifdef FB_SUPPORT
  DEFINES += -DFB_SUPPORT
endif

ifdef PP_LIBAVCODEC
  DEFINES     += -DPP_LIBAVCODEC
  FFMPEGLIBS  += -lpostproc
  INCLUDES    += -Ipostproc/
endif

ifdef SUSPEND_BY_KEY
  DEFINES     += -DSUSPEND_BY_KEY
endif

ifdef VIDIX_SUPPORT
  VIDIX_LIBS  = -L$(VIDIX_DIR)/lib -lvidix
  VIDIX_CFLAGS = -I$(VIDIX_DIR)/include/vidix
  DEFINES     += -DVIDIX_SUPPORT -DVIDIX_DIR=\"$(VIDIX_DIR)/lib/vidix/\"
endif

ifdef ALSA_SUPPORT
  DEFINES += -DALSA_SUPPORT
endif

# #############################################################################
# end of section without configure
#
else

  DEFINES += -DHAVE_CONFIG

### Allow user defined options to overwrite defaults:

-include $(VDRDIR)/Make.config

endif  # WITH_CONFIGURE

### normally you shoudn't have to touch something below this line

CXX      ?= g++
CXXFLAGS ?= -O2 -g -Wall -fPIC -Woverloaded-virtual 
CXXFLAGS += $(ADD_CXXFLAGS)
DEFINES  += -D__STDC_CONSTANT_MACROS

SHARED_FLAG ?= -shared

### The directory environment:


### 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')
APIVERSION = $(shell sed -ne '/define APIVERSION/ { s/^.*"\(.*\)".*$$/\1/; p }' $(VDRDIR)/config.h)

ifeq ($(APIVERSION),)
	APIVERSION = $(VDRVERSION)
endif

### The name of the distribution archive:

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

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

INCLUDES += -I$(VDRDIR)/include -I$(DVBDIR)/include $(FFMPEGCFLAGS)

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

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

TARGETS = libvdr-$(PLUGIN).so
LIBS = $(FFMPEGLIBS)
OBJS = $(PLUGIN).o utils.o i18n.o video.o mpeg2decoder.o \
				audio.o video-dummy.o \
				setup-softdevice.o setup-softdevice-menu.o \
				setup-softlog.o setup-softlog-menu.o\
				sync-timer.o SoftOsd.o PicBuffer.o VideoFilter.o
ALL_OBJS = $(OBJS)

ifdef MACOSAO_SUPPORT
  OBJS += audio-macos.o
  LIBS += -framework Carbon -framework CoreAudio -framework AudioUnit -framework AudioToolbox 
endif

ifdef QUARTZ_SUPPORT
  OBJS += video-quartz.o
  LIBS += -framework Carbon -framework AGL -framework OpenGL 
endif

ifdef ALSA_SUPPORT
  LIBS += -lasound
  OBJS += audio-alsa.o audio-ac3pt.o
endif

ifdef OSS_SUPPORT
  OBJS += audio-oss.o
endif

ifdef DFB_SUPPORT
  ifdef USE_SUBPLUGINS
    DFB_OBJS  = utils.o video.o video-dfb.o PicBuffer.o
    ALL_OBJS += $(DFB_OBJS)
    LIBS     += $(CLE266_LIBS)
    DFB_LIBS += $(CLE266_LIBS)
    TARGETS  += lib$(PLUGIN)-dfb.so
  else
    OBJS     += video-dfb.o
    LIBS     += $(DFB_LIBS) $(CLE266_LIBS)
  endif
  INCLUDES += $(DFB_CFLAGS) $(CLE266_CFLAGS)
endif

ifdef FB_SUPPORT
  ifdef USE_SUBPLUGINS
    FB_LIBS   =
    FB_OBJS   = utils.o video.o video-fb.o PicBuffer.o
    ALL_OBJS  += $(FB_OBJS)
    TARGETS   += lib$(PLUGIN)-fb.so
  else
    OBJS 	+= video-fb.o
  endif
endif

ifdef XV_SUPPORT
  ifdef USE_SUBPLUGINS
    XV_OBJS   = utils.o video.o video-xv.o xscreensaver.o PicBuffer.o
    ALL_OBJS  += $(XV_OBJS)
    TARGETS   += lib$(PLUGIN)-xv.so
  else
    OBJS 	+= video-xv.o xscreensaver.o
    LIBS 	+= $(XV_LIBS)
  endif
endif

ifdef VIDIX_SUPPORT
  ifdef USE_SUBPLUGINS
    VIDIX_OBJS  = utils.o video.o video-vidix.o PicBuffer.o
    ALL_OBJS    += $(VIDIX_OBJS)
    TARGETS     += lib$(PLUGIN)-vidix.so
  else
    OBJS        += video-vidix.o
    LIBS        += -lvidix
  endif
  INCLUDES    += $(VIDIX_CFLAGS)
endif

ifdef SHM_SUPPORT
  ifdef USE_SUBPLUGINS
    SHM_OBJS  = utils.o video.o video-shm.o PicBuffer.o
    ALL_OBJS    += $(SHM_OBJS)
    TARGETS     += lib$(PLUGIN)-shm.so
  else
    OBJS += video-shm.o
  endif
  DEFINES += -DSHM_SUPPORT
endif

ifdef SHMCLIENT_SUPPORT
  TARGETS += ShmClient
endif

ifdef MACVDRCLIENT_SUPPORT
  TARGETS += MacVdrClient
endif

### Implicit rules:


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

# Dependencies:

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

-include $(DEPFILE)

### Internationalization (I18N):

PODIR     = po
LOCALEDIR = $(VDRDIR)/locale
I18Npo    = $(wildcard $(PODIR)/*.po)
I18Nmsgs  = $(addprefix $(LOCALEDIR)/, $(addsuffix /LC_MESSAGES/vdr-$(PLUGIN).mo, $(notdir $(foreach file, $(I18Npo), $(basename $(file))))))
I18Npot   = $(PODIR)/$(PLUGIN).pot

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

$(I18Npot): $(wildcard *.c)
	xgettext -C -cTRANSLATORS --no-wrap --no-location -k -ktr -ktrNOOP --msgid-bugs-address='<softdevice-devel at lists.berlios.de>' -o $@ $^

%.po: $(I18Npot)
	msgmerge -U --no-wrap --no-location --backup=none -q $@ $<
	@touch $@

$(I18Nmsgs): $(LOCALEDIR)/%/LC_MESSAGES/vdr-$(PLUGIN).mo: $(PODIR)/%.mo
	@mkdir -p $(dir $@)
	cp $< $@

.PHONY: i18n
i18n: $(I18Nmsgs)

### Targets:

all: $(TARGETS) i18n

libvdr-$(PLUGIN).so: $(OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(OBJS) $(LIBS) -o $@
	@cp $(CPOTS) $@ $(LIBDIR)/$@.$(APIVERSION)

ifdef USE_SUBPLUGINS

lib$(PLUGIN)-dfb.so: $(DFB_OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(DFB_OBJS) $(DFB_LIBS) -o $@
	@cp $(CPOPTS) $@ $(LIBDIR)/$@.$(APIVERSION)

lib$(PLUGIN)-fb.so: $(FB_OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(FB_OBJS) $(FB_LIBS) -o $@
	@cp $(CPOPTS) $@ $(LIBDIR)/$@.$(APIVERSION)

lib$(PLUGIN)-xv.so: $(XV_OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(XV_OBJS) $(XV_LIBS) -o $@
	@cp $(CPOPTS) $@ $(LIBDIR)/$@.$(APIVERSION)

lib$(PLUGIN)-vidix.so: $(VIDIX_OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(VIDIX_OBJS) $(VIDIX_LIBS) -o $@
	@cp $(CPOPTS) $@ $(LIBDIR)/$@.$(APIVERSION)

lib$(PLUGIN)-shm.so: $(SHM_OBJS)
	$(CXX) $(CXXFLAGS) $(SHARED_FLAG) $(SHM_OBJS) $(SHM_LIBS) -o $@
	@cp $(CPOPTS) $@ $(LIBDIR)/$@.$(APIVERSION)

endif

dist: clean
	@-rm -rf $(TMPDIR)/$(ARCHIVE)
	@mkdir $(TMPDIR)/$(ARCHIVE)
	@cp -a * $(TMPDIR)/$(ARCHIVE)
	@tar czf $(PACKAGE).tgz -C $(TMPDIR) $(ARCHIVE)
	@-rm -rf $(TMPDIR)/$(ARCHIVE)
	@echo Distribution package created as $(PACKAGE).tgz

dist-clean: clean
	@-rm -f config.h config.mak

clean:
	@-rm -f $(PODIR)/*.mo $(PODIR)/*.pot
	@-rm -f $(OBJS) $(DEPFILE) ShmClient MacVdrClient *.so *.o *.tgz core* *~

VDR_SHM_CLIENT_OBJS = ../../../tools.o ../../../thread.o \
		      ../../../remote.o ../../../i18n.o ../../../keys.o \
		      ../../../config.o ../../../plugin.o\
		      ../../../sources.o
SHM_CLIENT_OBJS  = video_shm.o video-xv_shm.o \
			 setup-softdevice_shm.o setup-softlog_shm.o \
		   xscreensaver_shm.o utils_shm.o VdrReplacements_shm.o\
		   ShmClient_shm.o PicBuffer_shm.o

%_shm.o: %.c
	$(CXX) $(CXXFLAGS) -c $(DEFINES) -DSTAND_ALONE $(INCLUDES) $< -o $@

ShmClient: $(SHM_CLIENT_OBJS)
	$(CXX) $(CXXFLAGS) $(XV_LIBS)  $(SHM_CLIENT_OBJS) -lpthread -o $@


MacVdrClient_obj = video_shm.o video-quartz_shm.o setup-softdevice_shm.o utils_shm.o VdrReplacements_shm.o PicBuffer_shm.o setup-softlog_shm.o MacVdrClient_shm.o
MacVdrClient: $(MacVdrClient_obj)
	$(CXX) $(LDFLAGS) $(MacVdrClient_obj)  -framework Carbon -framework AGL -framework OpenGL -lpthread -o $@
	cp $@ ./MacVdrClient.app/Contents/MacOS/MacVdrClient
