#!/bin/bash

# echo "拷贝文件:"
# read -p "please input the server you share your os installation media and oracle software:" server
# mkdir /installyationMedia
# scp root@${server}:/iso/* /installationMedia
# if [ -e /installationMedia/*.iso ]{
#  echo "文件拷贝成功。"
# }else{
#  echo "操作系统文件拷贝不成功"
# }

echo "**********************Mount the iso******************************"
mount -o loop /tmp/rhel-server-6.0-x86_64-dvd.iso /media
cat >/etc/yum.repos.d/cd.repo<<EOF
[cd]
name=cd.repo
baseurl=file:///media/Server
enabled=1
gpgcheck=0
EOF
yum clean
yum update
if [[ $? -eq 0 ]]; then
 echo "Yum repo configure success!"
 else
 echo "Yum repo configure failure!"
fi

echo "**********************Verify the dependency packages**************************"
for i in `cat package.txt`; do
 rpm -qa | grep ${i}
  if [[ $? -eq 0 ]]; then
   echo "${i} have been installed !" | tee -a log.txt
  else
   yum -y install ${i}*
   if [[ $? -eq 0 ]]; then
    echo "${i} Install Success;" | tee -a log.txt
   else
    echo "${i} install failure" | tee -a log.txt
   fi
  fi
done

echo "**********************unzip the packages*****************************"
if [[ -d /installationMedias/database ]]; then
 echo "Database Installation File have been created!"
else
 for i in `ls -l /installationMedias/linux*.zip | awk '{print $9}'`; do
  unzip ${i} -d /installationMedias
 done
fi


echo "************************Environment Prepare*******************************"
if [[ -d /u01/app ]]; then
 echo "/u01/app Directory Have Been Created!"
else
 mkdir -p /u01/app
fi

if [[ -z `cat /etc/group | grep dba` ]]; then
 groupadd dba
else
 echo "dba group have been created" | tee log.txt
fi
if [[ -z `cat /etc/group | grep oinstall` ]]; then
 groupadd oinstall
else
 echo "dba group have been created" | tee log.txt
fi
if [[ -z `id oracle` ]]; then
 useradd -g oinstall -G dba oracle
else
 echo "user oracle have been created" | tee log.txt
fi
chown -R oracle:oinstall /u01
chmod -R 755 /u01

echo "**************************kernel parameter**********************************"
cat >>/etc/sysctl.conf<<EOF
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 536870912
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
EOF
/sbin/sysctl -p
echo "change User limts"
cat >>/etc/security/limits.conf<<EOF
oracle           soft    nproc   2047
oracle           hard    nproc   16384
oracle           soft    nofile  1024
oracle           hard    nofile  65536
EOF
cat >>/etc/pam.d/login<<EOF
session    required     pam_limits.so
EOF
cat >>/etc/profile<<EOF
if [ $USER = "oracle" ]; then
        if [ $SHELL = "/bin/ksh" ]; then
              ulimit -p 16384
              ulimit -n 65536
        else
              ulimit -u 16384 -n 65536
        fi
fi
EOF
cat >>/home/oracle/.bash_profile<<EOF
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH:$HOME/bin
EOF

su - oracle<<EOF
source ~/.bash_profile
/installationMedias/database/runInstaller
EOF