#!/bin/bash

# Retrieve arguments
domain=$1
path=$2
data_dir=$3
passw=$4
current_dir=${PWD}
debian_version=$(lsb_release -c -s)

# Check domain/path availability
sudo yunohost app checkurl $domain$path -a rutorrent
if [[ ! $? -eq 0 ]]; then
  echo "Error: domain/path not available"
  exit 1
fi

# Install sources
if [ "$debian_version" = "bullseye" ]; then
  if [ -f /etc/apt/sources.list.d/rutorrent.list ]; then
    sudo rm /etc/apt/sources.list.d/rutorrent.list
  fi

  echo 'deb http://ftp2.fr.debian.org/debian/ bullseye main non-free' | sudo tee -a /etc/apt/sources.list.d/rutorrent.list
  echo 'deb-src http://ftp2.fr.debian.org/debian/ bullseye main non-free' | sudo tee -a /etc/apt/sources.list.d/rutorrent.list
  echo 'deb http://www.deb-multimedia.org bullseye main non-free' | sudo tee -a /etc/apt/sources.list.d/rutorrent.list
else
  echo 'deb http://ftp2.fr.debian.org/debian/ jessy main non-free' | sudo tee -a /etc/apt/sources.list.d/rutorrent.list
  echo 'deb-src http://ftp2.fr.debian.org/debian/ jessy main non-free' | sudo tee -a /etc/apt/sources.list.d/rutorrent.list
fi

# Install de dependencies
sudo apt-get update
apt install -y --allow-unauthenticated deb-multimedia-keyring
sudo apt-get install -y apt-transport-https gnupg2 apache2-utils automake htop build-essential curl ffmpeg gawk git libcppunit-dev libcurl4-openssl-dev libncurses5-dev libsigc++-2.0-dev libsox-fmt-all libsox-fmt-mp3 libssl-dev libtinyxml2-8 libltdl-dev libtool mediainfo mktorrent net-tools nginx php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-fpm php7.4-json php7.4-mbstring php7.4-opcache php7.4-readline php7.4-xml php7.4-bcmath php-geoip pkg-config psmisc python3-venv python3-pip python-is-python2 rar screen subversion unrar unzip sox vim zip zlib1g-dev

# Build xmlrpc
cd /tmp
git clone https://github.com/mirror/xmlrpc-c.git
cd xmlrpc-c/stable/
sudo ./configure
sudo make
sudo make install

# Build libtorrent
cd /tmp
sudo git clone https://github.com/rakshasa/libtorrent.git
cd libtorrent
sudo git checkout 0.13.8
sudo ./autogen.sh
sudo ./configure --disable-debug
sudo make
sudo make install

# Build rtorrent
cd /tmp
sudo git clone https://github.com/rakshasa/rtorrent.git
cd rtorrent
sudo git checkout 0.9.8
sudo ./autogen.sh
sudo ./configure --with-xmlrpc-c --with-ncurses --disable-debug
sudo make
sudo make install

sudo ldconfig

# Install rutorrent
sudo git clone https://github.com/Novik/ruTorrent.git /var/www/rutorrent

# Install rutorrent plugins

sudo pip3 install cloudscraper

cd /var/www/rutorrent/plugins/
sudo git clone https://github.com/Micdu70/rutorrent-ratiocolor.git ratiocolor


cd /var/www/rutorrent/plugins/
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/rutorrent-logoff/logoff-1.3.tar.gz
tar xzfv  logoff-1.3.tar.gz
rm logoff-1.3.tar.gz

cd /var/www/rutorrent/plugins/
git clone https://github.com/Micdu70/geoip2-rutorrent.git geoip2

cd /var/www/rutorrent/plugins/
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/rutorrent-pausewebui/pausewebui.1.2.zip
unzip pausewebui.1.2.zip
rm -R  __MACOSX pausewebui.1.2.zip

cd /tmp
git clone https://github.com/Micdu70/rutorrent-thirdparty-plugins.git
cd rutorrent-thirdparty-plugins
mv filemanager /var/www/rutorrent/plugins/filemanager

chown -R www-data:www-data /var/www/rutorrent


# Configure plugins
cd $current_dir


# Configure nginx
sudo sed -i "s@PATHTOCHANGE@$path@g" ../sources/nginx/rutorrent.conf
sudo cp ../sources/nginx/rutorrent.conf /etc/nginx/conf.d/$domain.d/rutorrent.conf

# Create data dir
sudo mkdir $data_dir
sudo mkdir $data_dir/watch
sudo mkdir $data_dir/downloads
sudo mkdir $data_dir/.session

# Create rtorrent user
sudo useradd -p $(openssl passwd -1 $passw) -m rtorrent

# Add rtorrent config
sudo sed -i "s@DATA_DIR@$data_dir@g" ../sources/rtorrent/rtorrent.rc
sudo cp ../sources/rtorrent/rtorrent.rc /home/rtorrent/.rtorrent.rc
sudo chown -R rtorrent:www-data /home/rtorrent/.rtorrent.rc

sudo chown -R rtorrent:www-data $data_dir
sudo chmod 755 $data_dir

# Configure rutorrent
sudo sed -i "s@DATA_DIR@$data_dir@g" ../sources/rutorrent/config.php
sudo cp ../sources/rutorrent/config.php /var/www/rutorrent/conf/config.php
sudo chown -R www-data:www-data /var/www/rutorrent

# Check port availability
sudo yunohost app checkport 45069
if [[ ! $? -eq 0 ]]; then
  exit 1
fi

# Open port in firewall
sudo yunohost firewall allow TCP 45069 > /dev/null 2>&1

# Launch rtorrent on boot
sudo cp ../sources/init/rtorrent /etc/init.d/rtorrent
sudo chmod +x /etc/init.d/rtorrent
sudo update-rc.d rtorrent defaults

# Start rtorrent
sudo service rtorrent start
sudo yunohost app ssowatconf
sudo service nginx reload

# Save config
sudo yunohost app setting rutorrent data_dir -v $data_dir
sudo yunohost app setting rutorrent path -v $path

# Register the service on yunohost
sudo yunohost service add rtorrent
