#  13:21:18  28 Jun 2019 * tim
# Copyright 20 Nov 2014, Rocket Software, Inc. All rights reserved.
#
# tim
#
#
#
#
#
#
# [01] 11/20/14 tim - Created
# [02] 07/21/15 tim   -62971 Fix echo -n to preserve spaces
# [03] 08/30/16 DM    -55695 Fix spelling errors
# [04] 08/09/18 tim   -66226 Fixed improper parseing
# [05] 06/28/19 tim   -66929 Remove consoletype test
# [06] 11/19/19 ltim   tomate Make the install of yum d3depends silent too.
#
#
#!/bin/bash

# DoEcho - A generic version of echo
# ----------------------------------
DoEcho()
{
	if [ "$1" = "-n" ]; then
 echo -n "$2"
	else
		echo "$1"
	fi
}


UpdateShmMax()
{
  SHMMAX=1073741824
  TMPMAX=`cat /proc/sys/kernel/shmmax`
  CURRSHMMAX=`expr $TMPMAX / $SHMMAX`
  SHMMAX=`expr $SHMMAX \* 10`

  if [ $CURRSHMMAX -lt 10 ]; then
	echo "" >> /etc/sysctl.conf
 echo "# New maximum shared segment size for D3" >> /etc/sysctl.conf
	echo "kernel.shmmax = $SHMMAX" >> /etc/sysctl.conf
	sysctl -q kernel.shmmax=$SHMMAX
  fi

}


# GetVer - Convert /etc/redhat-release to version numbers
# -------------------------------------------------------
GetVer()
{
	MAJ=$1
	MIN=$2

	FILE="/etc/redhat-release"
	STR=$(cat ${FILE})

	grep "pdate" ${FILE} > /dev/null 2>&1
	res=$?

	TMP1=${STR##*release }
	MAJ=$(echo ${TMP1} | cut -d"." -f 1)

	if [ "$res" = "0" ]
	then
		TMP2=${STR##*pdate }
		MIN=$(echo ${TMP2} | cut -c 1)
	else
		MIN=$(echo ${TMP1} | cut -d"." -f 2 |cut -d" " -f 1)
	fi
	DISTRIB=$(echo ${STR} | \
		sed -e "s/\(.*\)[[:blank:]][Ll][Ii][Nn][Uu][Xx][[:blank:]].*/\1/g" \
		-e "s/[[:blank:]]//g")

	if [ "$DISTRIB" = "RedHatEnterprise" ]
	then
		DISTRIB="RedHatEnterpriseServer"
	fi
}

# Link the the d3zip and d3unzip to either gzip or compress/unxompress
# return 0 on success or 1 on failure.
# --------------------------------------------------------------------
LinkZips()
{           
	RetVal=0
	PickBase=/usr/lib/pick
	D3zip=${PickBase}/d3zip
	D3unzip=${PickBase}/d3unzip
	InsStat="/tmp/d3.install.stat"

	# Check for existing d3zip, if found leave them alone
	# ---------------------------------------------------
	if [ -x "${D3zip}" ]; then
		echo "${D3zip} exists, leaving ${D3zip} unmodified. " >> ${InsStat}
		return 1
	fi

	# Look for zip, if found set paths for gzip,gunzip else compress,uncompress
	# -------------------------------------------------------------------------
	Pzip=`which gzip`
	ret=$?
	if [ $ret -eq 0 ]; then
		# we'll use gzip for both compressing ans uncompressing
		Punzip=${Pzip}
	else
		# didn't find gzip using compress and uncompress
		Pzip=`which compress`
		Punzip=`which uncompress`
	fi

	# Link the zip command
	# --------------------
	echo "${D3zip} is being linked to ${Pzip} " >> ${InsStat}
	ln -s ${Pzip} ${D3zip}
	ret=$?
	if [ $ret -ne 0 ]; then
		echo "Unable to create link to ${Pzip}"
		RetVal=1
	fi

	# Link the unzip command
	# ----------------------
	echo "${D3unzip} is being linked to ${Punzip} " >> ${InsStat}
	ln -s ${Punzip} ${D3unzip}
	ret=$?
	if [ $ret -ne 0 ]; then
		echo "Unable to create link to ${Punzip}"
		RetVal=1
	fi

	return ${RetVal}
}

CheckVer()
{

	if [ ! -x "/usr/bin/uncompress" ]; then
		ln -s /usr/bin/gunzip  /usr/bin/uncompress
		ret=$?
		if [ $ret -ne 0 ]; then
			echo "ERROR:unable to link gunzip to uncompress"
			exit 4
		fi
	fi

	# Check for correct RedHat version
	# --------------------------------
	GetVer MAJ MIN
	skip=0

	if [ $MAJ -lt 6 ]
	then
		skip=1
	fi

	if [ "$MAJ" = "6" ]
	then
		if [ $MIN -lt 4 ]
		then
			skip=1
		fi
	fi


	if [ "$skip" = "1" ]
	then
	echo "D3 can not run on the installed version of Red Hat, please check the"
		echo "release notes for the correct version of Red Hat Enterprise Linux"
		exit
	fi

	DEPFILE=/usr/lib/pick/d3depends
	rpm -ql d3depends >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
		yum -y erase d3depends >/dev/null 2>&1
	fi
	if [ ! -f "${DEPFILE}" ]
	then
		echo "Checking for dependencies... This could take a little bit of time."
		# Check for required dependencies
		# -------------------------------
		YUMFILE="/etc/yum.repos.d/d3depends.repo"
		export YUM0=${DISTRIB}/${MAJ}.${MIN}
		echo "[d3depends]" > $YUMFILE
		echo "name=d3depends for RHEL/ CentOS \$releasever - \$basearch [\$YUM0]" >> $YUMFILE
		echo "baseurl=ftp://d3mv-repo:d3getRep0@ftp.rocketsoftware.com/$YUM0/\$basearch" >> $YUMFILE
		echo "enabled=1" >> $YUMFILE
		echo "gpgcheck=0" >> $YUMFILE
		echo "gpgkey=" >> $YUMFILE
		echo "protect=1" >> $YUMFILE

	USEDREPO="--disablerepo=\* --enablerepo d3depends"

		rpm -ql d3depends64 >/dev/null 2>&1
		if [ $? -eq 0 ]
		then
			REINS=reinstall
		else
			REINS=install
		fi
		SILENT=
		[ -r /tmp/D3INSTALLDA ] && SILENT=-y
		yum $SILENT $USEDREPO $REINS d3depends64

	# Check to see if we can use a distribution provided repository
	# -------------------------------------------------------------
	SUBMAN=`which subscription-manager >/dev/null 2>&1; echo $?`
	if [ $SUBMAN -eq 0 ]
	then
		# Subscription manager exists; see if we are entitled
		# ---------------------------------------------------
		ISRHELENT=`subscription-manager identity >/dev/null 2>&1;echo $?`
		if [ $ISRHELENT -eq 0 ]
		then
			USEDREPO=""
		fi
	fi
	if [ "${DISTRIB}" = "CentOS" ]
	then
		USEDREPO=""
	fi

		# Check for type of d3depends file
		# --------------------------------
		if [ -f "${DEPFILE}" ]; then
			DEPID=`sed -n 1,1p ${DEPFILE}|cut -d" " -f1`
			if [ $DEPID = d3depends ]; then

				# Load packages from list in d3depends file
				# -----------------------------------------
				for DEPPKG in `sed -n 2,2p ${DEPFILE}`
				do
					yum -y $USEDREPO install $DEPPKG
				done
			fi
		fi
		rm -rf $YUMFILE
	fi
}


GetPid()
{
#Find out if there are existing d3 or ap processes running before installing.
	ps auwx | grep -v " ap | d3 | pick " | awk '$0 ~ / ap | d3 | pick / {print}'
}

Initialize()
{
	SUFFIX=Z
	UNCOMPRESS="gunzip"
	CheckVer
  UpdateShmMax
}

Finish()
{
	LIBDIR=/usr/lib/pick
	mkdir $LIBDIR 2> /dev/null
	INSTAL_PICK_DIR=$LIBDIR
 export LANG=en_US #[22]
}
