#!/bin/sh

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#      Copyright (C) 2011-2012 Gregor Fuis (gujs@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="oscam.disabled"

# Check if process is already running
# use "$(pidof)" instead of $(pidof). otherwise 'if' statement will fail
# because oscam spawns at least one 'extra' process
if [ ! "$(pidof oscam)" ]; then

  ADDON_DIR="$HOME/.xbmc/addons/service.softcam.oscam"
  ADDON_HOME="$HOME/.xbmc/userdata/addon_data/service.softcam.oscam"
  ADDON_LOG_DIR="$ADDON_HOME/log"

  ADDON_SETTINGS="$ADDON_HOME/settings.xml"
  ADDON_CONF_DIR="$ADDON_HOME/config"
  ADDON_CONF="$ADDON_CONF_DIR/oscam.conf"
  ADDON_CONF_AC="$ADDON_CONF_DIR/oscam.ac"
  ADDON_CONF_CERT="$ADDON_CONF_DIR/oscam.cert"
  ADDON_CONF_DVBAPI="$ADDON_CONF_DIR/oscam.dvbapi"
  ADDON_CONF_GUESS="$ADDON_CONF_DIR/oscam.guess"
  ADDON_CONF_IRD="$ADDON_CONF_DIR/oscam.ird"
  ADDON_CONF_PROVID="$ADDON_CONF_DIR/oscam.provid"
  ADDON_CONF_SERVER="$ADDON_CONF_DIR/oscam.server"
  ADDON_CONF_SERVICES="$ADDON_CONF_DIR/oscam.services"
  ADDON_CONF_SRVID="$ADDON_CONF_DIR/oscam.srvid"
  ADDON_CONF_TIERS="$ADDON_CONF_DIR/oscam.tiers"
  ADDON_CONF_USER="$ADDON_CONF_DIR/oscam.user"

  OSCAM_ARG="-c $ADDON_CONF_DIR"

  mkdir -p $ADDON_HOME
  mkdir -p $ADDON_CONF_DIR
  mkdir -p $ADDON_LOG_DIR

  if [ ! -f "$ADDON_CONF" ]; then
    cp $ADDON_DIR/oscam-default.conf $ADDON_CONF
  fi

  touch $ADDON_CONF_AC
  touch $ADDON_CONF_CERT
  touch $ADDON_CONF_DVBAPI
  touch $ADDON_CONF_GUESS
  touch $ADDON_CONF_IRD
  touch $ADDON_CONF_PROVID
  touch $ADDON_CONF_SERVER
  touch $ADDON_CONF_SERVICES
  touch $ADDON_CONF_SRVID
  touch $ADDON_CONF_TIERS
  touch $ADDON_CONF_USER

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

# Restart process if it terminates.
  if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
    rm -rf "$LOCKDIR/$LOCKFILE"
  fi
  while [ true ] ; do
    if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
      break
    fi
    # 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 OSCAM "### Loading userspace DVB driver: $driver_dvb_name ###"
      # use ". " because of variable export
      . $driver_dvb
    done
    LD_LIBRARY_PATH="$ADDON_DIR/lib:$LD_LIBRARY_PATH" oscam $OSCAM_ARG > /dev/null 2>&1
    sleep 1
  done &
fi
