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

source /etc/normal-hybrid.conf

log=/var/log/normal-to-hybrid.log

# Run a yum command with retries. On any non-zero exit we 'yum clean metadata'
# (to drop a possibly half-written / mirror-corrupted repodata cache) and try
# again. After ${attempts} failed attempts we return the last yum exit code so
# the caller can abort instead of silently leaving the system half-converted.
# Output goes to both stdout and ${log}; ${PIPESTATUS[0]} is preserved.
yum_with_retries() {
    local attempts=3
    local delay=15
    local rc=0
    local i
    for ((i = 1; i <= attempts; i++)); do
        "$@" 2>&1 | tee -a "$log"
        rc=${PIPESTATUS[0]}
        if [ "$rc" -eq 0 ]; then
            return 0
        fi
        if [ "$i" -lt "$attempts" ]; then
            echo "yum failed (rc=$rc), attempt $i/$attempts; clearing metadata and retrying in ${delay}s" | tee -a "$log"
            yum clean metadata 2>&1 | tee -a "$log" || true
            sleep "$delay"
        fi
    done
    echo "ERROR: yum failed after $attempts attempts: $*" | tee -a "$log"
    return "$rc"
}
ENABLE_BETA_REPO="${ENABLE_BETA_REPO:-0}"
HYB_REPO_FILE_BASE="$(basename ${HYB_REPO_FILE})"
HYB_REPO_BETA_FILE_BASE="$(basename ${HYB_REPO_BETA_FILE})"
if [[ $HYB_REPO_FILE_BASE == *"*"* ]] || [[ $HYB_REPO_FILE_BASE == *"?"* ]] || [[ $HYB_REPO_FILE_BASE == *"["* ]] || [[ $HYB_REPO_FILE_BASE == *"]"* ]] ; then
        echo "Error: HYB_REPO_FILE contains wildcard characters"
	exit 1
fi
if [[ $HYB_REPO_BETA_FILE_BASE == *"*"* ]] || [[ $HYB_REPO_BETA_FILE_BASE == *"?"* ]] || [[ $HYB_REPO_BETA_FILE_BASE == *"["* ]] || [[ $HYB_REPO_BETA_FILE_BASE == *"]"* ]] ; then
        echo "Error: HYB_REPO_BETA_FILE contains wildcard characters"
	exit 1
fi

HYB_REPO_FILE_PATH="/etc/yum.repos.d/${HYB_REPO_FILE_BASE}"
HYB_REPO_BETA_FILE_PATH="/etc/yum.repos.d/${HYB_REPO_BETA_FILE_BASE}"

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-*el${VERSION_NUM}h* &> /dev/null
kmod=$?
yum list installed kernel-*el${VERSION_NUM}h* &> /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 ${VERSION_NUM} Hybrid has been installed already"
    exit 1
fi

#
# CLX -> CLXh
#

echo "[cl${VERSION_NUM}h]
name=CloudLinux ${VERSION_NUM}h stable repo
baseurl=${CLXH_REPO}
enabled=1" > ${HYB_REPO_FILE_PATH} | tee -a $log

echo "[cl${VERSION_NUM}h_beta]
name=CloudLinux ${VERSION_NUM}h beta repo
baseurl=${CLXH_BETA_REPO}
enabled=${ENABLE_BETA_REPO}" > ${HYB_REPO_BETA_FILE_PATH} | tee -a $log

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

echo "Installing CloudLinux ${VERSION_NUM} hybrid" | tee -a $log

if [ ${VERSION_NUM} -eq 7 -a $kmod -eq 1 ]; then
	yum_with_retries yum -y update kmod || exit $?
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_with_retries yum -y install kernel kmod-lve || exit $?
fi

if [ $kmodlve -eq 1 ]; then
	yum_with_retries yum -y install kmod-lve || exit $?
fi
if [ ${VERSION_NUM} -eq 7 ]; then
  yum_with_retries yum -y install dracut-squash || exit $?
fi
