#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#
#  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; either version 2, or (at your option)
#  any later version.
#
#  This Program 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 OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. /etc/profile

LOCKDIR="/var/lock/"
LOCKFILE="vdr.disabled"

ADDON_DIR="$HOME/.xbmc/addons/service.multimedia.vdr-addon"
ADDON_HOME="$HOME/.xbmc/userdata/addon_data/service.multimedia.vdr-addon"
LOG_FILE="$ADDON_HOME/service.log"
ADDON_SETTINGS="$ADDON_HOME/settings.xml"

ADDON_CONFIG_DIR="$ADDON_HOME/config"
ADDON_PLUGIN_DIR="$ADDON_DIR/plugin"

if [ ! -f "$ADDON_SETTINGS" ]; then
  mkdir -p $ADDON_HOME
  cp $ADDON_DIR/settings-default.xml $ADDON_SETTINGS
fi

mkdir -p /var/config
cat "$ADDON_DIR/settings-default.xml" | awk -F\" '{print $2"=\""$4"\""}' | sed '/^=/d' > /var/config/vdr.conf.default
cat "$ADDON_SETTINGS" | awk -F\" '{print $2"=\""$4"\""}' | sed '/^=/d' > /var/config/vdr.conf
mkdir -p /var/lib/epgsources
cp $ADDON_DIR/config/epgsources/epgdata2xmltv/epgdata2xmltv.dist /var/lib/epgsources/epgdata2xmltv

. /var/config/vdr.conf.default
. /var/config/vdr.conf

VDR_ARG="-g /tmp --no-kbd --log=3 --port=0"
VDR_ARG="$VDR_ARG --config=$ADDON_CONFIG_DIR"
VDR_ARG="$VDR_ARG --lib=$ADDON_PLUGIN_DIR"
VDR_ARG="$VDR_ARG --video=\"$VDR_VIDEO_DIR\""
VDR_ARG="$VDR_ARG -P $PVR_PLUGIN"

if [ "$ENABLE_SOFTCAM" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P $SOFTCAM_PLUGIN"
fi
if [ "$ENABLE_IPTV" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P iptv"
fi
if [ "$ENABLE_STREAMDEV_SERVER" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P streamdev-server"
fi
if [ "$ENABLE_STREAMDEV_CLIENT" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P streamdev-client"
fi
if [ "$ENABLE_WIRBELSCAN" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P wirbelscan -P wirbelscancontrol"
fi
if [ "$ENABLE_LIVE" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P 'live -i $LIVE_IP -p $LIVE_PORT'"
fi
if [ "$ENABLE_ROTORNG" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P rotorng"
fi
if [ "$ENABLE_CONTROL" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P 'control -p $CONTROL_PORT'"
fi
if [ "$ENABLE_EPGSEARCH" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P epgsearch"
fi
if [ "$ENABLE_XMLTV2VDR" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P xmltv2vdr"
fi
if [ "$ENABLE_XINELIBOUTPUT" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P 'xineliboutput --local=none --remote=$XINELIBOUTPUT_PORT' -s $ADDON_HOME/vdr-shutdown.sh"
fi
if [ "$ENABLE_TEXT2SKIN" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P text2skin"
fi
# dynamite must be LAST loaded plugin
if [ "$ENABLE_DYNAMITE" == "true" ] ; then
  VDR_ARG="$VDR_ARG -P dynamite"
fi

(
  cd $ADDON_DIR/config
  mkdir -p $ADDON_CONFIG_DIR

  for dir in `find . -type d`; do
    mkdir -p $ADDON_CONFIG_DIR/$dir
  done

  for config in `find . -type f`; do
    if [ ! -f $ADDON_CONFIG_DIR/$config ]; then
      cp $config $ADDON_CONFIG_DIR/$config
    fi
  done

  if [ -f "$ADDON_CONFIG_DIR/setup.conf" ]; then
    sed -i -e '/^$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^OSDHeight.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^OSDSkin.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^OSDTheme.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^xineliboutput.OSD.Blending.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^xineliboutput.OSD.Size.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^xineliboutput.OSD.SoftOSD.*$/d' $ADDON_CONFIG_DIR/setup.conf
    sed -i -e '/^epgsearch.SVDRPPort.*$/d' $ADDON_CONFIG_DIR/setup.conf
  fi
  cat >>$ADDON_CONFIG_DIR/setup.conf << MYDATA
OSDHeight = 1080
OSDSkin = anthra_1280_OS
OSDTheme = yabluelight
xineliboutput.OSD.Blending = 1
xineliboutput.OSD.Size = 1280x720
xineliboutput.OSD.SoftOSD = 1
epgsearch.SVDRPPort = 2004
MYDATA

)

if [ ! "$(pidof vdr.bin)" ];then
  # start userspace DVB driver/addon
  for driver_dvb in $(find /storage/.xbmc/addons/driver.dvb.*/bin/userspace-driver.sh -type f 2>/dev/null); do
    driver_dvb_name=$(echo $driver_dvb | awk 'BEGIN {FS="/"} {printf("%s", $5)}')
    logger -t VDR "### Loading userspace DVB driver: $driver_dvb_name ###"
    # use ". " because of variable export
    . $driver_dvb
  done
  if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
    rm -rf "$LOCKDIR/$LOCKFILE"
  fi
  while [ true ] ; do
    if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
      break
    fi
    # (wait for) at least 1 adapter (xbmc allows to set 0)
    [ $NUM_ADAPTERS -lt 1 ] && NUM_ADAPTERS=1
    if [ "$WAIT_FOR_FEINIT" == "true" ] && [ ! -e /dev/dvb/adapter$((NUM_ADAPTERS-1))/frontend* ] ; then
      sleep 1
      continue
    fi
    LANG=en_US.UTF-8 LD_LIBRARY_PATH="$ADDON_DIR/lib:$LD_LIBRARY_PATH" eval vdr.bin $VDR_ARG &>$LOG_FILE
  done &
fi
