# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../vdr/vdr.conf
# Copyright (C) 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

PLUGIN_LIST=
SKIN_LIST=
[ $SDECFG_PKG_VDR_REMOTE -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST remote"
[ $SDECFG_PKG_VDR_FEMON -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST femon"
[ $SDECFG_PKG_VDR_MP3 -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST mp3"
[ $SDECFG_PKG_VDR_OSDTELETEXT -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST osdteletext"
[ $SDECFG_PKG_VDR_GRAPHLCD -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST graphlcd"
[ $SDECFG_PKG_VDR_IMAGE -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST image"
[ $SDECFG_PKG_VDR_DVD -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST dvd"
[ $SDECFG_PKG_VDR_VCD -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST vcd"
[ $SDECFG_PKG_VDR_PCD -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST pcd"
[ $SDECFG_PKG_VDR_AUDIOCD -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST audiocd"
[ $SDECFG_PKG_VDR_SOFTDEVICE -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST softdevice"
[ $SDECFG_PKG_VDR_XINE -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST xine"
[ $SDECFG_PKG_VDR_DXR3 -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST dxr3"
[ $SDECFG_PKG_VDR_BURN -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST burn"
[ $SDECFG_PKG_VDR_CDDA -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST cdda"
[ $SDECFG_PKG_VDR_EPGSEARCH -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST epgsearch"
[ $SDECFG_PKG_VDR_BITSTREAMOUT -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST bitstreamout"
[ $SDECFG_PKG_VDR_DIGICAM -gt 0 ] && PLUGIN_LIST="$PLUGIN_LIST digicam"
[ $SDECFG_PKG_VDR_TEXT2SKIN -gt 0 ] && {
	PLUGIN_LIST="$PLUGIN_LIST text2skin"
	SKIN_LIST="DeepBlue lightblue16 vdrskin-enigma"
}

# Enable compatibility mode for weird streaming clients
[ $SDECFG_PKG_VDR_VFAT -gt 0 ] && var_append GCC_WRAPPER_APPEND ' ' '-DVFAT=1'


extract_patches() {
	echo "Extracting patches ..."
	for fpatch in `match_source_file -p vdr-$ver.*.diff` ; do
		echo "$fpatch"
		cat $fpatch | patch -p 1
	done
}

extract_plugins() {
	echo "Extracting plugins ..."
	cd PLUGINS/src
	for vplugin in $PLUGIN_LIST ; do
		for fplugin in `match_source_file -p vdr-$vplugin` ; do

			# Extract plugin source
			echo "$vplugin:  $fplugin"
			tar $taropt $fplugin
			find . -type d -name "$vplugin-*" -exec ln -sf {} $vplugin \;

			# Apply plugin patches
			for fpatch in `ls $confdir/$vplugin-*.diff` ; do
				echo "$fpatch"
				cat $fpatch | patch -p 0
			done

			# Fix plugins: VDRVERSION -> APIVERSION
			sed -i -e 's/VDRVERSION/APIVERSION/g' $vplugin/Makefile

			# Copy plugin documentation to vdr root directory
			for docu in README README.DE INSTALL MANUAL FAQ ; do
				[ -e $vplugin/$docu ] && cp -av $vplugin/$docu ../../$docu.$vplugin
			done
		done
	done
	cd ../..

	tar $taropt `match_source_file -p mplayer.sh`
}

gen_runvdr ()
{
	cat << EOC > $root/etc/vdr/runvdr.sh
#!/bin/sh

VDRPRG="$bindir/vdr"
VDRUSR=
KILL="$bindir/killall -q -TERM \$VDRPRG"
VDIR="/video"

PLUGINS=
[ $SDECFG_PKG_VDR_REMOTE -ne 0 ] && PLUGINS="\$PLUGINS@-Premote"
[ $SDECFG_PKG_VDR_FEMON -ne 0 ] && PLUGINS="\$PLUGINS@-Pfemon"
[ $SDECFG_PKG_VDR_GRAPHLCD -ne 0 ] && PLUGINS="\$PLUGINS@-P@graphlcd -c ks108"
[ $SDECFG_PKG_VDR_MP3 -ne 0 ] && PLUGINS="\$PLUGINS@-P@mp3 -m /etc/vdr/mount.sh@-P@mplayer -m /etc/vdr/mount.sh -M /etc/vdr/mplayer.sh@"
[ $SDECFG_PKG_VDR_DVD -ne 0 ] && PLUGINS="\$PLUGINS@-Pdvd"
[ $SDECFG_PKG_VDR_VCD -ne 0 ] && PLUGINS="\$PLUGINS@-Pvcd"
[ $SDECFG_PKG_VDR_PCD -ne 0 ] && PLUGINS="\$PLUGINS@-Ppcd"
[ $SDECFG_PKG_VDR_IMAGE -ne 0 ] && PLUGINS="\$PLUGINS@-P@image -m /etc/vdr/mount.sh -C /etc/vdr/plugins/image/imageplugin.sh@"
[ $SDECFG_PKG_VDR_OSDTELETEXT -ne 0 ] && PLUGINS="\$PLUGINS@-Posdteletext"
[ $SDECFG_PKG_VDR_TEXT2SKIN -ne 0 ] && PLUGINS="\$PLUGINS@-Ptext2skin"
[ $SDECFG_PKG_VDR_SOFTDEVICE -ne 0 ] && PLUGINS="\$PLUGINS@-Psoftdevice -L /usr/lib/vdr -vo xv:@"
[ $SDECFG_PKG_VDR_XINE -ne 0 ] && PLUGINS="\$PLUGINS@-Pxine -r"
[ $SDECFG_PKG_VDR_DXR3 -ne 0 ] && PLUGINS="\$PLUGINS@-Pdxr3"
[ $SDECFG_PKG_VDR_BURN -ne 0 ] && PLUGINS="\$PLUGINS@-Pburn"
[ $SDECFG_PKG_VDR_CDDA -ne 0 ] && PLUGINS="\$PLUGINS@-Pcdda"
[ $SDECFG_PKG_VDR_EPGSEARCH -ne 0 ] && PLUGINS="\$PLUGINS@-Pepgsearch"
[ $SDECFG_PKG_VDR_AUDIOCD -ne 0 ] && PLUGINS="\$PLUGINS@-Paudiocd"
[ $SDECFG_PKG_VDR_BITSTREAMOUT -ne 0 ] && PLUGINS="\$PLUGINS@-Pbitstreamout"
[ $SDECFG_PKG_VDR_DIGICAM -ne 0 ] && PLUGINS="\$PLUGINS@-Pdigicam"

VDRCMD="\$VDRPRG@-c@/etc/vdr@-s@/usr/bin/vdrshutdown@-L@/usr/lib/vdr@-w@300@-u@root@-v@\$VDIR@\$PLUGINS@\$*"

export HOME=/var/lib/vdr
#export DVDCSS_METHOD=key
#export LD_ASSUME_KERNEL=2.4.1

OIFS="\$IFS"
IFS=@

echo "VDRCMD=\$VDRCMD"
echo "\$\$" > /var/run/runvdr

while test /bin/true ; do

	date
	echo "Starting VDR"

	(cd \$VDIR; \$VDRCMD \$VDRUSR)
	if test \$? -eq 0; then exit; fi

	date
	echo "Stopping VDR"

	\$KILL
	sleep 3
done

EOC
}

vdr_install() {
	echo "Installing vdr, plugins and scripts ..."

	mkdir -p $root/usr/lib/vdr
	mkdir -p $root/usr/lib/vdr/plugins

	mkdir -p $root/etc/vdr
	ln -sf $root/usr/lib/vdr/plugins $root/etc/vdr

	cp -a vdr $root/usr/bin
	cp -a ./PLUGINS/lib/* $root/usr/lib/vdr

	# Copy vdr default .conf
	cp -a *.conf $root/etc/vdr

#	cp -a $confdir/remote.conf $root/etc/vdr
	cp -a $confdir/{commands,reccmds}.conf $root/etc/vdr
	cp -a $confdir/mount.sh $root/etc/vdr

	gen_runvdr

	if [ "$SDECFG_PKG_VDR_MP3" = "1" ] ; then
		cp -a mplayer.sh* $root/etc/vdr
		cp -a $confdir/mp3sources.conf $root/etc/vdr/plugins
		cp -a $confdir/mplayersources.conf $root/etc/vdr/plugins
	fi

	if [ "$SDECFG_PKG_VDR_IMAGE" = "1" ] ; then
		cp -a $confdir/imagecmds.conf $root/etc/vdr
		cp -a $confdir/imagesources.conf $root/etc/vdr/plugins
		mkdir -p $root/etc/vdr/plugins/image
		cp -a ./PLUGINS/src/image/scripts/* $root/etc/vdr/plugins/image
	fi

	if [ "$SDECFG_PKG_VDR_DIGICAM" = "1" ] ; then
		cp -pv ./PLUGINS/src/digicam/examples/digicam{sources,destinations}.conf $root/etc/vdr/plugins
	fi

	if [ "$SDECFG_PKG_VDR_DVD" = "1" ] ; then
		touch $root/etc/vdr/plugins/VCD
		touch $root/etc/vdr/plugins/DVD
	fi

	if [ "$SDECFG_PKG_VDR_GRAPHLCD" = "1" ] ; then
		mkdir -p $root/etc/vdr/plugins/graphlcd
		cp -a ./PLUGINS/src/graphlcd/graphlcd $root/etc/vdr/plugins
		for tgt in convpic crtfont showpic drawtest ; do
			echo "graphlcd: $tgt"
			cp -pv ./PLUGINS/src/graphlcd/tools/$tgt/$tgt $root/$bindir
		done
	fi

	if [ "$SDECFG_PKG_VDR_TEXT2SKIN" = "1" ] ; then
		mkdir -p $root/etc/vdr/plugins/text2skin
		pushd $root/etc/vdr/plugins/text2skin
		for skin in $SKIN_LIST ; do
			echo "Extracting skin: $skin"
			tar $taropt `match_source_file -p $skin`
		done
		popd
	fi

	if [ "$SDECFG_PKG_VDR_XINE" = "1" ] ; then
		mkdir -p $root/etc/vdr/plugins/xine
		cp -a ./PLUGINS/src/xine/data/*.pes $root/etc/vdr/plugins/xine
	fi

	chmod u+x $root/etc/vdr/*.sh
}

vdr_extract () {
	extract_patches
	extract_plugins
}

vdr_plugins () {
	echo "Compiling vdr plugins"
	eval $MAKE plugins
}

makeinstopt=
var_append makeopt ' ' 'all'

hook_add preconf 5 vdr_extract
hook_add postmake 5 vdr_plugins
hook_add postmake 8 vdr_install