#!/bin/bash
# Prepara paquetes y entorno para desarrollar con Django y Oracle 11 sobre
# Ubuntu 12.04 en x86_64
# vtamara@pasosdeJesus.org. Dominio Publico. 2013

# Basado en instrucciones de: 
#   http://dhobsd.pasosdejesus.org/index.php?id=Desarrollar+Lernanta+en+OpenBSD+adJ
#   https://github.com/p2pu/lernanta/wiki/Setup-Lernanta-Manually
#   https://www.digitalocean.com/community/articles/installing-mod_wsgi-on-ubuntu-12-04

# Verifica parametros y entorno e inicializa variables 
# Instala un RPM via alien

function verinsor() {
	nom=$1;
	deb=$2;
	apt-cache policy ${deb} | grep "Installed" > /dev/null 2>&1
	if (test "$?" != "0") then {
		rpm=$3;
		if (test "$rpm" = "") then {
			rpm="$deb-${voibl}.x86_64.rpm";
		} fi;
		if (test ! -f "/tmp/$rpm") then {
			echo "Descargue en /tmp RPM de $nom ($rpm)";
			exit 1;
		} else {
			sudo alien -i /tmp/$rpm
		} fi;
	} fi;
}

# Verifica entorno, prepara variables e instala paquetes iniciales
# Llena variables miruta, mius y migr
function inicializa {
	# Ruta de Apache
	apv=$1
	if (test "$apv" = "") then {
		apv="/etc/apache2/sites-available/default"
			echo "No se especificó sitio de apache por emplear, se empleara '$apv'";
		echo "[ENTER] para continuar"
			read
	} fi;

	if (test ! -f "bin/prepara.sh") then {
		echo "Este script debe ejecutarse desde el directorio base con fuentes";
		exit 1;
	} fi;
	# Ruta a fuentes
	miruta=`pwd`;
	# usuario
	mius=`id -nu`;
	# grupo
	migr=`id -ng`;
	if (test "$mius" = "root") then { 
		mius=`export | grep "SUDO_UID[=]" | sed -e "s/.*=\"//g;s/\"//g"`;
		migr=`export | grep "SUDO_GID[=]" | sed -e "s/.*=\"//g;s/\"//g"`;
		if (test "$mius" = "0" -o "$mius" = "") then {
			echo "Ejecute este script como un usuario (con el cual se ejecutará el wsgi), no como root"
				exit 1;
		} fi;
	} fi;

	r=`find /usr/local -name "*instantclient*"`;
	if (test "$r" != "") then {
		echo "Eliminar primero instalaciones de instantclient en /usr/local";
		echo "$r"
			exit 1;
	} fi;

	sudo apt-get install python-dev python-setuptools python-imaging \
		python-m2crypto make sqlite3 alien libaio1 libmemcached-dev \
		apache2 apache2.2-common apache2-mpm-prefork apache2-utils \
		libexpat1 ssl-cert libapache2-mod-wsgi;

	sudo easy_install virtualenv;
	sudo easy_install pip;
	sudo pip install virtualenvwrapper;

	grep WORKON_HOME ~/.bashrc > /dev/null 2>&1
	if (test "$?" != "0") then {
		cat >> ~/.bashrc <<EOF
			export PATH=\$PATH:/usr/local/bin
			export WORKON_HOME=\$HOME/.virtualenvs
			export PIP_VIRTUALENV_BASE=\$WORKON_HOME
			export PIP_RESPECT_VIRTUALENV=true
			source /usr/local/bin/virtualenvwrapper.sh
EOF
		export WORKON_HOME=$HOME/.virtualenvs
		export PIP_VIRTUALENV_BASE=$WORKON_HOME
		export PIP_RESPECT_VIRTUALENV=true
		source /usr/local/bin/virtualenvwrapper.sh
	} fi;

# Oracle basado en http://marcelozambranav.blogspot.com/2012/08/how-to-install-oracle-sql-plus-on.html

	r=`apt-cache search "oracle-instantclient.*basic" 2> /dev/null`;
	if (test "$r" != "") then {
		# Version corta de Oracle InstantClient
		voib=`echo $r | sed -e "s/.*instantclient//g;s/-basic.*//g"`;
		# Version larga de Oracle InstantClient
		voibl=$voib;
		echo "Versión de Oracle InstantClient Basic instalada : $voib - ($voibl)";
	}  fi;
	if (test ! -f /tmp/oracle-instantclient*-basic-*x86_64.rpm) then {
		echo "Descargue en /tmp la versión más reciente del RPM de Oracle InstantClient Basic de http://download.oracle.com/otn/linux/instantclient/"
		exit 1;
	} else {
		nvoib=`ls /tmp/oracle-instantclient*-basic-*x86_64.rpm | head -n 1 | sed -e "s/.*instantclient//g;s/-basic.*//g"`;
		if (test "$voib" != "" -a "$voib" != "$nvoib") then {
			echo "Versión instalada y versión descargada de Oracle InstantClient Basic diferentes";
			exit 1;
		} fi;
		voib=$nvoib;
		voibl=`ls /tmp/oracle-instantclient*-basic-*x86_64.rpm | head -n 1 | sed -e "s/.*basic-//g;s/.x86_64.*//g"`;
		echo "Versión de RPM de Oracle InstantClient Basic disponible en /tmp: $voib - ($voibl)";
	} fi;

	verinsor "RPM de Oracle InstantClient Basic" oracle-instantclient${voib}-basic;
	verinsor "RPM de Oracle InstantClient sqlplus" oracle-instantclient${voib}-sqlplus;
	verinsor "RPM de Oracle InstantClient Tools" oracle-instantclient${voib}-tools;
	verinsor "RPM de Oracle InstantClient SDK" oracle-instantclient${voib}-devel;


	if (test ! -f /etc/ld.so.conf.d/oracle.conf) then {
		if (test -f /tmp/oracle.conf) then {
			sudo rm /tmp/oracle.conf
		} fi;
		cat > /tmp/oracle.conf <<EOF
			/usr/lib/oracle/$voib/client64/lib/
EOF
		sudo mv /tmp/oracle.conf /etc/ld.so.conf.d/
		sudo ldconfig
	} fi;

	if (test "${ORACLE_HOME}" != "" -a "${ORACLE_HOME}" != "/usr/lib/oracle/${voib}/client64/" ) then {
		echo "ORACLE_HOME no es /usr/lib/oracle/${voib}/client64/ ";
		exit 1;
	} fi;

	sudo ORACLE_HOME=/usr/lib/oracle/11.2/client64/ pip install cx-oracle
}

function wsgi {
	puerto=$1
	apv=$2
	miruta=$3
	mius=$4
	migr=$5
	if (test "$puerto" = "") then {
		echo "Falta puerto como primer parametro de WSGI";
		exit 1;
	} fi;
	if (test ! -f "$apv") then {
		echo "Problema con $apv";
		exit 1;
	} fi;
	if (test "$puerto" = "443") then {
		grep "WSGIScriptAlias /ic" $apv > /dev/null 2>&1
		if (test "$?" != "0") then {
			sudo ed $apv << EOF
/\/VirtualHost>
i

	Alias /site_media/ $miruta/app/InterfacesContables/site_media/
        Alias /static/ $miruta/app/static/

        LogLevel warn

        WSGIDaemonProcess DOMAIN user=$mius group=$migr processes=1 threads=15 maximum-requests=10000 python-path=/usr/lib/python2.7/site-packages
        WSGIProcessGroup DOMAIN
        WSGIScriptAlias /ic $miruta/app/conf/apache/django.wsgi

        <Directory $miruta/app/InterfacesContables/site_media>
                Order deny,allow
                Allow from all
                Options -Indexes FollowSymLinks
        </Directory>

        <Directory $miruta/app/conf/apache>
                Order deny,allow
                Allow from all
        </Directory>
.
w
q
EOF
		} fi;
	} else {
		grep "WSGIScriptAlias /" $apv > /dev/null 2>&1
		if (test "$?" != "0") then {
			sudo ed $apv << EOF
/\/VirtualHost>
a
Listen $puerto
<VirtualHost *:$puerto>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog \${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog \${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

	Alias /site_media/ $miruta/app/InterfacesContables/site_media/
        Alias /static/ $miruta/app/static/

        LogLevel warn

        WSGIDaemonProcess DOMAIN user=$mius group=$migr processes=1 threads=15 maximum-requests=10000 python-path=/usr/lib/python2.7/site-packages
        WSGIProcessGroup DOMAIN
        WSGIScriptAlias / $miruta/app/conf/apache/django.wsgi

        <Directory $miruta/app/InterfacesContables/site_media>
                Order deny,allow
                Allow from all
                Options -Indexes FollowSymLinks
        </Directory>

        <Directory $miruta/app/conf/apache>
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>
.
w
q
EOF
		} fi;
	} fi;

	grep "WSGIPythonPath " $apv > /dev/null 2>&1
	if (test "$?" != "0") then {
			sudo ed $apv << EOF
/\/VirtualHost>
a
        WSGIPythonPath /usr/lib/python2.7/site-packages/
.
w
q
EOF
	} fi;

	sudo service apache2 restart;
# Instalando dependencias de python gloalmente para que esten disponibles ante el WSGI
	(cd app/InterfacesContables; sudo make pip);
	echo "-------------------";
	echo "Paquetes instalados en sistema y Apache configurado con WSGI y reiniciado.";
	echo "Asegurese de habilitar el sitio de Apache (por ejemplo a2ensite default) y examinar";
	echo "Empiece a desarrollar con:";
	echo "  . ~/.bashrc";
	echo "  mkvirtualenv ic --system-site-packages";
	echo "  workon ic";
	echo "  cd app/InterfacesContables";
	echo "  make pip";

}


