#!/bin/bash
# migrate from normal channel to hybrid channel
#

log=/var/log/normal-to-hybrid.log
ENABLE_BETA_REPO="${ENABLE_BETA_REPO:-0}"

if [ "$(uname -m)" != "x86_64" ]; then
    echo "Hybrid channel is available only for x86_64 machines, cannot switch to the hybrid channel"
    exit 1
fi

yum list installed kmod-*el7h* &> /dev/null
kmod=$?
yum list installed kernel-*el7h* &> /dev/null
kern=$?
yum list installed kmod-lve &> /dev/null
kmodlve=$?

if [ $kmod -eq 0 -a $kern -eq 0 -a $kmodlve -eq 0 ]; then
    echo "CloudLinux 7 Hybrid has been installed already"
    exit 1
fi

#
# CL7 -> CL7h
#

echo "[cl7h]
name=CloudLinux 7h stable repo
baseurl=https://www.repo.cloudlinux.com/cloudlinux/7h/updates/x86_64/
enabled=1" > /etc/yum.repos.d/cl7h.repo | tee -a $log

echo "[cl7h_beta]
name=CloudLinux 7h beta repo
baseurl=https://www.repo.cloudlinux.com/cloudlinux/7h/updates-testing/x86_64/
enabled=${ENABLE_BETA_REPO}" > /etc/yum.repos.d/cl7h_beta.repo | tee -a $log

yum clean all 2>&1 | tee -a $log

echo "Installing CloudLinux 7 hybrid" | tee -a $log

if [ $kmod -eq 1 ]; then
	yum -y update kmod 2>&1 | tee -a $log
fi

if [ $kern -eq 1 ]; then
	echo "Save current kernel version" | tee -a $log
	uname -r  | sed 's/.x86_64$//' > /etc/sysconfig/kernel-version.pre-hybrid

	yum -y install kernel kmod-lve 2>&1 | tee -a $log
fi

if [ $kmodlve -eq 1 ]; then
	yum -y install kmod-lve 2>&1 | tee -a $log
fi
