#!/bin/bash
# $Header: /home/kickstart/updates/CentOS7.5-64/RCS/yum_install,v 1.1 2018/08/09 17:21:07 root Exp $
# I created this script because yum script in this directory doesn't exit with "1" when 
# failed to find the package.
# -Hiro 2018-02-09
#
# 2018-05-21
# -r option added. This option will remove the package first and reinstall the package
#    This is useful when you need to make sure the particular version of the package
#    is installed even there is newer version of the package available in the repo.
#    Regular yum install package-version will not install or downgrade the package if the
#    newer version is already installed. 
#
#    This option also avoid the post uninstall script in rpm package is executed for the
#    yum update.
#    yum update will do:
#        1. Install the new rpm package 
#        2. Run post installation script of rpm
#        3. Uninstall the old version of rpm package
#        4. Post uninstallation script is executed
#    For some case, we don't want to run the post uninstallation script when the new
#    rpm is installed.
#
#    Downside of the "yum_install -r" instead of yum upgrade:
#        If some user is using the target file, during the yum_install, the file is temporally
#        removed before the new rpm is installed.

set -u

YUM=/usr/bin/yum
version=""
reinstall=false
full_pkgname=""

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

for full_pkgname; do true; done

if [ -z "$full_pkgname" ]
then
	echo Usage $0 \<package_name\>
	exit 1
fi


#Remove the version number for $pkgname
pkgname=$(echo ${full_pkgname} | sed 's/-[0-9]\+\([-\.].*$\|$\)//')
if [ "$full_pkgname" != "$pkgname" ]
then
	version=${full_pkgname#${pkgname}-}
fi

#Commented out, some of the package doesn't have el6 in the version
#Add .el6 if it's not included. .el6 is must be a part of the version
#if ((${#} > 0)) && [ -n "$version" ]
#then
#	dist_str=$( rpm -E %{?dist} | sed 's/\./\\./g' )
#	if [[ ! "$full_pkgname" =~ ${dist_str}(\.|$) ]]
#	then
#		full_pkgname="$full_pkgname$( rpm -E %{?dist})"
#	fi
#	set -- ${@:1:$(expr ${#} - 1)} "${full_pkgname}"
#fi

#echo $full_pkgname
#echo ${@:+"$@"}

while true;
do
	case ${1:+${1}} in
	"-r")
		reinstall=true
		;;
	*)
		break
		;;
	esac
	shift
done


if $reinstall && rpm -qi "$pkgname" > /dev/null
then
        #echo ${YUM} -y remove $pkgname
        ${YUM} -y remove $pkgname
fi

#echo version: ${version} other options: ${@:+"$@"}
if ! ${YUM} info ${full_pkgname} 2>&1 /dev/null
then
	${script_dir}/delete-svcunix-yum-cache

	if ! MSG=$(${YUM} info ${full_pkgname} 2>&1)
	then
		echo "Cannot find the package ${full_pkgname}" >&2
		echo "${MSG}"
		exit 1
	fi
fi

if ! MSG=$(${YUM} install -y ${@:+"$@"} 2>&1 ); then
        case "${MSG}" in
        *"No package"*"available."*)
                EXITCODE=1
                ;;
        *"Nothing to do"*)
                ;;
        *)
                EXITCODE=1
                ;;
        esac
else
	EXITCODE=0
fi

echo "${MSG}"
exit ${EXITCODE}

