#!/bin/bash
clear

#
# command to get the file to your server
# wget --no-check-certificate https://raw.github.com/Chlorel/rtInstScript/master/rtInstScript-SVN
# chmod +x ./rtInstScript-SVN
# ./rtInstScript-SVN
#

# Checking if user is root.

if [ "$(id -u)" != "0" ]; then
	echo
	echo "This script must be run as root." 1>&2
	echo
	exit 1
fi

echo " ----------------------------------------------------------------------------
'THE BEER-WARE LICENSE' (Revision 42):
<patrick@kerwood.dk> wrote this script. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy me a beer in return.
- Patrick Kerwood @ LinuxBloggen.dk
----------------------------------------------------------------------------"
echo
echo
echo "Is the OS on this server Debian 7 ? If not, quit or continue at own risk."
echo
read -p "Press [Enter] to continue..." -n 1
echo

# Prompting for system user.

con=0
while [ $con -eq 0 ]; do

        echo -n "Please type a valid system user: "
        read -e user
        uid=$(cat /etc/passwd | grep "$user": | cut -d: -f3)

        if [ -z $(cat /etc/passwd | grep "$user":) ]; then
                echo
                echo "This user does not exist!"

        elif [ $uid -lt 999 ]; then
                echo
                echo "That user's UID is too low!"

        elif [ $user == nobody ]; then
                echo
                echo "You cant use 'nobody' as user!"
        else
                con=1
        fi
done

homedir=$(cat /etc/passwd | grep "$user": | cut -d: -f6)

# Prompting for default file location

echo -n "Please type your prefered download directory: "
read -e dldir

# Installing dependencies

apt-get update
apt-get upgrade
apt-get install openssl git subversion apache2 apache2-utils build-essential libsigc++-2.0-dev libcurl4-openssl-dev automake libtool libcppunit-dev libncurses5-dev libapache2-mod-scgi php5 php5-curl php5-cli libapache2-mod-php5 screen unrar-free unzip

# Enable ssl

a2enmod ssl
a2ensite default-ssl
service apache2 reload

# Installing xmlrpc-c

svn checkout http://svn.code.sf.net/p/xmlrpc-c/code/stable xmlrpc-c
cd xmlrpc-c
./configure --disable-cplusplus
make
make install
cd ..
rm -rv xmlrpc-c

mkdir rtorrent
cd rtorrent

# Installing libtorrent.

wget http://libtorrent.rakshasa.no/downloads/libtorrent-0.13.4.tar.gz
tar -zxvf libtorrent-0.13.4.tar.gz
cd libtorrent-0.13.4
./autogen.sh
./configure
make
make install
cd ..

# Installing rtorrent.

wget http://libtorrent.rakshasa.no/downloads/rtorrent-0.9.4.tar.gz
tar -zxvf rtorrent-0.9.4.tar.gz
cd rtorrent-0.9.4
./autogen.sh
./configure --with-xmlrpc-c
make
make install
cd ../..
rm -rv rtorrent

ldconfig

# Creating session directory

if [ ! -d "$homedir"/.rtorrent-session ]; then
	mkdir "$homedir"/.rtorrent-session
	chown "$user"."$user" "$homedir"/.rtorrent-session
else
	chown "$user"."$user" "$homedir"/.rtorrent-session
fi

# Creating downloads folder

if [ ! -d "$homedir"/"$dldir" ]; then
	mkdir "$homedir"/"$dldir"
	chown "$user"."$user" "$homedir"/"$dldir"
else
	chown "$user"."$user" "$homedir"/"$dldir"
fi

# Downloading rtorrent.rc file.

wget -O $homedir/.rtorrent.rc https://raw.github.com/Chlorel/rtInstScript/master/conf/rtorrent.rc

chown "$user"."$user" $homedir/.rtorrent.rc

sed -i "s@DOWNLOADDIRHERE@$dldir@g" $homedir/.rtorrent.rc 
sed -i "s@HOMEDIRHERE@$homedir@g" $homedir/.rtorrent.rc 


# Creating symlink for scgi.load

if [ ! -h /etc/apache2/mods-enabled/scgi.load ]; then

        ln -s /etc/apache2/mods-available/scgi.load /etc/apache2/mods-enabled/scgi.load
fi

# Installing rutorrent.

mkdir /var/www/ 
cd /var/www/

svn checkout http://rutorrent.googlecode.com/svn/trunk/rutorrent/

# Installing rutorrent plugins

cd rutorrent
rm -R plugins
svn checkout http://rutorrent.googlecode.com/svn/trunk/plugins/

# Installing chat plugins

cd /var/www/rutorrent/plugins/
svn co http://rutorrent-chat.googlecode.com/svn/trunk/ chat

# Installing logoff plugins

cd /var/www/rutorrent/plugins/
svn co http://rutorrent-logoff.googlecode.com/svn/trunk/ logoff

# Installing label suite plugins

cd /var/www/rutorrent/plugins/
wget http://rutorrent-tadd-labels.googlecode.com/files/lbll-suite_0.8.1.tar.gz
tar zxfv lbll-suite_0.8.1.tar.gz
rm lbll-suite_0.8.1.tar.gz

# Installing pausewebui plugins

cd /var/www/rutorrent/plugins/
svn co http://rutorrent-pausewebui.googlecode.com/svn/trunk/ pausewebui

# Installing rationcolor plugins

cd /var/www/rutorrent/plugins
git clone git://github.com/Gyran/rutorrent-ratiocolor.git
mv rutorrent-ratiocolor ratiocolor
sed -i -e "s/cell-background/font/g" /var/www/rutorrent/plugins/ratiocolor/init.js

#installing filemanager plugins

cd /var/www/rutorrent/plugins/
svn co -q http://svn.rutorrent.org/svn/filemanager/trunk/filemanager

# Installing mktorrent alternative to create .torrent

cd /root
mkdir tmp
cd tmp
wget http://downloads.sourceforge.net/mktorrent/mktorrent-1.0.tar.gz 
tar -xvzf mktorrent-1.0.tar.gz 
rm *.tar.gz 
cd mktorrent-1.0 
make USE_OPENSSL=1 USE_LONG_OPTIONS=1 USE_LARGE_FILES=1 
make install 

# mktorrent conf
rm /var/www/rutorrent/plugins/create/conf.php
wget -O /var/www/rutorrent/plugins/create/conf.php https://raw.github.com/Chlorel/rtInstScript/master/conf/plugins/create/conf.php
chmod -R 644 /var/www/rutorrent/plugins/create/conf.php

# Installing mediastream plugins
#
#cd /var/www/rutorrent/plugins/
#svn co -q http://svn.rutorrent.org/svn/filemanager/trunk/mediastream

#mkdir -p /"$homedir"/public_html/
#mkdir -p /"$homedir"/public_html/stream/

#ln -s /var/www/rutorrent/plugins/mediastream/view.php /"$homedir"/public_html/stream/view.php
#chown www-data: /"$homedir"/public_html/stream
#chown www-data: /"$homedir"/public_html/stream/view.php

#ip=$(ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d/ -f1)

#sed -i -e "s/mydomain.com/${ip}/g" /var/www/rutorrent/plugins/mediastream/conf.php

# Updating some dependencies is not done ...

apt-get -y install php5-geoip ffmpeg curl libzen0 libmediainfo0 mediainfo zip rar

# Changeing permissions for rutorrent and plugins.

chown -R www-data.www-data /var/www/rutorrent
chmod -R 775 /var/www/rutorrent

# Creating login - htaccess

echo 'AuthUserFile /var/www/rutorrent/.htpasswd
AuthName "Zone Administration"
AuthType Basic
Require Valid-User' > /var/www/rutorrent/.htaccess

# Creating Apache virtual host

if [ ! -f /etc/apache2/sites-available/rutorrent.script.conf ]; then
	echo "<VirtualHost *:80>
	ServerName *
	ServerAlias *

		DocumentRoot /var/www/

		CustomLog /var/log/apache2/rutorrent.log vhost_combined

		ErrorLog /var/log/apache2/rutorrent_error.log

		<Directory /var/www/rutorrent/>
				Options Indexes FollowSymLinks MultiViews
				AllowOverride AuthConfig
				Order allow,deny
				allow from all
		</Directory>
</VirtualHost>" > /etc/apache2/sites-available/rutorrent.script.conf

	a2ensite rutorrent.script.conf
fi

# Creating user for webinterface.

echo -n "Please type the username for the webinterface, system user not required: "
read -e htauser

while true; do

	htpasswd -c /var/www/rutorrent/.htpasswd "$htauser"
	if [ $? = 0 ]; then
		break
	fi
done

# Downloading and installing rtorrent init script.

wget -O /etc/init.d/rtorrent-init https://raw.github.com/Chlorel/rtInstScript/master/conf/rtorrent-init

chmod +x /etc/init.d/rtorrent-init

sed -i "s/USERNAMEHERE/$user/g" /etc/init.d/rtorrent-init

update-rc.d rtorrent-init defaults

service apache2 restart

echo -e "Installation is complete.."
echo

service rtorrent-init start

echo -e "Done.."
echo
echo -e "Your downloads is put in the '"$dldir"' folder, sessions data in '.rtorrent-session' and 
rtorrent's configuration file is '.rtorrent.rc', all in your home directory."
echo
echo -e "If you want to change configuration for rtorrent, such as download folder, port, etc., 
you will need to edit the '.rtorrent.rc' file. E.g. 'nano $homedir/.rtorrent.rc'"
tput sgr0
echo

if [ -z "$(ip addr | grep eth0)" ]; then
	echo "Visit rutorrent at http://IP.ADDRESS/rutorrent" 
else
	ip=$(ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d/ -f1)
	echo "Visit rutorrent at http://$ip/rutorrent"
fi
echo
echo -e "Patrick Kerwood - http://LinuxBloggen.dk"
echo -e "Chlorel - Chromosome"