#!/bin/bash

######################################################################
#
#  Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
#  --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin

osname=$(lsb_release -si)
relno=$(lsb_release -sr | cut -d. -f1)
fullrelno=$(lsb_release -sr)

# Fallback if lsb_release -si returns nothing
if [ "$osname" = "" ]; then
  osname=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')
  osname=${osname^}
fi

# Fallback if lsb_release -sr returns nothing
if [ "$relno" = "" ]; then
  relno=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"' | cut -d. -f1)
fi

if [ "$fullrelno" = "" ]; then
  fullrelno=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"')
fi


sourcedir='https://rakshasa.github.io/rtorrent/'

xmlrpc_url='https://svn.code.sf.net/p/xmlrpc-c/code/stable/'
xmlrpcloc='svn://svn.code.sf.net/p/xmlrpc-c/code/stable/'

xmlrpc_url_alt='https://github.com/mirror/xmlrpc-c'
xmlrpcloc_alt='https://github.com/mirror/xmlrpc-c/trunk/stable'

rtdevrel=1

libvers='0.13.8'
rtvers='0.9.8'

duoversion=''
passed_rtvers=$1
[ -z $logfile ] && logfile="/dev/null"


# checks if an application is installed
installed() {
hash $1 2>/dev/null
}

#function to check if a web site is reachable
check_url() {
  if [[ `wget -S -T 3 --spider $1  2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then return 0; else return 1; fi
}


ask_user(){
local answer
while true
  do
    read answer
    case $answer in [Yy]* ) return 0 ;;
                    [Nn]* ) return 1 ;;
                        * ) echo "Enter y or n";;
    esac
  done
}

if [ "$(id -u)" != "0" ]; then
  echo "Must be run as root, or with sudo"
  exit 1
fi

# Check the libtorrent/rtorrent and xmlrpc sites are available
xmlrpc_repo=0
rt_repo=0
echo
echo "Checking File Repositories"

echo -n "xmlrpc-c: "; check_url $xmlrpc_url && echo "OK" ||  xmlrpc_repo=1

if [[ $xmlrpc_repo = 1 ]]; then
  xmlrpc_url=$xmlrpc_url_alt
  xmlrpcloc=$xmlrpcloc_alt
  check_url $xmlrpc_url && { echo "OK";  xmlrpc_repo=0; } || { echo "FAIL"; xmlrpc_repo=1; }
fi

echo -n "rtorrent: "; check_url $sourcedir && echo "OK" || { echo "FAIL"; rt_repo=1; }

if [ $rt_repo = 1 ]; then
  echo "The libtorrent/rtorrent file repositoy is offline"
  echo "Try again later, you can check if the following address is online in your browser:"
  echo $sourcedir
  exit 1
fi

if [ $xmlrpc_repo = 1 ]; then
  if [ -d /usr/include/xmlrpc-c ]; then
    echo "xmlrpc repository is off line, but looks like it has already beed installed."
    echo "We will try and install rtorrent"
  else
    echo "The xmlrpc-c file repositoy is offline"
    echo "Try again later, you can check if the following address is online in your browser:"
    echo $xmlrpcloc
    exit 1
  fi
fi

if [ -z $passed_rtvers ]; then
  if ( installed rtorrent ); then
    rt_current=$(rtorrent -h | grep -om 1 "[0-9]\{1,2\}\.[0-9]\{1,2\}\.[0-9]\{1,2\}")
    echo "rtorrent $rt_current detected"
    exit 1
  else
    echo "rtorrent NOT installed"
    echo "This script is for upgrading/downgrading only, not for initial install"
    exit 1
  fi
fi


if [ -z "$home" ]; then
  home=$HOME
fi

echo
echo "Installing libtorrent-$libvers/rtorrent-$rtvers"

cd $home

if [ -d source ]; then
  cd source
else
  mkdir source && cd source
fi
rm -rf xmlrpc libtorrent* rtorrent*

echo "Fetching source files"

#if [ $rtdevrel = 0 ]; then
#  git clone -q -b feature-bind --single-branch https://github.com/rakshasa/libtorrent.git libtorrent-$libvers
#  git clone -q -b feature-bind --single-branch https://github.com/rakshasa/rtorrent.git rtorrent-$rtvers
#else
  curl -LOJ# https://github.com/rakshasa/rtorrent-archive/raw/master/libtorrent-$libvers.tar.gz
  tar -xzf libtorrent-$libvers.tar.gz
  curl -LOJ# https://github.com/rakshasa/rtorrent-archive/raw/master/rtorrent-$rtvers.tar.gz
  tar -xzf rtorrent-$rtvers.tar.gz
#fi

if [ $xmlrpc_repo = 0 ]; then
  svn co $xmlrpcloc xmlrpc  >> $logfile 2>&1
  if [ ! -e xmlrpc ]; then 
    git clone $xmlrpc_url_alt xmlrpc >> $logfile 2>&1
    cd xmlrpc/stable
  else cd xmlrpc; fi

  echo "Installing xmlrpc" | tee -a $logfile
  ./configure --prefix=/usr --disable-cplusplus >> $logfile 2>&1
  make -j$(nproc) >> $logfile 2>&1
  make -s install >> $logfile 2>&1
  cd $home/source
fi

cd libtorrent-$libvers
echo "Installing libtorrent" | tee -a $logfile
./autogen.sh >> $logfile 2>&1
if [ $osname = "Raspbian" ]; then
  ./configure --prefix=/usr --disable-instrumentation >> $logfile 2>&1
else
  ./configure --prefix=/usr >> $logfile 2>&1
fi
make -j$(nproc) >> $logfile 2>&1
make -s install >> $logfile 2>&1

cd ../rtorrent-$rtvers
echo "Installing rtorrent" | tee -a $logfile
./autogen.sh >> $logfile 2>&1
./configure --prefix=/usr --with-xmlrpc-c --enable-ipv6 >> $logfile 2>&1
make -j$(nproc) >> $logfile 2>&1
make -s install >> $logfile 2>&1
ldconfig >> $logfile 2>&1


if [ -d /var/www/rutorrent/conf/users ]; then
  cd /var/www/rutorrent/conf/users
  user_list=*
  for user in $user_list; do
    if [ ! "$user" = '*' ]; then
      echo "Restarting rtorrent for $user"
      su $user -c 'rt restart >> /dev/null 2>&1'
    fi
  done
  echo
fi
