#!/bin/bash
#
# This is an automated script created to ease the installation of rtorrent 
# with rutorrent web user interface in a headless Ubuntu 10.04 server.
#
#########################################
#  USE THIS SCRIPT AT YOUR OWN RISK!!!  #
#########################################
#
# The script will not work for Ubuntu 10.10 as the repository used contains packages 
# only for Ubuntu 10.04.
#
# It needs one valid user with a user id of 1000 or more as an argument. The first 
# user created in the installation will do fine.
# This user will be used to run rtorrent backend.
# Under the user's home directory, some more rtorrent directories will be created.
#
# Example usage: ./rtorrent_server.sh your_user
#
# The script will do the following changes in your computer:
#	1) Download rtorrent 3.2
#	2) Download rtorrent plugins 3.2
#	3) Download a startup script for rtorrent
#	4) Download a working .rtorrent.rc
#	5) Check the md5sum of the downloaded files.
#	6) Add a PPA repository for rtorrent compiled against the latest xmlrpc libraries.
#	7) Download any extra dependencies using apt-get such as apache, screen and some more.
#	8) Create all necessary directories
#	9) Set all necessary permissions
#	10) Ask for a password (using htpasswd) for the given username to be able to access the web page of rutorrent
#	11) Install the startup script for automatic startup of rtorrent on boot.
#
# Only the installation script has been created by me (cyberang3l).
#
# rtorrent startup script has been found while I was searching around (lostnihilist is the corresponding author)
#
# Some more information for the create script has been found from the following link:
# http://chrispenner.info/blog/2010/07/20/installing-rtorrent-rutorrent-on-ubuntu-server-10-04-lts/

if [ $# -ne 1 ]
then
	echo -e "Usage: $(basename $0) <username>"
	exit 1
else
	if [ "$(whoami)" != "root" ]
	then
	        echo "You must be root to run this script."
        	exit 1
	fi

	ID=$(id $1 | cut -d"=" -f2 | cut -d"(" -f1)
	if [ "$ID" != "" ]
	then
		if [ $ID -lt 1000 ]
		then
			echo "Provide an existing username with an ID of 1000 or greater"
			exit 1
		fi
	else
		echo "Provide an existing username with an ID of 1000 or greater"
		exit 1
	fi
fi

WGET=$(whereis wget | cut -d" " -f2)
MD5SUM=$(whereis md5sum | cut -d" " -f2)
USER=$1
USER_HOME=$(cat /etc/passwd | grep $USER | cut -d":" -f6)

APACHE_USER=www-data
FIRST_IF=$(ifconfig | head -1 | cut -d" " -f1)
IP_ADDRESS=$(ifconfig $FIRST_IF | grep "inet addr" | cut -d":" -f2 | cut -d" " -f 1)

MD5SUM_RUTORRENT_ORIG="516c1c5c7360f540812eb38bacaa60b2"
MD5SUM_PLUGINS_ORIG="660ec019878b98c7c77b0aa6a232480a"
MD5SUM_INITSCRIPT_ORIG="b6409856da78939b19edff515a63041b"
MD5SUM_RTORRENTRC_ORIG="f5cf1e2f9293b2c86c87154f5d9730e1"

$WGET http://dl.dropbox.com/u/3397346/rutorrent-3.2.tar.gz -O /tmp/rutorrent.tar.gz
$WGET http://dl.dropbox.com/u/3397346/plugins-3.2.tar.gz -O /tmp/plugins.tar.gz
$WGET http://dl.dropbox.com/u/3397346/rtorrentInit.sh -O /tmp/rtorrentInit.sh
$WGET http://dl.dropbox.com/u/3397346/rtorrent.rc -O /tmp/rtorrent.rc

cd /tmp
MD5SUM_RUTORRENT=$($MD5SUM rutorrent.tar.gz | cut -d" " -f1)
MD5SUM_PLUGINS=$($MD5SUM plugins.tar.gz | cut -d" " -f1)
MD5SUM_INITSCRIPT=$($MD5SUM rtorrentInit.sh | cut -d" " -f1)
MD5SUM_RTORRENTRC=$($MD5SUM rtorrent.rc | cut -d" " -f1)

if [ "$MD5SUM_RUTORRENT" != "$MD5SUM_RUTORRENT_ORIG" ]
then
	echo -e "Incorrect rutorrent md5sum... Aborting..\n"
	exit 1
else
	echo -e "rutorrent md5 checksum OK!\n"
fi

if [ "$MD5SUM_PLUGINS" != "$MD5SUM_PLUGINS_ORIG" ]
then
        echo -e "Incorrect plugins md5sum... Aborting..\n"
        exit 1
else
        echo -e "plugins md5 checksum OK!\n"
fi

if [ "$MD5SUM_INITSCRIPT" != "$MD5SUM_INITSCRIPT_ORIG" ]
then
        echo -e "Incorrect init script md5sum... Aborting..\n"
        exit 1
else
        echo -e "init script md5 checksum OK!\n"
fi

if [ "$MD5SUM_RTORRENTRC" != "$MD5SUM_RTORRENTRC_ORIG" ]
then    
        echo -e "Incorrect rtorrent.rc md5sum... Aborting..\n"
        exit 1
else    
        echo -e "rtorrent.rc md5 checksum OK!\n"
fi

#Add PPA repository for rtorrent compiled against the latest xmlrpc libraries.
apt-get update && apt-get -y install python-software-properties && add-apt-repository ppa:patricksissons/rtorrent

#Install dependencies
apt-get update && apt-get -y install php5-cli unrar rar curl apache2 libapache2-mod-scgi libapache2-mod-php5 unzip screen rtorrent

#Enable apache scgi module
a2enmod scgi

sed -i "s/user=\"myUser\"/user=\"$USER\"/" rtorrentInit.sh
USER_HOME_BACKSLASHED=$(echo $USER_HOME | sed "s/\\//\\\\\\//g")
sed -i "s/\\/home\\/myUser/$USER_HOME_BACKSLASHED/g" rtorrent.rc

#Creating default rtorrent config file
mv rtorrent.rc $USER_HOME/.rtorrent.rc
chown $USER.$USER $USER_HOME/.rtorrent.rc

#Creating rtorrent directories
mkdir -p $USER_HOME/rtorrent/downloads
mkdir -p $USER_HOME/rtorrent/session
mkdir -p $USER_HOME/rtorrent/watch

chmod -R 775 $USER_HOME/rtorrent
chown -R $USER.$APACHE_USER $USER_HOME/rtorrent

#Create Startup scripts for rtorrent
chmod +x rtorrentInit.sh
mv rtorrentInit.sh /etc/init.d/
update-rc.d rtorrentInit.sh defaults

#Extract rutorrent and plugins
tar xzvf rutorrent.tar.gz -C /var/www/
tar xzvf plugins.tar.gz -C /var/www/rutorrent/
chown -R $APACHE_USER.$APACHE_USER /var/www/rutorrent/

#Create Virtual Host for Apache
echo "SCGIMount /RPC2 127.0.0.1:5000
<directory /var/www/rutorrent/>

AuthName \"Log In\"

AuthType Basic

AuthUserFile /var/www/rutorrent/.htpasswd

AuthGroupFile /dev/null

require user $USER

</directory>" > /etc/apache2/sites-available/rutorrent

#Enable the newly created host
a2ensite rutorrent

echo -e "\nPlease enter a password for user \"$USER\" to access the web ui"
htpasswd -c /var/www/rutorrent/.htpasswd $USER
HTPASSWD_EXIT_STATUS=$?
while [ $HTPASSWD_EXIT_STATUS -eq 3 ]
do
	echo -e "\nPassword missmatch. Please re-enter the password\n"
	htpasswd -c /var/www/rutorrent/.htpasswd $USER
	HTPASSWD_EXIT_STATUS=$?
done

#Restart Apache Web Server
service apache2 restart

#Start rtorrent
/etc/init.d/rtorrentInit.sh start

rm -f rutorrent.tar.gz plugins.tar.gz rtorrentInit.sh rtorrent.rc

echo -e "\nInstallation was succesful\nYou can now navigate to http://$IP_ADDRESS/rutorrent and login using user: $USER and the password you provided.\n"

exit 0
