#!/bin/bash
##########################################################
#
#  Copyright (c) 2006, Oracle. All rights reserved
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Name: oracle-em-agent-12cR1-preinstall
# Description: A script to verify and set requirement to install Oracle 
# Enterprise Manager 12cR1 on OL6.
#

PWD=`/usr/bin/readlink $0`;
PWD=`/usr/bin/dirname ${PWD:-$0}`;
BASE_DIR=`cd ${PWD} && /bin/pwd`;
NAME="oracle-em-agent-12cR1-preinstall"
TEST_LOG_LOC=/var/log/${NAME}/
PRE_PARAM_LOG=${TEST_LOG_LOC}/emagent-preinstall.log;
BACKUP_DIR=${TEST_LOG_LOC}/backup/`date "+%b-%d-%Y-%H-%M-%S"`;
SUDOFILE="/etc/sysconfig/${NAME}/oracle-em-agent-12cR1-preinstall-sudo-conf";

USERID="54321"
GROUPID="54321"
RESULT=0

#Add oracle user. If present ,do nothing but create the mandatory groups
f_createuser ()
{
  groupnum=`cat /etc/group | sort -t: -g +2 -3 | grep -v nfsnobody | cut -f3 -d":" | tail -1`
  if [ "${groupnum}" -ge "${GROUPID}" ]; then
    GROUPID=`expr $groupnum + 1`
  fi
  usernum=`cat /etc/passwd | sort -t: -g +2 -3 | grep -v nfsnobody | cut -f3 -d":" | tail -1`
  if [ "${usernum}" -ge "${USERID}" ]; then
    USERID=`expr $usernum + 1`
  fi
  echo "Adding group oinstall with gid ${GROUPID}" >> ${PRE_PARAM_LOG}
  groupadd -g ${GROUPID} oinstall  >> ${PRE_PARAM_LOG} 2>&1
  echo "Adding group dba" >> ${PRE_PARAM_LOG}
  groupadd dba  >> ${PRE_PARAM_LOG} 2>&1
  grep "^oracle:" /etc/passwd   > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    # Add the user - password is disabled for oracle user.
    echo "Adding user oracle with user id ${USERID}, initial login group oinstall, supplementary group dba and  home directory /home/oracle" >> ${PRE_PARAM_LOG}
    /usr/sbin/useradd -u ${USERID} -g oinstall -G dba -d /home/oracle oracle  >> ${PRE_PARAM_LOG} 2>&1
    if [ "$?" -eq "0" ]; then
      echo "Changing ownership of /home/oracle to oracle:oinstall" >> ${PRE_PARAM_LOG}
      /bin/chown oracle:oinstall /home/oracle  >> ${PRE_PARAM_LOG} 2>&1
    else		
      echo "Error: Failed to create oracle user" >> ${PRE_PARAM_LOG}
      return 1;
    fi
    echo "Please set password for oracle user" >> ${PRE_PARAM_LOG}
  else
    echo "User oracle is already present" >> ${PRE_PARAM_LOG}
  fi
  oracle_info=`id oracle`
  echo "${oracle_info}" >> ${PRE_PARAM_LOG}
  return 0;
}

f_sudo ()
{
  if [ -f ${SUDOFILE} ]; then
    cp -f ${SUDOFILE} /etc/sudoers.d/
    if [ "$?" -eq "0" ]; then	
      echo "Copied ${SUDOFILE} to /etc/sudoers.d" | tee -a ${PRE_PARAM_LOG}
    else
      echo "Error: Failed to copy ${SUDOFILE} to /etc/sudoers.d" | tee -a ${PRE_PARAM_LOG}
      return 1;
    fi
  else
    echo "Error: ${SUDOFILE} not found" | tee -a ${PRE_PARAM_LOG}	
    return 1;	
  fi
  return 0;
	
}	
	

#This function is invoked during rpm uninstall. Roll back all configuration changes here
f_uninstall ()
{
  return 0;
}

export PATH=$PATH:/usr/sbin:/sbin:/usr/bin
USER=`/usr/bin/id -un -n`
if [ "x${USER}" != "xroot" ]; then
  echo ""
  echo "Error: This script needs to be executed as root user"
  echo "Exiting.."
  echo ""
  exit 1;
fi

if [ $# -ge 1 ]; then
  while getopts ":us" options; do
    case $options in
      u ) f_uninstall ; exit $? ;;
      s ) f_sudo ; exit $? ;;	
      h ) echo " Usage: oracle-em-agent-12cR1-preinstall [-s]" ; exit 1 ;;
      * ) echo " Usage: oracle-em-agent-12cR1-preinstall [-s]" ; exit 1 ;;
    esac
  done
fi

mkdir --mode 0700 -p ${TEST_LOG_LOC};
test -d ${TEST_LOG_LOC} && rm -f ${TEST_LOG_LOC}/* 2> /dev/null;
mkdir --mode 0700 -p ${BACKUP_DIR};
f_createuser;
if [ $? -ne 0 ]; then
  echo "Creating oracle user failed " >> ${PRE_PARAM_LOG}
  echo "" >> ${PRE_PARAM_LOG}
else
  echo "Creating oracle user passed " >> ${PRE_PARAM_LOG}
  echo "" >> ${PRE_PARAM_LOG}
fi;

cp ${PRE_PARAM_LOG} ${BACKUP_DIR}
exit 0
