#!/bin/sh

_fail() {
	echo "Upgrade process has failed!  Please contact support"
}

YUM="yum -y"
DNF="dnf -y"
CENTOS_RPM_SERVER="http://mirror.centos.org/centos/8/BaseOS/x86_64/os/Packages"
CENTOS_VERSION="8.2-2.2004.0.1"

if [ "$(rpm -q kernel | wc -l)" = "1" ]; then
	echo "Only one kernel found, upgrading current system"
	${YUM} upgrade
	echo "Reboot your system and run this script again"
	exit 0
fi

echo "This script will upgrade your TNSR instance to a new major"
echo "version based on CentOS 8.  This operation is irreversible."
echo "Make sure you backup your configuration files before start."
echo ""
read -p "Are you ready to proceed? (y/N) " -e -n 1 _proceed

if [ "${_proceed}" != "y" -a "${_proceed}" != "Y" ]; then
	echo "Aborting..."
	exit 0
fi

# Install items that are part of CentOS 8 Core group so they will be upgraded
# at distro-sync step
${YUM} install epel-release dnf tnsr-release NetworkManager \
	NetworkManager-team NetworkManager-tui dnf-plugins-core \
	rng-tools sssd-common sssd-kcm libibverbs

systemctl disable NetworkManager >/dev/null 2>&1 || true

# Adjust dnf.conf
if ! grep -q 'clean_requirements_on_remove=False' /etc/dnf/dnf.conf; then
	sed -i -e '/clean_requirements_on_remove/d' /etc/dnf/dnf.conf
	echo 'clean_requirements_on_remove=False' >> /etc/dnf/dnf.conf
fi

# At this point tnsr/tnsr-release packages will be removed due to yum-utils
# dependency
rpm -e --nodeps yum-utils
${DNF} remove yum yum-metadata-parser '*rpmconf*' redhat-rpm-config

mv -f /etc/yum /etc/yum.backup_tnsr

# Remove unnecessary kernels
TO_BE_REMOVED=$(dnf repoquery --installonly --latest-limit=-2 -q)
if [ -n "${TO_BE_REMOVED}" ]; then
	${DNF} remove ${TO_BE_REMOVED}
fi

trap '' HUP INT TERM
trap _fail EXIT

set -e

${DNF} --disablerepo=* install \
	${CENTOS_RPM_SERVER}/centos-repos-${CENTOS_VERSION}.el8.x86_64.rpm \
	${CENTOS_RPM_SERVER}/centos-release-${CENTOS_VERSION}.el8.x86_64.rpm \
	${CENTOS_RPM_SERVER}/centos-gpg-keys-${CENTOS_VERSION}.el8.noarch.rpm

${DNF} --disablerepo=* upgrade \
	https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

# Cleanup all local files
dnf -y clean all

# Remove CentOS 7 kernel used to boot
rpm -e --nodeps $(rpm -q kernel-$(uname -r))

# Remove conflicting packages
rpm -e --nodeps sysvinit-tools

${DNF} --releasever=8 makecache

# Enable nginx 1.16 module
${DNF} --releasever=8 module enable nginx:1.16

# Cache all packages that will be used to upgrade
${DNF} --releasever=8 --allowerasing --setopt=deltarpm=false --best \
	--downloadonly distro-sync
${DNF} --releasever=8 --allowerasing --setopt=deltarpm=false --best \
	--downloadonly groupinstall Core
${DNF} --releasever=8 --allowerasing --setopt=deltarpm=false --best \
	--downloadonly install tnsr-release libibverbs

# Cleanup old dpdk stuff
rpm -e --nodeps dpdk
rm -rf /var/lib/dkms/dpdk

# Upgrade CentOS
${DNF} --releasever=8 --allowerasing --setopt=deltarpm=false --best \
	-C distro-sync

# Enable legacy network service
systemctl disable NetworkManager >/dev/null 2>&1 || true
systemctl enable network

trap "-" HUP INT TERM EXIT

echo "Upgrade is complete!  Reboot your system!"
