#!/bin/bash

. ./param_bash.ini

clear
test -s ${IKIT_DIR}/tmp/.dbcreation.pid
if [ "$?" = "0" ] ; then
	CANCEL "A pid file exist:${IKIT_DIR}/tmp/.dbcreation.pid, check if db creation step is running, if not delete the pid file and execute this step again" "$0"
fi
if [ "$INST_TYPE" = "SW_ONLY" ] ; then
    INFORM "The installation type you choose doesn't require DB installation"
    INFORM "You can skip to the next step to create tnsnames.ora file"
    DONE "the installation type you choose doesn't require DB installation" "$0"
fi
START "Install database" "$0"
if [ ! -w /etc/passwd ] && [ ! ~ = "$ORACLE_HOME" ] ; then
	ERROR "Please run the script as $ORACLE_USER user or root user" "$0"
fi 

OracleUserExist
if [ "$?" = "1" ] ; then 
	ERROR "The $ORACLE_HOME user doesn't exist" "$0"
else 
	ORACLE_USER=`cat /etc/passwd | grep $ORACLE_HOME |tail -1 | awk -F: '{print $1}'` > /dev/null 2>&1
fi

if [ ! -d $ORACLE_HOME ] ; then
        ERROR "The directory $ORACLE_HOME does not exist" "$0"
fi

echo -n "Please enter the location of the db_copy scripts [$IKIT_FTP_DIR] : "
read ans
if [ "$ans" != "" ] ; then	
	IKIT_FTP_DIR=$ans	
fi 

if [ ! -f $IKIT_FTP_DIR/$COPY_DB_SCRIPT.tar.gz ] ; then 
	ERROR "The file $COPY_DB_SCRIPT.tar.gz does not exist in $IKIT_FTP_DIR" "$0"
fi

Update_params "IKIT_FTP_DIR" "${IKIT_FTP_DIR}"

echo -n "Please enter the location of the db files [$IKIT_FTP_DIR] : "
read ans
if [ "$ans" != "" ] ; then	
	IKIT_FTP_DIR=$ans	
fi 
if [ ! -f $IKIT_FTP_DIR/$EXLDB.tar.gz_aa ] ; then 
	ERROR "the files $EXLDB.tar.gz_* do not exist in $IKIT_FTP_DIR" "$0"
fi

INFORM "According to your info Data-Base will be installed on the following:"
INFORM "for each value, enter new value or confirm"
echo ""
echo -n "Archiving [$IKIT_DB_ARCH_DIR] : "
read ans
if [ "$ans" != "" ] ; then	
	IKIT_DB_ARCH_DIR=$ans	
	Update_params "IKIT_DB_ARCH_DIR" "${IKIT_DB_ARCH_DIR}"
fi
echo -n "Redo logs and System files [$IKIT_DB_SYS_DIR] : "
read ans
if [ "$ans" != "" ] ; then	
	IKIT_DB_SYS_DIR=$ans	
	Update_params "IKIT_DB_SYS_DIR" "${IKIT_DB_SYS_DIR}"
fi
echo -n "Data files [$IKIT_DB_DATA_DIR] : "
read ans
if [ "$ans" != "" ] ; then	
	IKIT_DB_DATA_DIR=$ans	
	Update_params "IKIT_DB_DATA_DIR" "${IKIT_DB_DATA_DIR}"
fi
for disk in ${IKIT_DB_ARCH_DIR} ${IKIT_DB_SYS_DIR} ${IKIT_DB_DATA_DIR}
do
        if [ ! -d ${disk}/oradata ] ; then
                mkdir -p ${disk}/oradata > /dev/null 2>&1
                if [ ! -d ${disk}/oradata ] ; then
                        ERROR "The directory ${disk}/oradata with oracle:dba permissions does not exist" "$0"
                else
                        chown $ORACLE_USER:$ORACLE_GROUP ${disk}/oradata > /dev/null 2>&1
                fi
	else
		if [ -d ${disk}/oradata/${ORACLE_SID} ] ; then
			ERROR "the directory $disk/oradata/$ORACLE_SID already exist on the server" "$0"
		fi
        fi
done

if [ ! -d $ORACLE_BASE/admin ] ; then
        mkdir -p $ORACLE_BASE/admin
        chown $ORACLE_USER:$ORACLE_GROUP $ORACLE_BASE/admin
fi

if [ "$SERVER_NAME" = "" ] ; then
	echo -n "Please enter the full server hostname [sname.exlibrisgroup.com] : "
	read ans
	if [ "$ans" != "" ] ; then
        	SERVER_NAME=$ans
        else
		ERROR "The full server hostname which was enterded is not valid" "$0"
        fi
	echo "The full server hostname that will be used for database configuration is ${SERVER_NAME}"
	echo -n "Proceed with installation? (y,n) [n] : "
	read ans
	if [ "$ans" != "y" ] ; then
       		 CANCEL "" "$0"
	fi
fi
Update_params "SERVER_NAME" "${SERVER_NAME}" 
	
WAIT "Extracting copy db scripts"

cd $IKIT_FTP_DIR
cat $IKIT_FTP_DIR/$COPY_DB_SCRIPT.tar.gz | gzip -d | ${TAR} -xvf - >> $gen_log
if [ "$?" != "0" ] ; then
	ERROR "Can not extract the $COPY_DB_SCRIPT package in the $IKIT_FTP_DIR directory" "$0"
fi
chown -R $ORACLE_USER:$ORACLE_GROUP $IKIT_FTP_DIR/$COPY_DB_SCRIPT
WAIT "Extracting Source Data files"

cat $IKIT_FTP_DIR/$EXLDB.tar.gz_* | gzip -d | tar -xvf - >> $gen_log
if [ "$?" != "0" ] && [ "$?" != "2" ]  ; then
	ERROR "Can not extract the $EXLDB package in the $IKIT_FTP_DIR directory" "$0"
fi

cd ${IKIT_FTP_DIR}/${EXLDB}

tar -xvf config.tar >> $gen_log
if [ "$?" != "0" ] && [ "$?" != "2" ]  ; then
        ERROR "Can not extract the ${IKIT_FTP_DIR}/${EXLDB}/config.tar package" "$0"
fi

cat ${IKIT_FTP_DIR}/${EXLDB}/config/ctl.dat | \
        sed -e "s:exlibris/ftp_from_exlibris:${IKIT_FTP_DIR}:"  \
        > ${IKIT_FTP_DIR}/${EXLDB}/config/ctl.dat.new
rm -f $IKIT_FTP_DIR/$EXLDB/config/ctl.dat >> $gen_log 2>&1
cp -p $IKIT_FTP_DIR/$EXLDB/config/ctl.dat.new $IKIT_FTP_DIR/$EXLDB/config/ctl.dat >> $gen_log 2>&1
cd $IKIT_FTP_DIR/$EXLDB
rm -f ${IKIT_FTP_DIR}/${EXLDB}/config.tar
tar cvf config.tar config  >> $gen_log 2>&1
chown -R $ORACLE_USER:$ORACLE_GROUP $IKIT_FTP_DIR/$EXLDB >> $gen_log 2>&1
chmod -R 777 $IKIT_FTP_DIR/$EXLDB >> $gen_log 2>&1
INFORM "DB creation runs in background, an email message will be sent at completion"
INFORM "DO NOT proceed further untill this step successfully complete"
INFORM "Logs can be found at:"
INFORM "General Installation Kit Log: $gen_log"
INFORM "DB Installation Log: ${IKIT_FTP_DIR}/copy_db/log/db_target.log"
cd ${IKIT_DIR}
if [ -w /etc/passwd ] ; then
	su $ORACLE_USER -c "source ${ORACLE_HOME}/.cshrc; bash db_creation.sh" & 
else
	bash db_creation.sh &
fi

DONE "An E-mail message will sent at completion" "$0"	
