#
# Softcam plugin to VDR
#
# This code is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Or, point your browser to http://www.gnu.org/copyleft/gpl.html

# 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 = sc

### 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:

CXX      ?= g++
CXXFLAGS ?= -O2 -fPIC -Wall -Woverloaded-virtual

### The directory environment:

DVBDIR = ../../../../DVB
VDRDIR = ../../..
LIBDIR = ../../lib
TMPDIR = /tmp

### 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')
VDRVERSNUM = $(shell grep 'define VDRVERSNUM ' $(VDRDIR)/config.h | awk '{ print $$3 }')

### The name of the distribution archive:

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

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

OBJS = $(PLUGIN).o data.o filter.o system.o i18n.o misc.o cam.o \
       smartcard.o network.o crypto.o system-common.o parse.o

### Includes and Defines

INCLUDES += -I$(VDRDIR)/include -I$(DVBDIR)/include
DEFINES  += -DPLUGIN_NAME_I18N='"$(PLUGIN)"' -DVDRVERSNUM=$(VDRVERSNUM) -D_GNU_SOURCE

#
# include system specific make rules
#
-include system-*.mk

#
# crypto stuff
#
ifdef CRYPTOTPS
DEFINES += -DCRYPTO_TPS
CRYPTOAES = 1
endif

ifdef CRYPTODES
DEFINES += -DCRYPTO_DES
endif

ifdef CRYPTOAES
DEFINES += -DCRYPTO_AES
# test if there is an openssl executable and if it
# support AES encryption
TEST := $(shell which openssl)
ifneq ($(strip $(TEST)),)
  TEST := $(shell echo 'test' | openssl aes-128-ecb -a -k test 2>&1 | grep 'invalid command')
  TEST2 = $(findstring invalid command,$(TEST))
  ifneq ($(TEST2),invalid command)
    DEFINES += -DOPENSSL_HAS_AES
    OPENSSL = 1
  endif
endif
endif

ifdef CRYPTOIDEA
DEFINES += -DCRYPTO_IDEA
endif

#
# generic stuff
#
ifdef SMARTCARD
DEFINES += -DSMARTCARD
ifdef DEFAULT_PORT
DEFINES += -DDEFAULT_PORT='$(DEFAULT_PORT)'
endif
endif
ifdef OPENSSL
SHAREDLIBS += -lcrypto
DEFINES += -DOPENSSL
endif
ifdef NETWORK
DEFINES += -DNETWORK
endif

#
# debug
#
ifdef DBG
CXXFLAGS += -g
export DBG
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)

### Targets:

all: libvdr-$(PLUGIN).so

libvdr-$(PLUGIN).so: $(OBJS)
	$(CXX) $(CXXFLAGS) -shared $(OBJS) $(SHAREDLIBS) -o $@
	@cp $@ $(LIBDIR)/$@.$(VDRVERSION)

system-i18n.c: $(I18N)
	echo >$@ "/* generated file, do not edit */"
	if test "A$^" != "A"; then for name in $^; do echo >>$@ "#include \"$$name\""; done; fi

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

clean:
	@$(MAKE) -C testing clean
	@-rm -f $(OBJS) $(DEPFILE) system-i18n.c *.so *.tar.gz core* *~
