#该脚本包含以下模块
#sys_check,系统检查
#evn_check,配置系统环境变量
#oracle_check,对Oracle安装目录进行检查
#silent_check, 对静默安装的配置文件进行配置。
#oracle_ins,安装模块
#clean_check,清除模块
#rollback,回滚模块

#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail

ora_DIR=${1:-"/oracle"}
ora_HOME=$ora_DIR/product/11.2.0/db_1
ora_USER=oracle
ora_z1=${2:-"linux.x64_11gR2_database_1of2.zip"}
ora_z2=${3:-"linux.x64_11gR2_database_2of2.zip"}
host_NAME=${4:-"/oracledb"}
ora_rsp=$ora_DIR/database/response/db_install.rsp
USER_PASSW=${5:-"oracle"}
USER_HOME=${6:-"/oracle"}
FTP=${8:-"10.243.124.6"}

#sys_check,
function sys_check() {
if grep 'Red Hat' /etc/system-release>/dev/null 2>&1 && [ $(awk 'BEGIN {FS=" "} {print int($7)}' /etc/system-release) -ge 6 ];then
    echo "$(date "+%Y-%m-%d"): check $(grep "Red Hat" /etc/system-release) supported"
elif grep 'CentOS' /etc/system-release>/dev/null 2>&1  &&  [ $(awk 'BEGIN {FS=" "} {print int($4)}' /etc/system-release) -ge 6 ];then
    echo "$(date "+%Y-%m-%d"): check $(grep 'CentOS' /etc/system-release) supported"
else
    echo "$(date "+%Y-%m-%d"): Our installing shell support 'Redhat 6 or later' and 'centos 6or later'."
    echo '            Please check your system`s release.'
    echo '            the installing shell exit'
    exit
fi
}



#evn_check,
function evn_check() {
touch  $ora_DIR/.bash_profile
if grep 'ORACLE_BASE=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i "s#ORACLE_BASE=.*#ORACLE_BASE=$ora_DIR#g" $ora_DIR/.bash_profile
    #echo "ORACLE_BASE=$ora_DIR modified"
else
    echo "export ORACLE_BASE=$ora_DIR">>$ora_DIR/.bash_profile
    #echo "ORACLE_BASE=$ora_DIR imported"
fi

if grep 'ORACLE_HOME=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i "s#ORACLE_HOME=.*#ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1#g" $ora_DIR/.bash_profile
    #echo "ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1 modified"
else
    echo "export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1">>$ora_DIR/.bash_profile
    #echo "ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1 imported"
fi

if grep 'PATH=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i 's#PATH=.*#PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin#g' $ora_DIR/.bash_profile
    #echo "PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin modified"
else
    echo 'export PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin'>>$ora_DIR/.bash_profile
    #echo "PATH=$PATH:$HOME/bin:$ORACLE_HOME/bin imported"
fi

if grep 'ROACLE_PID=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i 's#ROACLE_PID=.*#ROACLE_PID=ora11g#g' $ora_DIR/.bash_profile
    #echo "ROACLE_PID=ora11g modified"
else
    echo 'export ROACLE_PID=ora11g'>>$ora_DIR/.bash_profile
    #echo "ROACLE_PID=ora11g imported"
fi

if grep 'NLS_LANG=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i 's#NLS_LANG=.*#NLS_LANG=AMERICAN_AMERICA.AL32UTF8#g' $ora_DIR/.bash_profile
    #echo "NLS_LANG=AMERICAN_AMERICA.AL32UTF8 modified"
else
    echo 'export NLS_LANG=AMERICAN_AMERICA.AL32UTF8'>>$ora_DIR/.bash_profile
    #echo "NLS_LANG=AMERICAN_AMERICA.AL32UTF8 imported"
fi

if grep 'LD_LIBRARY_PATH=' $ora_DIR/.bash_profile>/dev/null 2>&1;then
    sed -i 's#LD_LIBRARY_PATH=.*#LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib#g' $ora_DIR/.bash_profile
    #echo "LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib modified"
else
    echo 'export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib'>>$ora_DIR/.bash_profile
    #echo "LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib imported"
fi

echo "$(date "+%Y-%m-%d"): check system EVN and profile of user overwrited"
}

#silent_check
function silent_check(){
# local ora_rsp='$ora_DIR'/database/response/db_install.rsp
if [ -e $ora_home ] ;then
   echo "$(date "+%Y-%m-%d"): found $ora_home "
else
    mkdir -p $ora_DIR/product/11.2.0/db_1
fi

 sed -i 's#oracle.install.option=.*#oracle.install.option=INSTALL_DB_SWONLY#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#ORACLE_HOSTNAME=.*#ORACLE_HOSTNAME='$host_NAME'#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#UNIX_GROUP_NAME=.*#UNIX_GROUP_NAME=oinstall#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#INVENTORY_LOCATION=.*#INVENTORY_LOCATION='$ora_DIR'/oraInventory#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#SELECTED_LANGUAGES=.*#SELECTED_LANGUAGES=en,zh_CN#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#ORACLE_HOME=.*#ORACLE_HOME='$ora_HOME'#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#ORACLE_BASE=.*#ORACLE_BASE='$ora_DIR'#g'  $ora_rsp >/dev/null 2>&1
 sed -i 's#oracle.install.db.isCustomInstall.*#oracle.install.db.isCustomInstall=false#g'  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.DBA_GROUP=.*#oracle.install.db.DBA_GROUP=dba#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.OPER_GROUP=.*#oracle.install.db.OPER_GROUP=oinstall#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.type=.*#oracle.install.db.config.starterdb.type=GENERAL_PURPOSE#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.globalDBName=.*#oracle.install.db.config.starterdb.globalDBName=ora11g#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.SID=.*#oracle.install.db.config.starterdb.SID=ora11g#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.characterSet=.*#oracle.install.db.config.starterdb.characterSet=AL32UTF8#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.memoryOption=.*#oracle.install.db.config.starterdb.memoryOption=true#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.memoryLimit=.*#oracle.install.db.config.starterdb.memoryLimit=1500#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.password.ALL=.*#oracle.install.db.config.starterdb.password.ALL=oracle#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.storageType=.*#oracle.install.db.config.starterdb.storageType=FILE_SYSTEM_STORAGE#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.dbcontrol.enableEmailNotification=.*#oracle.install.db.config.starterdb.dbcontrol.enableEmailNotification=false#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#oracle.install.db.config.starterdb.automatedBackup.enable=.*#oracle.install.db.config.starterdb.automatedBackup.enable=false#g"  $ora_rsp >/dev/null 2>&1
 sed -i "s#DECLINE_SECURITY_UPDATES=.*#DECLINE_SECURITY_UPDATES=true#g"  $ora_rsp >/dev/null 2>&1
 echo "silent_check,is ok"
}

#oracle_ins,
function oracle_ins(){
echo "$(date "+%Y-%m-%d"): begining install oracle"
trap rollback INT TERM EXIT
if [ -e $ora_z1 ] ;then
   echo "$(date "+%Y-%m-%d"): found $ora_z1 "
else
 echo "$(date "+%Y-%m-%d"): download $ora_z1 "
{ curl -s ftp://$FTP/oracle/p10404530_112030_Linux-x86-64_1of7.zip -o $ora_z1;wait; } || { echo "$(date "+%Y-%m-%d"): download $ora_zip1 failed"; exit; }
{ unzip -o  -d  $ora_DIR $ora_z1 >/dev/null 2>&1;wait; } || { echo "$(date "+%Y-%m-%d"): unzip $ora_z1  failed"; exit; }
fi

if [ -e $ora_z2 ] ;then
   echo "$(date "+%Y-%m-%d"): found $ora_z2 "
else
 echo "$(date "+%Y-%m-%d"): download $ora_z2 "
{ curl -s ftp://$FTP/oracle/p10404530_112030_Linux-x86-64_1of7.zip -o $ora_z2;wait; } || { echo "$(date "+%Y-%m-%d"): download $ora_zip2 failed"; exit; }
{ unzip -o -d $ora_DIR $ora_z2 >/dev/null 2>&1;wait; } || { echo "$(date "+%Y-%m-%d"): unzip $ora_z2  failed"; exit; }
fi

silent_check
cd  $ora_DIR >/dev/null 2>&1
#{"./database/runInstaller -silent -force -responseFile  $ora_DIR/database/response/db_install.rsp > $ora_DIR/oinstall.log" >/dev/null 2>&1;wait;} || { echo "install oracle failed"; exit; }
{ bash -c "./database/runInstaller -silent -force -responseFile  $ora_DIR/database/response/db_install.rsp > $ora_DIR/oinstall.log">/dev/null 2>&1;wait; } || { echo "install weblogic failed"; exit; }
wait
sleep 1m
trap - INT TERM EXIT
grep "Successfully Setup Software." $ora_DIR/oinstall.log
echo "oracle_ins  is ok"
}

#clean_all
function clean_all(){
rm -rf $ora_DIR/database
#rm -rf $ora_DIR/oinstall.log >/dev/null 2>&1
rm -rf $ora_DIR/checkpoints >/dev/null 2>&1
rm -rf $ora_DIR/diag >/dev/null 2>&1
rm -rf $ora_DIR/oraInventory  >/dev/null 2>&1

echo "clean_all"
}

#rollback!
function rollback() {
if [ -e $ora_HOME ]; then
rm -rf $ora_HOME/.*
echo  "rollback!"
fi
exit
}

sys_check
user_check
evn_check
oracle_ins
clean_all