#!/bin/bash
#Rasanegar Smart Soloutions @ rasanegar.com
#Contact us via phone if you have any question about our repositories or this script
#Our Reposes Get Data Directly from UpStream Server without any file modifications 
#we do our best to Keep Our mirros Update Repository Service and Script Provided AS IS - use at your own risk

ORANGE='\033[01;31m'
COLOROFF='\033[0m' 
Purple='\033[0;35m'
repoArray=()
isCentos=false

echo "Welcome to CentOS Mirror switch script By Rasanegar.com"
echo "This script will First BackUp Your current mirror setting and then changes .repo files and switch mirror to local IRANIAN mirros with higher Speed and Availability "
read -p "Are you sure? " -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then

if [   $(rpm -qa \*-release | grep -Ei "centos" | cut -d"-" -f1) == "centos" ]
    then
    CentOSVersion=$(rpm -qa \*-release | grep -Ei "centos" | cut -d"-" -f3 | cut -d"." -f1)
    haveBackup="/etc/yum.repos.d/OriginalRepos/"
    if [ -d "$haveBackup" ]; then
            echo -e "$COLOROFF -----------------------------------"
            echo -e "$Purple It seems you have already switched to Rasanegar $ORANGE CentOS $Purple mirror ,  check $haveBackup Directory For Current Backups $COLOROFF"
            echo -e "$Purple no change have been made . Exiting Script......"
            echo -e "$COLOROFF -----------------------------------"
    else 
        if [ $CentOSVersion -eq 8 ]
        then
            repoArray=(
                        "CentOS-Base.repo"
                        "CentOS-AppStream.repo"
                        "CentOS-centosplus.repo"
                        "CentOS-Extras.repo"
                        "CentOS-CR.repo"
                        "CentOS-PowerTools.repo"
            )
        else
            repoArray=( 
                "CentOS-Base.repo"
             )
        fi

    mkdir /etc/yum.repos.d/OriginalRepos/

    for i in "${repoArray[@]}"
        do
            : 
            name=$(echo $i | cut -d"-" -f2 | cut -d"." -f1 )
            now=$(date +"%m-%b-%Y--%H-%M-%S")            
            backupName="$i.orginal-$now"
            cp -f /etc/yum.repos.d/$i /etc/yum.repos.d/OriginalRepos/$backupName
            sed  -i 's/mirror.centos.org/mirror.rasanegar.com/g' /etc/yum.repos.d/$i 
            sed  -i 's/mirrorlist=/#mirrorlist=/g' /etc/yum.repos.d/$i 
            sed  -i 's/#baseurl=/baseurl=/g' /etc/yum.repos.d/$i 
            sed  -i "s/\[$name\]/\[$name-hosted-by-Rasanegar-IRAN\]/g" /etc/yum.repos.d/$i 
            sed  -i "s/\[BaseOS\]/\[BaseOS-hosted-by-Rasanegar-IRAN\]/g" /etc/yum.repos.d/$i

            echo -e "$ORANGE $i Switched to Rasanegar IRANIAN Mirror Successfully $COLOROFF"
            
        done               
    echo -e "-----------------------------------------------------"      
    echo -e "$Purple Cleaning YUM repositores cache..... $COLOROFF"
    yum clean all
    echo -e "$Purple Iranian SuperSast Official CentOS Mirror sponsored by Rasanegar.com"
    echo -e "$COLOROFF Enjoy !!"
    fi
else
    echo -e "$Purple  Please make Sure Running this Script on $ORANGE CentOS $Purple Only ? failed to determine OS as CentOS  $COLOROFF "
fi
# end of prompt IFS
fi