﻿#!/bin/bash


 ######     ###    ##    ## ######## ########   #######  ##     ## 
##    ##   ## ##   ##   ##  ##       ##     ## ##     ##  ##   ##  
##        ##   ##  ##  ##   ##       ##     ## ##     ##   ## ##   
##       ##     ## #####    ######   ########  ##     ##    ###    
##       ######### ##  ##   ##       ##     ## ##     ##   ## ##   
##    ## ##     ## ##   ##  ##       ##     ## ##     ##  ##   ##  
 ######  ##     ## ##    ## ######## ########   #######  ##     ## 
#					par MardamBey (iam.mardambey@gmail.com)
#					https://github.com/MardamBeyK/Cakebox
#					Dernière update : Juin 2012

# ~~~~~~~ PARAMETRES DU SCRIPT ~~~~~~~~
#
#	* 	--wwwdir=/chemin/vers/le/serveur/web 		Par défaut /var/www /!\ Ne mas mettre de "/" final
#
#	* 	--enable-htaccess 				Modifie Apache2 pour activer les .htaccess; 
#								Obligatoire si Apache n'est pas encore installé ou configuré sur votre serveur
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# URL des fichiers à télécharger pendant l'installation
# Attention à bien modifier dans le code, plus loin, le nom des archives à extraire si changement de version !
URL_LIBTORRENT="https://github.com/downloads/MardamBeyK/Cakebox/libtorrent-0.13.0.tar.gz"
URL_RTORRENT="https://github.com/downloads/MardamBeyK/Cakebox/rtorrent-0.9.0.tar.gz"
URL_XMLRPC="https://github.com/downloads/MardamBeyK/Cakebox/xmlrpc-c-stable.tar.gz"
URL_RUTORRENT="https://github.com/downloads/MardamBeyK/Cakebox/rutorrent-3.4.tar.gz"
PLUGINS_LIST="diskspace erasedata unpack create"

# Génération d'un password aléatoire pour le .htaccess
function randompass () {
        local randompassLength
        if [ $1 ]; then
                randompassLength=$1
        else
                randompassLength=8
        fi

        pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength
        echo $pass
}

# Parsing des paramètres
for i in $*
do
        case $i in
        --wwwdir=*)
                WWWDIR=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
                ;;
        --enable-htaccess)
                ENABLEHT=1
                ;;
        *)
                # unknown option
                ;;
        esac
done

# Configuration du repertoire web publique
if [ -z $WWWDIR ]
then
        WWWDIR="/var/www"
fi


# Détection du gestionnaire de paquet à utiliser (aptitude en priorité)
if [ "`dpkg --status aptitude | grep Status:`" == "Status: install ok installed" ]
then
        packetg="aptitude"
else
        packetg="apt-get"
fi

# Installation des packets vitaux
$packetg update
$packetg install -y subversion git php5 libapache2-mod-scgi php5-curl build-essential automake libtool libcppunit-dev libcurl3-dev libsigc++-2.0-dev unzip unrar curl libncurses-dev git screen


# Installation de Rutorrent
cd $WWWDIR
wget $URL_RUTORRENT
tar -zxvf rutorrent-3.4.tar.gz
chmod 777 -R rutorrent/share/
# INSTALLATION PLUGINS
cd rutorrent/plugins/
for X in $PLUGINS_LIST
do
        echo "DL of $X"
        svn co http://rutorrent.googlecode.com/svn/trunk/plugins/$X
done


# Installation de Cakebox
cd $WWWDIR
git clone git://github.com/MardamBeyK/Cakebox.git
mv Cakebox/ cakebox/
chmod 777 cakebox/downloads cakebox/data

# Installation d'un crontab pour attribuer des droits au dossier /downloads de Cakebox
# Par défaut, le cron est activé chaque minute, modifiez-le à votre convenance
cd
crontab -l > crontab.new
echo "* * * * * chmod -R 777 $WWWDIR/cakebox/downloads" >> crontab.new
crontab crontab.new

# Installation du mod SGCI d'Apache (obligatoire pour rtorrent et rutorrent)
echo SCGIMount /RPC2 127.0.0.1:5000 >> /etc/apache2/apache2.conf

# Génération du .htpasswd
htpassword=`randompass 10`
htpasswd -mbc $WWWDIR/.htpasswd admin $htpassword

# Generation des .htaccess
echo "AuthName \"Restricted\"
AuthType Basic
AuthUserFile \"$WWWDIR/.htpasswd\"
Require valid-user" > $WWWDIR/.htaccess # Protège la racine du serveur web

echo "Satisfy Any
Options -Indexes
" > $WWWDIR/cakebox/data/.htaccess	# Protège les downloads de Cakebox (pas d'index of/)

echo "Satisfy Any
Options -Indexes
" > $WWWDIR/cakebox/downloads/.htaccess	# Protège les downloads de Cakebox (pas d'index of/)

# Configuration et activation des .htaccess si demandé par l'utilisateur
if [ -n $ENABLEHT ]
then
        cd /etc/apache2/sites-enabled/
        sed '0,/AllowOverride None/ s//AllowOverride All/' 000-default > 000-default.1
        sed '0,/AllowOverride None/ s//AllowOverride All/' 000-default.1 > 000-default
fi

# Redémarage d'Apache pour prendre en compte la nouvelle config
a2enmod scgi && /etc/init.d/apache2 restart


# Installation de XMLRPC
cd
wget $URL_XMLRPC && tar -zxvf xmlrpc-c-stable.tar.gz
cd stable
./configure --disable-cplusplus
make
make install

# Installation de la libtorrent
cd
wget $URL_LIBTORRENT && tar -zxvf libtorrent-0.13.0.tar.gz
cd libtorrent-0.13.0
./autogen.sh
./autogen.sh
./configure
make
make install

# Installation de Rtorrent
cd
wget $URL_RTORRENT && tar -zxvf rtorrent-0.9.0.tar.gz
cd rtorrent-0.9.0
./autogen.sh && ./configure --with-xmlrpc-c 
make
make install
ldconfig

# Configuration de Rtorrent
cd
echo "directory = $WWWDIR/cakebox/downloads
session = ~/session
port_range = 6890-6999
port_random = no
check_hash = yes
use_udp_trackers = yes
schedule = watch_directory,15,15,load_start=~/watch/*.torrent
dht = auto
dht_port = 6881
scgi_port = 127.0.0.1:5000" > .rtorrent.rc
mkdir session

# Application des droits nécessaire à Cakebox et Rutorrent
chown -R www-data:www-data $WWWDIR

# Lancement de rtorrent en background
screen -fa -d -m /usr/local/bin/rtorrent

# Affichage final
echo "--"
echo "--"
echo "=========== FIN DE L'INSTALLATION ! On dirait que tout a fonctionné ! ============"
echo "Username : admin"
echo "Password : $htpassword"
echo "Vous pouvez changer vos logins dans $WWWDIR/.htpasswd ou grâce à la commande htpasswd."
echo "--"
echo "Maintenant, rendez-vous sur :"
echo "http://ip_of_your_server/cakebox/"
echo "et"
echo "http://ip_of_your_server/rutorrent/"
echo "Profitez bien !"
if [ -n $ENABLEHT ]
then
        echo "[Note : les .htaccess ont été activé pendant l'installation, comme demandé avec --enable-htaccess]"
fi
echo "==========================================================================="
echo "--"
echo "--"

