#!/bin/sh

## centos2cl INFO
# Short Description	:upgrades CentOS or RHEL distro to CloudLinux
# Description      	:removes yum repo configuration related to CentOS/RHEL,
#	  		as well as release related packages. Installs cloudlinux 		
#		        release related packages, new kernel and lve tools
## centos2cl INFO


function check_kernel_update_permission {
    if [ -e /etc/yum.conf ] && [ -n "$(grep exclude /etc/yum.conf | grep kernel)" ]; then
	echo Kernel update is prohibited on your system. 
	echo Remove "exclude kernel" option from /etc/yum.conf
	echo and run this script again.
	exit 1;
    fi;

}

function check_yum {
    if [ ! -n "$(cat /etc/yum.conf | sed -e 's/ //g' | grep -i ^"plugins=1")" ]; then
	echo "Yum error: Plugins are disabled"
	echo "Please enable yum-plugins: add string \"plugins=1\" to the /etc/yum.conf"
	exit 1;
    fi;
}

function check_release {
    if [ "$(rpm -q --qf %{version} `rpm -q --whatprovides redhat-release`)" != 5 ]; then
	echo "This script is for Version 5 only"
	exit 1;
    fi;
}

function backup {
    BACKUP=/etc/cl-convert-saved
    mkdir -p $BACKUP    
    cp /etc/redhat-release $BACKUP
#disable redhat
    mv /etc/yum.repos.d/RedHat-Base.repo $BACKUP 2> /dev/null #test if that is the rate path
    rpm -e --nodeps redhat-release-notes redhat-release yum-rhn-plugin redhat-logos 2> /dev/null

#disable centos
    mv /etc/yum.repos.d/CentOS-Media.repo $BACKUP 2> /dev/null
    mv /etc/yum.repos.d/CentOS-Base.repo $BACKUP 2> /dev/null
    rpm -e --nodeps centos-release centos-release-notes redhat-logos 2> /dev/null ;
}

function prep {
    rpm -Uvh http://repo.cloudlinux.com/cloudlinux/migrate/$ARCH/CloudLinux/cloudlinux-release-5-6.noarch.rpm \
    http://repo.cloudlinux.com/cloudlinux/migrate/x86_64/CloudLinux/cloudlinux-logos-0.3-1.el5.1.noarch.rpm --force ;
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
    if [ $? -ne 0 ] 
      then
       echo "Installation of cloudlinux-release failed, restoring"
       rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
       rpm -ivh http://mirror.centos.org/centos/5/os/${ARCH}/CentOS/centos-release-5-6.el5.centos.1.${ARCH}.rpm --force --nodeps
       yum -y install centos-release-notes
       exit 1 
    fi
    echo "Success"
cat > /etc/yum.repos.d/cloudlinux.repo << EOF
[cloudlinux-base-h2e]
name=CloudLinux-$releasever - Base - h2e
baseurl=http://repo.cloudlinux.com/cloudlinux/5e/os/x86_64/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CloudLinux
EOF
}


check_release
check_yum

ARCH=$(uname -i)
NO_ARGS=0

if [ $# -eq "$NO_ARGS" ]   # check for no arguments
then
	yum clean all
	check_kernel_update_permission
	backup
	prep
	echo -n "Registering the activation key... "
	if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	echo "Installing kernel..."
	if [ -n "$(uname -r | grep PAE)" ]; then
		yum -y install kernel-PAE lve lve-utils liblve
		exit 0;
	fi
	if [ -n "$(uname -r | grep ent)" ]; then
		yum -y install kernel-ent lve lve-utils liblve
		exit 0;
	fi
	echo "Installing lve..."
	yum -y install lve lve-utils liblve
  exit 0
fi



while getopts ":ck" opt; do
case "$opt" in

    k)
	yum clean all
	check_kernel_update_permission
	backup
	prep
	echo -n "Registering the activation key... "
	if [ $? -ne 0 ]; then echo "Failed"; exit 1; fi; echo "Success"
	echo "Installing kernel..."
	if [ -n "$(uname -r | grep PAE)" ]; then
		yum -y install kernel-PAE lve lve-utils liblve
		exit 0;
	fi
	if [ -n "$(uname -r | grep ent)" ]; then
		yum -y install kernel-ent lve lve-utils liblve
		exit 0;
	fi
	echo "Installing lve..."
	yum -y install lve lve-utils liblve
	exit 0;;

    c)
	rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
	rpm -ivh http://mirror.centos.org/centos/5/os/${ARCH}/CentOS/centos-release-5-6.el5.centos.1.${ARCH}.rpm --force --nodeps
	yum install centos-release-notes
	yum -y erase cloudlinux-release lve liblve lve-utils mod_hostinglimits mod_sucgid
	yum -y erase rhnlib > /dev/null 2>&1
	echo "You converted back to CentOS"
	echo "Now is the time to install kernel."
	echo -e "To delete CloudLinux kernel do \nrpm -e $(rpm -qa | grep ^kernel | grep lve | tr -s '\n' ' ')"
	echo "To install new centos kernel once you deleted cloudlinux kernel, type yum install kernel"
	exit 0;;

    \?)
	echo "Invalid option: -$OPTARG" >&2
	exit 1;;

    :)
	echo "Option -$OPTARG requires activation key"
	echo "For more info see http://www.cloudlinux.com/docs/cln_register.php"
	exit 1;;
   esac
done
