#!/bin/sh

#################################################
# 												#
#			   	 TNSconnect v1.0				#	
#												#
# Script realized by Danilo Vizzarro			#
# You can find the step-by-step instructions on #
# www.danilovizzarro.it							#
# 12-JUL-2008									#
#################################################

# 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 create/update the file tnsnames.ora and it will create a test page that will try to connect the Oracle DB.

# 1. You need to be logged as root

mkdir -p /opt/oracle/tns
cd /opt/oracle/tns

echo "This script will configure tnsnames.ora"
echo "Please write the following information.";
echo -n "ORACLE SID (SIDDB): " 
read sid
echo -n "ORACLE SERVER IP ADDRESS (10.9.1.1): "
read server
echo -n "ORACLE SERVER PORT (1521): "
read port
echo -n "ORACLE DB USERNAME (ex. RAC_ACCNT): "
read oracle_user
echo -n "ORACLE DB PASSWORD: "
read oracle_password

echo "";
echo "This information will be now written on the file /opt/oracle/tnsnames.ora"
echo "If the information you typed are wrong the connection to Oracle wont work"
echo -n "Do you want to save the information you typed? (y/n): "

read confirm
case $confirm in yes|Yes|YES|y|Y)
	echo "$sid =" >> tnsnames.ora
	echo "  (DESCRIPTION =" >> tnsnames.ora
	echo "    (ADDRESS_LIST =" >> tnsnames.ora
	echo "      (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port))" >> tnsnames.ora
	echo "    )" >> tnsnames.ora
	echo "    (CONNECT_DATA =" >> tnsnames.ora
	echo "      (SID = $sid)" >> tnsnames.ora
	echo "    )" >> tnsnames.ora
	echo "  )" >> tnsnames.ora
	echo "" >> tnsnames.ora
	
	cd /var/www
	rm -f test.php

	echo "<?php" >> test.php
	echo "\$odbc = ocilogon($oracle_user, $oracle_password, $sid) or die( 'Could not connect the Oracle database!');" >> test.php 
	echo "if (\$odbc == false)" >> test.php
	echo "{\$msg = OCIError(\$odbc);}" >> test.php
	echo "else" >> test.php
	echo "{echo 'Your Oracle Connection is working properly!';}" >> test.php
	echo "?>" >> test.php 

	/etc/init.d/apache2 restart
	
	echo ""
	echo "You can now try to connect the page http://your_ip_address/test.php"
	echo "If everything went fine you would be able to connect the Oracle DB you defined"
	echo "If something went wrong you can delete maually the connections from the file /opt/oracle/tns/tnsnames.ora"
	;;
esac



