#!/bin/sh

#################################################
# 												#
#			    OracleViaPHP v1.2				#	
#												#
# Script realized by Danilo Vizzarro			#
# You can find the step-by-step instructions on #
# www.danilovizzarro.it							#
# v1.0 - 12-JUL-2008							#
# v1.1 - 22-JUL-2008 							#
# v1.2 - 27-NOV-2009 							#
#################################################

# The script is tested on a virtual machine running OS Linux Ubuntu Server 8.04.1 i386 created on a Mac OS X running Leopard 10.5.3.
# It will install and configure Apache2, PHP5, OracleClient v10.2 and the OCI8 v1.3.3.

# 1. You need to be logged as root
# 2. You need to be connected to internet
# 3. You need to download the Oracle Client from the Oracle website

clear
echo "Oracle Client Installation..."
echo "Script tested an Virtual Machine Ubuntu 8.04.1 Server Edition x86 running on a Mac OS X Leopard 10.5.3."
echo "---"
echo "Starting Oracle Client Installation..."
echo "---"

# It downloads and installs of the basic components
apt-get update
apt-get --force-yes -y upgrade
apt-get install --force-yes -y ssh unzip apache2 php5 php5-dev php-pear build-essential libaio1

# It create the directory that will contain the 'Instant Client Package - Basic Lite' and the 'Instant Client Package - SDK'
mkdir -p /opt/oracle
cd /opt/oracle
cp /root/*.zip /opt/oracle
unzip \*.zip
cd /opt/oracle/instantclient_10_2

# It creates 2 links to the libraries libclntsh and libocci
ln -s libclntsh.so.10.1 libclntsh.so
ln -s libocci.so.10.1 libocci.so  

# It downloads and compiles the OCI8 module
# can be also use the command 
# pecl install oci8
# but the it will be required to specify manyally the ORACLE_HOME
mkdir -p /usr/local/src
cd /usr/local/src
pecl download oci8
tar xzf oci8*
cd oci*
phpize
./configure --with-oci8=shared,instantclient,/opt/oracle/instantclient_10_2
make
make install

# It adds the oci8.so extension to the php.ini
echo "extension=oci8.so" >> /etc/php5/cli/php.ini
echo "extension=oci8.so" >> /etc/php5/apache2/php.ini

# It sets the environment variables
cd /etc/init.d

cat apache2 | sed 's/ENV="env -i LANG=C PATH=\/usr\/local\/bin:\/usr\/bin:\/bin"/ENV=\"env -i LANG=C PATH=\/usr\/local\/bin:\/usr\/bin:\/bin ORACLE_HOME=\/opt\/oracle\/instantclient_10_2 TNS_ADMIN=\/opt\/oracle\/tns LD_LIBRARY_PATH=\/opt\/oracle\/instantclient_10_2\"/' > apache2.tmp

cp apache2 apache2.backup
rm apache2
mv apache2.tmp apache2
chmod 755 apache2

cd
echo "LD_LIBRARY_PATH=\"/opt/oracle/instantclient_10_2\"" >> .profile
echo "export LD_LIBRARY_PATH" >> .profile

# It creates some test pages
echo "<? phpinfo(); ?>" >> /var/www/info.php

# It restarts apache
/etc/init.d/apache2 restart

echo ""
echo "You can now try to connect the page http://your_ip_address/info.php"
echo "If everything went fine you should see the module oci8 in the list"

