#!/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

ADDON_DIR="$HOME/.xbmc/addons/service.downloadmanager.transmission"
ADDON_HOME="$HOME/.xbmc/userdata/addon_data/service.downloadmanager.transmission"
LOG_FILE="$ADDON_HOME/service.log"

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

export TRANSMISSION_WEB_HOME="$ADDON_DIR/web"

mkdir -p $ADDON_HOME
ADDON_SETTINGS="$ADDON_HOME/settings.xml"

if [ ! -f "$ADDON_SETTINGS" ]; then
  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/transmission.conf.default
cat "$ADDON_SETTINGS" | awk -F\" '{print $2"=\""$4"\""}' | sed '/^=/d' > /var/config/transmission.conf
. /var/config/transmission.conf.default
. /var/config/transmission.conf

mkdir -p "$TRANSMISSION_DL_DIR"
mkdir -p "$TRANSMISSION_DL_DIR/incoming"
mkdir -p "$TRANSMISSION_DL_DIR/watch"

if [ -z "$TRANSMISSION_IP" ]; then
  TRANSMISSION_IP="*.*.*.*"
fi

TRANSMISSION_ARG="$TRANSMISSION_ARG -w \"$TRANSMISSION_DL_DIR\""
TRANSMISSION_ARG="$TRANSMISSION_ARG --incomplete-dir \"$TRANSMISSION_DL_DIR/incoming\""
TRANSMISSION_ARG="$TRANSMISSION_ARG --watch-dir \"$TRANSMISSION_DL_DIR/watch\""
TRANSMISSION_ARG="$TRANSMISSION_ARG -e /var/log/transmission.log"
TRANSMISSION_ARG="$TRANSMISSION_ARG -g /storage/.cache/transmission"

if [ ! "$TRANSMISSION_LIMIT_GLOBAL" = "" ]; then
  TRANSMISSION_ARG="$TRANSMISSION_ARG -L $TRANSMISSION_LIMIT_GLOBAL"
fi
if [ ! "$TRANSMISSION_LIMIT_TORRENT" = "" ]; then
  TRANSMISSION_ARG="$TRANSMISSION_ARG -l $TRANSMISSION_LIMIT_TORRENT"
fi

TRANSMISSION_ARG="$TRANSMISSION_ARG -a '$TRANSMISSION_IP'"

if [ "$TRANSMISSION_AUTH" = "true" ]; then
  TRANSMISSION_ARG="$TRANSMISSION_ARG -t"
  TRANSMISSION_ARG="$TRANSMISSION_ARG -u $TRANSMISSION_USER"
  TRANSMISSION_ARG="$TRANSMISSION_ARG -v $TRANSMISSION_PWD"
else
  TRANSMISSION_ARG="$TRANSMISSION_ARG -T"
fi

if [ ! "$(pidof transmission-daemon)" ];then
  if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
    rm -rf "$LOCKDIR/$LOCKFILE"
  fi
  while [ true ] ; do
    if [ -f "$LOCKDIR/$LOCKFILE" ] ; then
      break
    fi
    EVENT_NOEPOLL=1 LD_LIBRARY_PATH="$ADDON_DIR/lib:$LD_LIBRARY_PATH" eval transmission-daemon -f $TRANSMISSION_ARG &>$LOG_FILE
    sleep 1
  done &
fi
