#!/bin/sh
# Installation script for CDR-Stats
# Copyright (C) <2011> <Star2Billing S.L>

# Modified from Areski's repo
# Author: José Pablo Méndez Soto

#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

#**IMPORTANT NOTES**
# -Check that your system is not running Django versions earlier than 1.3,
#otherwise it will be a mess.
# -Make your SQL password secure but don't include special characters =)
# I have had bad experiences with them, you will tell if during install
# you get SQL connection errors. If you want special charac.,  comment out
# the section "Setting up settings.py..." not to prompt for a password, 
# and set it manually in /usr/share/django_app/cdr_stats/settings.py


VERSION=v1.4.0
APACHE_CONF_DIR="/etc/apache2/sites-enabled/"
IPADDR=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`

echo ""
echo ""
echo "This will install CDR-Stats on your server"
echo "press any key to continue or CTRL-C to exit"
echo ""
echo ""
echo "Installing dependencies and python modules..."

apt-get -y install	mercurial\
			python-setuptools python-dev build-essential\
        		libapache2-mod-python libapache2-mod-wsgi\
			libmysqlclient-dev\
			git-core

easy_install pip
easy_install virtualenv
echo ""
echo ""


echo "Installing CDR-Stats..."
mkdir /usr/share/django_app/
cd /usr/src/
wget --no-check-certificate https://github.com/Star2Billing/cdr-stats/tarball/$VERSION
tar zxf $VERSION
ln -s /usr/src/Star*/cdr_stats /usr/share/django_app/cdr_stats

pip install -r /usr/share/django_app/cdr_stats/requirements.txt
echo ""
echo ""


echo "Updating Secret Key..."
RANDPASSW=`</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 50)`
sed -i "s/^SECRET_KEY.*/SECRET_KEY = \'$RANDPASSW\'/g" /usr/share/django_app/cdr_stats/settings.py
echo ""
echo ""


echo "Disabling debugging..."
sed -i "s/DEBUG = True/DEBUG = False/g" /usr/share/django_app/cdr_stats/settings.py
sed -i "s/TEMPLATE_DEBUG = DEBUG/TEMPLATE_DEBUG = False/g" /usr/share/django_app/cdr_stats/settings.py
echo ""
echo ""


echo "Setting up settings.py..."
echo ""
echo "What is the name of the database where Asterisk writes the call detail records?"
read CDRDB
echo ""
echo "What is the name of the user with access to the CDR database?"
read CDRUSER
echo ""
echo "What is the SQL password for this user?"
read CDRPASS
echo ""
echo ""

sed -i "s/backends.sqlite3/backends.mysql/"  /usr/share/django_app/cdr_stats/settings.py
sed -i "s/.*'NAME'/ 'NAME': '$CDRDB',#/" /usr/share/django_app/cdr_stats/settings.py
sed -i "/'USER'/s/''/'$CDRUSER'/" /usr/share/django_app/cdr_stats/settings.py
sed -i "/'PASSWORD'/s/''/'$CDRPASS'/" /usr/share/django_app/cdr_stats/settings.py
sed -i "/'HOST'/s/''/'localhost'/" /usr/share/django_app/cdr_stats/settings.py
sed -i "/'PORT'/s/''/'3306'/" /usr/share/django_app/cdr_stats/settings.py

echo ""
echo ""


echo "Syncing the database..."
cd /usr/share/django_app/cdr_stats/
python manage.py syncdb
python manage.py collectstatic -l --noinput
echo ""
echo ""

echo "Preparing \"A Patchy Server\"..."
echo '
Listen *:9000

<VirtualHost *:9000>
DocumentRoot /usr/share/django_app/cdr_stats/
ErrorLog /var/log/err-cdr-stats.log
LogLevel warn

WSGIPassAuthorization On
WSGIDaemonProcess cdr_stats user=www-data user=www-data threads=25
WSGIProcessGroup cdr_stats
WSGIScriptAlias / /usr/share/django_app/cdr_stats/django.wsgi

<Directory /usr/share/django_app/cdr_stats>
Order deny,allow
Allow from all
</Directory>

</VirtualHost>

' > $APACHE_CONF_DIR/cdr-stats.conf
sed -i "s/@/'/g" $APACHE_CONF_DIR/cdr-stats.conf
service apache2 restart
echo ""
echo ""


clear
echo "Installation Complete"
echo ""
echo ""
echo "Please log on to CDR-Stats at "
echo "http://$IPADDR:9000"
echo "the username and password are the ones you entered during this installation."
echo ""
echo "Congratulations. You are using Open Source Software"
echo ""