#!/bin/sh

#################################################
# 												#
#			   MacOracleViaPHP v1.1				#	
#												#
# Script realized by Danilo Vizzarro			#
# You can find the step-by-step instructions on #
# www.danilovizzarro.it							#
# v1.0 - 30-JUL-2008							#
# v1.1 -  1-AUG-2008							#
#################################################

# 1. You need to be logged as root
# 2. You need to download the Oracle Client, SDK, SqlPlus, Apache2, Oci8 and PHP5 and save it on the /var/root directory
# in the following format
# instantclient-basic-macosx-10.2.0.4.0.zip			http://www.oracle.com/technology/software/tech/oci/instantclient/index.html
# instantclient-sdk-macosx-10.1.0.3.zip				http://www.oracle.com/technology/software/tech/oci/instantclient/index.html
# instantclient-sqlplus-macosx-10.2.0.4.0.zip		http://www.oracle.com/technology/software/tech/oci/instantclient/index.html
# httpd-2.2.9.tar.gz	tar.gz	http://httpd.apache.org/download.cgi
# oci8-1.3.4.tgz		tgz		http://pecl.php.net/package/oci8
# php-5.2.6.tar.gz		tar.gz	http://www.php.net/downloads.php

clear
echo "*******************************************************************"
echo "*                    MacOracleViaPHP v1.0                         *"
echo "*******************************************************************"
echo "* The script will install the Oracle Instant Client, SqlPlus,     *"
echo "* PHP and the OCI library on your Mac.                            *" 
echo "* You can find the updated version of the code, a video demo      *"
echo "* and a step-by-step tutorial on www.danilovizzarro.it/?p=11      *"
echo "* Script tested on Mac OS X running Leopard 10.5.4                *"
echo "* Script created by Danilo Vizzarro.                              *"
echo "*******************************************************************"

if [ $USER == root ]
then 

# INSTANT CLIENT INSTALLATION + SQLPLUS
mkdir -p /opt/oracle
cd /opt/oracle
cp /var/root/*.zip /opt/oracle
unzip \*.zip
cd /opt/oracle/instantclient_10_2

ln -s libclntsh.dylib.10.1 libclntsh.dylib 
ln -s libocci.dylib.10.1 libocci.dylib 

cp /etc/profile ~/.profile
cp /etc/profile ~/.profile.backup

cd

echo "" >> ~/.profile
echo "DYLD_LIBRARY_PATH=\"/opt/oracle/instantclient_10_2\"" >> ~/.profile
echo "export DYLD_LIBRARY_PATH" >> ~/.profile
echo "" >> ~/.profile
echo "TNS_ADMIN=/opt/oracle/tns" >> ~/.profile
echo "export TNS_ADMIN" >> ~/.profile
echo "" >> ~/.profile
echo "ORACLE_HOME=\"/opt/oracle/instantclient_10_2\"" >> ~/.profile
echo "export ORACLE_HOME" >> ~/.profile
echo "" >> ~/.profile

ln -s /opt/oracle/instantclient_10_2/sqlplus /usr/bin/sqlplus

DYLD_LIBRARY_PATH=/opt/oracle/instantclient_10_2
ORACLE_HOME=/opt/oracle/instantclient_10_2
TNS_ADMIN=/opt/oracle/tns
export DYLD_LIBRARY_PATH
export ORACLE_HOME
export TNS_ADMIN

cd
mkdir -p /var/root/soft
mv oci* php* http* /var/root/soft
cd /var/root/soft
tar xvfz php*
tar xvfz http*
gunzip  oci*.tgz
tar xvf oci*


# APACHE2 INSTALLATION
cd /var/root/soft/http*
./configure --enable-layout=Darwin --enable-mods-shared=all 
make
make install


# PHP5 INSTALLATION
cd /var/root/soft/php*

ln -s /usr/libexec/apache2/libphp5.so /usr/libexec/apache2/libphp5.so.apple
./configure --with-apxs2 --with-oci8=instantclient,/opt/oracle/instantclient_10_2

cp makefile makefile.backup
cat makefile | sed 's/CFLAGS_CLEAN = /CFLAGS_CLEAN = -I\/usr\/include -arch ppc -arch i386 -isysroot \/Developer\/SDKs\/MacOSX10.5.sdk \#CFLAGS_CLEAN = /' > makefile1.tmp 
cat makefile1.tmp | sed 's/EXTRA_LDFLAGS = /EXTRA_LDFLAGS = -arch ppc -arch i386 -isysroot \/Developer\/SDKs\/MacOSX10.5.sdk -Wl,-syslibroot,\/Developer\/SDKs\/MacOSX10.5.sdk \#EXTRA_LDFLAGS = /' > makefile2.tmp 
rm makefile
mv makefile2.tmp makefile
rm makefile1.tmp

make
make install


# OCI8 INSTALLATION
cd /var/root/soft/oci*
phpize
./configure --with-oci8=shared,instantclient,/opt/oracle/instantclient_10_2
make
make install


echo "extension=oci8.so" >> /private/etc/php.ini
echo "extension=oci8.so" >> /etc/php.ini
	
apachectl restart

clear
echo "Please write the following information.";
echo "ORACLE SID (SIDDB): \c" 
read sid
echo "ORACLE SERVER IP ADDRESS (10.9.1.1): \c"
read server
echo "ORACLE SERVER PORT (1521): \c"
read port
echo "ORACLE DB USERNAME (ex. RAC_ACCNT): \c"
read oracle_user
echo "ORACLE DB PASSWORD: \c"
read oracle_password

echo "<?php" >> /Library/WebServer/Documents/test.php
echo "\$rnum=rand(0,99999999);" >> /Library/WebServer/Documents/test.php
echo "\$conn = oci_new_connect('$oracle_user', '$oracle_password'," >> /Library/WebServer/Documents/test.php
echo "'" >> /Library/WebServer/Documents/test.php
echo "(DESCRIPTION =" >> /Library/WebServer/Documents/test.php
echo "           (ADDRESS = " >> /Library/WebServer/Documents/test.php
echo "        (PROTOCOL = TCP)" >> /Library/WebServer/Documents/test.php
echo "        (HOST = $server)" >> /Library/WebServer/Documents/test.php
echo "        (PORT = $port)" >> /Library/WebServer/Documents/test.php
echo "        (HASH = '.\$rnum.')" >> /Library/WebServer/Documents/test.php
echo "     )" >> /Library/WebServer/Documents/test.php
echo "         (CONNECT_DATA =(SID = $sid))" >> /Library/WebServer/Documents/test.php
echo "     )" >> /Library/WebServer/Documents/test.php
echo "')" >> /Library/WebServer/Documents/test.php
echo "or die( 'Could not connect the Oracle database!');" >> /Library/WebServer/Documents/test.php 
echo "if (\$conn == false)" >> /Library/WebServer/Documents/test.php
echo "{\$msg = OCIError(\$conn);}" >> /Library/WebServer/Documents/test.php
echo "else" >> /Library/WebServer/Documents/test.php
echo "{echo 'Your Oracle Connection is working properly!';}" >> /Library/WebServer/Documents/test.php
echo "?>" >> /Library/WebServer/Documents/test.php

echo "<?php phpinfo(); ?>"  >> /Library/WebServer/Documents/info.php

else
echo "Please log in as root and execute again the script."
echo ""
fi

