#!/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
# Ver 1.1 
# Date : 2020/03/14
###########################################################################
# Known Issue : 
#		Exit on Anweres other that Y 
# Functionalities to be added :
#	 	Select host OS type and update its repo if we have repo for that OS
#		More interactive and become more user friendly
#		
########################################################################### 



ORANGE='\033[01;31m'
NC='\033[0m' 
Purple='\033[0;35m'
LGreen='\033[0;92m'
LCyan='\033[0;96m'
LRed='\033[0;91m'
repoArray=()
isCentos=false


# Get OS Installation Info for proper modifications
#OS=`uname`;
OS=$(awk -F= '$1=="ID" { print $2 ;}' /etc/os-release)
OS_VER=$(awk -F= '$1=="VERSION_ID" { print $2 ;}' /etc/os-release)
#OS_VER=;
#REDHAT_RELEASE=/etc/redhat-release
#DEBIAN_VERSION=/etc/debian_version
yesno=n;

cr=`echo $'\n.'`
cr=${cr%.}
clear
echo -e "$NC"
echo ""
echo -e "$LGreen Welcome to Linux mirror switch script By $Purple Rasanegar.com $NC"
echo ""
echo -e "$LCyan This script will First Backup Your current repository files  \n and then changes repository list and configuration file for switch their URL's \n to our local IRANIAN mirros with higher Speed and Availability $NC "
echo ""
read -p "Are you sure you want to switch repositories address to Rasanegar Repositories hosted in IRAN? (press y/Y to accept) $cr" -n 1 -r
echo    # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo -e "$NC"
	echo -e "$LRed our OpenSource Project mirros are offered as our community responsibility \n	it comes with AS-IS Policy \n We do our best to keep our servers and repositories updated \n and out mirror severs network availibility at 99.9% \n  but there is no waranty \n $NC "
	echo -e "You are Using  $OS $OS_VER"
	read -p "Do you agree with these conditions &  Rasanegar TOS ? ((press y/Y) to accept) $cr" -n 1 -r
	echo  ""
	if [[ $REPLY =~ ^[Yy]$ ]]
	then
		if [  OS == "centos" ]
			then
			CentOSVersion=$(rpm -qa \*-release | grep -Ei "centos" | cut -d"-" -f3 | cut -d"." -f1)
				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
			BackupDirectory="/etc/yum.repos.d/OriginalRepos/"
			Files=""
			if [ -d "$BackupDirectory" ] && [ "$(ls -A $BackupDirectory)" ]; then
					echo -e "$NC -----------------------------------"
					echo -e "$LGreen It seems you had switched to Rasanegar $ORANGE CentOS $LGreen mirrors before\n check $LRed $BackupDirectory $LGreen Directory For OLD Backup files $NC"
					echo -e "$NC -----------------------------------"
					echo -e "$NC no changes have been made \n Exiting modification Script \n $LGreen Have a Good time !"
					echo -e "$NC -----------------------------------"
			else 
				
			if [ ! -d "$BackupDirectory" ]
			then
			mkdir /etc/yum.repos.d/OriginalRepos/
			fi
			echo -e "you are running script on $LGreen CentOS $CentOSVersion $NC \n"
			echo -e "following files will be modified $Purple\n"
			
			printf '%s\n' "${repoArray[@]}"
			echo -e "$NC"
			for i in "${repoArray[@]}"
				do
				read -p "Do you want to update $i file ? (press y/Y)" -n 1 -r
					if [[ $REPLY =~ ^[Yy]$ ]]
					then
							echo ""
							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 $BackupDirectory/$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 ""
							echo -e "$ORANGE $i Switched to Rasanegar IRANIAN Mirror Successfully $NC"
							sleep  1
							echo ""
							
						fi
				done      
			echo ""				
			read -p "Do you want to clean yum repositoreis cache ? ( highly recomended ) " -n 1 -r
					if [[ $REPLY =~ ^[Yy]$ ]]
					then
						echo ""
						echo -e "-----------------------------------------------------"
						echo -e "$Purple Cleaning YUM repositores cache..... $NC"
						yum clean all
					fi
			
			echo -e "$Purple \n Your repositoreis succesfully switched to Official CentOS Mirror sponsored by Rasanegar.com"
			echo -e " \n $NC Enjoy !!"
			fi
		else
			echo -e "$Purple  Please make Sure Running this Script on $ORANGE CentOS $Purple Only ? failed to determine OS as CentOS  $NC "
		fi
# end of prompt IFS
fi
fi