#!/bin/bash
# rhel511-to-cent511.sh
# This script converts a RHEL 5.11 install into CentOS 5.11
#   Copyright (C) 2016  Jun Go

    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.

    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.

    # You should have received a copy of the GNU General Public License
    # along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Jun Go gojun077@gmail.com
# Last Updated 2016-08-04
# This script should be executed as root.

# USAGE:
# ./rhel511-to-cent511.sh [mountpt for CentOS 5.11 iso]
# ex: ./rhel511-to-cent511.sh /media
# no trailing slash in the path!

if [ -z "$1" ]; then
  echo "You must specify the path to the CentOS 5.11 iso mountpoint"
  exit 1
fi

yum clean all

if grep --silent "Red Hat" /etc/redhat-release; then
  cp /etc/redhat-release /etc/redhat-release.old
fi

DELETE=(redhat-release
        yum-rhn-plugin
        redhat-logos
        rhn-client-tools
        rhn-setup
        rhn-check
        rhnsd
        rhel-instnum
       )

for i in ${DELETE[*]}; do
  if rpm -q "$i" > /dev/null; then
    echo -e "### Now deleting $i ###\n"
    rpm -e --nodeps "$i"
  fi
done


INSTALL=(centos-release-notes-5.11-0.x86_64.rpm
         centos-release-5-11.el5.centos.x86_64.rpm
         redhat-logos-4.9.99-11.el5.centos.noarch.rpm
         yum-fastestmirror-1.1.16-21.el5.centos.noarch.rpm
         yum-3.2.22-40.el5.centos.noarch.rpm
         )


if grep --silent "CentOS-5.11" "$1/RELEASE-NOTES-en"; then
  cd "$1"/CentOS || exit 1
  for j in ${INSTALL[*]}; do
    echo -e "### Now installing $j ###\n"
    rpm -Uvh --force "$j"
  done
else
  echo -e "Please mount the proper ISO file\n" 1>&2
  exit 1
fi

sed -i "s:Red Hat Enterprise Linux Server:CentOS release 5.11 (Final):g" /boot/grub/grub.conf

# Rename all the repos under /etc/yum.repos.d/ so that only
# the local repo is used
echo -e "### Rename CentOS Repo files ###"
find /etc/yum.repos.d/ -type f -name "CentOS*" -exec mv {} {}.old \;

# check for local.repo file and create it if it doesn't
# exist

if [ -f /etc/yum.repos.d/local.repo ]; then
  echo -e "### Local yum repo file exists ###\n"
  echo -e "### Changing baseurl for local.repo (to CentOS DVD iso) ###\n"
  # Check if the existing local.repo uses mountpoint '/media/Server'
  if grep --silent "/media/Server" /etc/yum.repos.d/local.repo; then
    # change file path to CentOS path
    sed -i "s%file:///media/Server%file://$1%g" \
        /etc/yum.repos.d/local.repo
  fi
else
  cat << XYZ > /etc/yum.repos.d/local.repo
[CentOS]
name=CentOS
baseurl=file://\$1
enabled=1
gpgkey=file://\$1/RPM-GPG-KEY-CentOS-5

XYZ
fi

yum clean all
yum update -y