#!/bin/sh

if [ $(id -u) != "0" ]
then
    echo "You must be root to run the configure script.  Login as root and then run the 
configure script."
    exit 1
fi

ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
ORACLE_OWNER=oracle
ORACLE_SID=XE
LSNR=$ORACLE_HOME/bin/lsnrctl
SQLPLUS=$ORACLE_HOME/bin/sqlplus
export ORACLE_HOME
export ORACLE_SID
export PATH=$ORACLE_HOME/bin:$PATH

export LC_ALL=C

if `grep -q ^dba: /etc/group`
then
	echo ""
else
	pw groupadd dba
fi
id oracle > /dev/null 2>&1
status=$?
if test $status -eq 0
then
        groups oracle | grep dba > /dev/null
	status=$?
        if test $status != 0
        then
                pw usermod -G dba oracle
        fi
else
        pw useradd oracle -g dba -G dba -d /usr/lib/oracle/xe -s /compat/linux/bin/bash
fi

chown -R oracle:dba /usr/lib/oracle/xe
/compat/linux/sbin/ldconfig >/dev/null
chown -R oracle:dba /usr/lib/oracle/xe

sgamin=146800640
pgamin=16777216
sgamax=805306368
pgamax=268435456
TM=`cat /compat/linux/proc/meminfo | grep '^MemTotal' | awk '{print $2}'`
TM=`echo $TM / 1024 | bc`
TM=`echo 0.40 \* $TM | bc | sed "s/\..*//"`
TMSP=`echo $TM-40 | bc`
sga_target=`echo 0.75 \* $TMSP | bc`
pga_target=`echo 0.25 \* $TMSP | bc `
sga=`echo $sga_target \* 1048576  | bc | sed "s/\..*//"`
pga=`echo $pga_target \* 1048576  | bc | sed "s/\..*//"`
check=`echo $sga \< $sgamin | bc`
if test $check != 0
then
        sga=$sgamin
fi

check=`echo $pga \< $pgamin | bc`
if test $check != 0
then
        pga=$pgamin
fi

if test `echo $sga + $pga \> $sgamax + $pgamax | bc`
then
	check=`echo $sga \> $sgamax | bc`
	if test $check != 0
	then
		sga=$sgamax
	fi

	check=`echo $pga \> $pgamax | bc`
	if test $check != 0
	then
		pga=$pgamax
	fi
fi

sed -i "" "s/%sga_target%/$sga/g" $ORACLE_HOME/config/scripts/init.ora
sed -i "" "s/%pga_aggregate_target%/$pga/g" $ORACLE_HOME/config/scripts/init.ora
sed -i "" "s/%sga_target%/$sga/g" $ORACLE_HOME/config/scripts/initXETemp.ora
sed -i "" "s/%pga_aggregate_target%/$pga/g" $ORACLE_HOME/config/scripts/initXETemp.ora

chmod 6751 $ORACLE_HOME/bin/oracle
chmod 755 $ORACLE_HOME/bin/sqlplus

rm -fr $ORACLE_HOME/rdbms/admin/patch

if [ ! -d /var/tmp/.oracle ]
then
  mkdir -p /var/tmp/.oracle;
fi

chmod 01777 /var/tmp/.oracle
chown root  /var/tmp/.oracle

if [ ! -d /tmp/.oracle ]
then
  mkdir -p /tmp/.oracle;
fi

chmod 01777 /tmp/.oracle
chown root  /tmp/.oracle

# configure_perform()
#
# Instantantiate listener.ora,tnsnames.ora,and create the database,
# sets the password,start the listener,and adds database to inittab
# if necessary

configure_perform()
{
    sed -i "" "s/%hostname%/`hostname`/g" $ORACLE_HOME/network/admin/listener.ora
    sed -i "" "s/%port%/$LISTENER_PORT/g" $ORACLE_HOME/network/admin/listener.ora
    chown oracle:dba $ORACLE_HOME/network/admin/listener.ora

    sed -i "" "s/%hostname%/`hostname`/g" $ORACLE_HOME/network/admin/tnsnames.ora
    sed -i "" "s/%port%/$LISTENER_PORT/g" $ORACLE_HOME/network/admin/tnsnames.ora
    chown oracle:dba $ORACLE_HOME/network/admin/tnsnames.ora

    sed -i "" "s/%httpport%/$HTTP_PORT/g" $ORACLE_HOME/config/scripts/postDBCreation.sql
    chown oracle:dba $ORACLE_HOME/config/scripts/postDBCreation.sql

    if test $LISTENER_PORT -ne 1521
    then
	if [ -f /tmp/local_listener ]
	then
	cat >/tmp/local_listener$$ <<EOF
###########################################
# Registration of instance with listener
###########################################
local_listener="(ADDRESS = (PROTOCOL=TCP) (HOST=%hostname%) (PORT=%port%))"
EOF
	chmod 664 /tmp/local_listener$$
	cat /tmp/local_listener$$ >> $ORACLE_HOME/config/scripts/init.ora
	else
	cat >/tmp/local_listener <<EOF
###########################################
# Registration of instance with listener
###########################################
local_listener="(ADDRESS = (PROTOCOL=TCP) (HOST=%hostname%) (PORT=%port%))"
EOF
	chmod 664 /tmp/local_listener
	cat /tmp/local_listener >> $ORACLE_HOME/config/scripts/init.ora
	fi

	if test -f /tmp/local_listener
	then
		rm -fr /tmp/local_listener
	elif test -f /tmp/local_listener$$
	then
		rm -fr /tmp/locallistener$$
	fi
	
	sed -i "" "s/%port%/$LISTENER_PORT/g" $ORACLE_HOME/config/scripts/init.ora
	sed -i "" "s/%hostname%/`hostname`/g" $ORACLE_HOME/config/scripts/init.ora
        chown oracle:dba $ORACLE_HOME/config/scripts/init.ora

    fi

    sed -i "" "s/%httpport%/$HTTP_PORT/g" $ORACLE_HOME/config/scripts/DatabaseHomePage.sh
    chown oracle:dba $ORACLE_HOME/config/scripts/DatabaseHomePage.sh

    sed -i "" "s/%httpport%/$HTTP_PORT/g" $ORACLE_HOME/config/scripts/readonlinehelp.sh
    chown oracle:dba $ORACLE_HOME/config/scripts/readonlinehelp.sh

    if [ -f $ORACLE_HOME/bin/tnslsnr ]  
    then
     	echo -n "Starting Oracle Net Listener..."
       	su -s $ORACLE_OWNER -c "$LSNR  start" > /dev/null 2>&1
	echo "Done"
    fi

    echo -n "Configuring Database..."
    su -s $ORACLE_OWNER -c "$ORACLE_HOME/config/scripts/XE.sh" > /dev/null 2>&1
    err=`grep "ORA-44410" $ORACLE_HOME/config/log/*.log`
    if test "$err" != ""
    then
	echo "Database Configuration failed.  Look into $ORACLE_HOME/config/log for details"
	exit 1
    fi
    echo  alter user sys identified by \"$ORACLE_PASSWORD\"\; | su -s $ORACLE_OWNER -c "$SQLPLUS -s / as sysdba" > /dev/null 2>&1
 echo  alter user system identified by \"$ORACLE_PASSWORD\"\; | su -s $ORACLE_OWNER -c "$SQLPLUS -s / as sysdba" > /dev/null 2>&1
    echo "Done"

    chmod -R 640 /usr/lib/oracle/xe/oradata/XE
    chmod 750 /usr/lib/oracle/xe/oradata/XE
#     rm -fr $ORACLE_HOME/config/seeddb 

    if [ -f /etc/oratab ]
    then
	echo "XE:$ORACLE_HOME:N" >> /etc/oratab
    else
   	echo "XE:$ORACLE_HOME:N" >> /etc/oratab
	chown oracle:dba /etc/oratab
	chmod 644 /etc/oratab
    fi
    
   echo -n "Starting Oracle Database 10g Express Edition Instance..."
   pmon=`ps -U $ORACLE_OWNER | egrep pmon_$ORACLE_SID'\>' | grep -v grep`

   if [ "$pmon" = "" ];
   then
	   su -s $ORACLE_OWNER -c "$SQLPLUS -s /nolog @$ORACLE_HOME/config/scripts/startdb.sql" > /dev/null 2>&1
   fi
   echo "Done"

   echo "Installation Completed Successfully."

	
   return 0
}

configure_ask()
{
	cat <<EOF

Oracle Database 10g Express Edition Configuration
-------------------------------------------------
This will configure on-boot properties of Oracle Database 10g Express 
Edition.  The following questions will determine whether the database should 
be starting upon system boot, the ports it will use, and the passwords that 
will be used for database accounts.  Press <Enter> to accept the defaults. 
Ctrl-C will abort.

EOF

    #get the http port value
	while :
	do
	    while [ 1 ]
	    do
            echo -n Specify the HTTP port that will be used for Oracle Application Express [8080]:
            read LINE
            if [ -z $LINE ]
            then
                LINE=8080
            fi
            port=`netstat -an -p tcp | grep .$LINE | awk '{print $4}' | cut -d'.' -f2`
            if [ "$port" = "$LINE" ]
            then
                echo Port $port appears to be in use by another application.\
                Please specify a different port.
            else
                break;
            fi
        done

	    case "$LINE" in
	    "")
            break
            ;;
        *[^0-9]*)
            echo "Invalid http port: $LINE"
            ;;
        *)
            HTTP_PORT=$LINE
            break
            ;;
	    esac
	done
    
    #get the listener port value
	while : 
	do
        echo 
        while [ 1 ]
        do
            echo -n Specify a port that will be used for the database listener [1521]:
            read LINE
            if [ -z $LINE ]
            then
                LINE=1521
            fi
            echo
            port=`netstat -an -p tcp | grep .$LINE | awk '{print $4}' | cut -d'.' -f2`
            if [ "$port" = "$LINE" ]
            then	
                echo Port $port appears to be in use by another application.\
                Please specify a different port.
            else
                break;
            fi
        done
	          
        case "$LINE" in
        "")
            break
            ;;
        *[^0-9]*)
            echo "Invalid port: $INE" >&2
            ;;
	    *) 
            	if [ "$HTTP_PORT" != "$LINE" ]
	        then
        	    LISTENER_PORT=$LINE
                    break
	        else
        	    echo Database listener cannot be configured on the same port as Oracle Application Express.
	        fi
            ;;
	    esac
	done
    
    #get the database password
	    while :
	    do
	    echo -n "Specify a password to be used for database accounts.  Note that the same
password will be used for SYS and SYSTEM.  Oracle recommends the use of 
different passwords for each database account.  This can be done after 
initial configuration:"
	   while [ 1 ]
	   do
	     stty -echo > /dev/null 2>&1
	     temp=`echo $IFS`
             export IFS="\n"	
	     while [ 1 ]
  	     do
	     read LINE
             while [ -z "$LINE" ]
	     do
		 echo
		 echo -n "Password can't be null. Enter password:"  
	         read LINE
	     done

#	     result=`expr index "$LINE" [\'\"]`
#	     if [ $result != 0 ];
#	     then
#             	echo 
#	        echo -n "The password you entered contains invalid characters. Enter password:"	
#	     else
		break
#	     fi
	     done
		echo
		echo -n "Confirm the password:"
	        read LINE1
		echo
                if [ "$LINE" != "$LINE1" ];
		then
			echo    
			echo -n "Passwords do not match.  Enter the password:"
		else
			break
		fi
	done
            stty echo > /dev/null 2>&1
            ORACLE_PASSWORD=$LINE
	    export IFS=$temp
            break;
	done
}

configure_ask
configure_perform

