#!/bin/bash
############################################################################
#Function:    confiture mirrors source
#Usage:       bash mirror_source.sh
#Author:      Huawei Cloud OS Team
#Company:     Huawei Cloud Computing
#Version:     1.0
#Version:     1.1   Add the arm platform yum source configuration
############################################################################

os_arch=$(arch)

check_os_release() {
  while true; do
    os_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)
    os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]; then
      if echo "$os_release" | grep "release 5" >/dev/null 2>&1; then
        os_release=redhat5
        echo "$os_release"
      elif echo "$os_release" | grep "release 6" >/dev/null 2>&1; then
        os_release=redhat6
        echo "$os_release"
      elif echo "$os_release" | grep "release 7" >/dev/null 2>&1; then
        os_release=redhat7
        echo "$os_release"
      elif echo "$os_release" | grep "release 8" >/dev/null 2>&1; then
        os_release=redhat8
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    ###################################CentOS###########################################
    #Check CentOS version
    os_release_2=$(grep "CentOS" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]; then
      if echo "$os_release_2" | grep "release 5" >/dev/null 2>&1; then
        os_release=centos5
        echo "$os_release"
      elif echo "$os_release_2" | grep "release 6" >/dev/null 2>&1; then
        os_release=centos6
        echo "$os_release"
      elif echo "$os_release_2" | grep "release 7" >/dev/null 2>&1; then
        os_release=centos7
        echo "$os_release"
      elif echo "$os_release_2" | grep "release 8" >/dev/null 2>&1; then
        os_release=centos8
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    ################################# Huawei Cloud EulerOS############################################
    os_release_2=$(grep "Huawei Cloud" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]; then
      if echo "$os_release_2" | grep "release 1.0" >/dev/null 2>&1; then
        os_release=hce10
        echo "$os_release"
      elif echo "$os_release_2" | grep "release 1.1" >/dev/null 2>&1; then
        os_release=hce11
        echo "$os_release"
      elif echo "$os_release_2" | grep "release 2.0" >/dev/null 2>&1; then
        os_release=hce20
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    ################################Ubuntu###########################################
    os_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]; then
      if echo "$os_release" | grep "Ubuntu 10" >/dev/null 2>&1; then
        os_release=ubuntu10
        echo "$os_release"
      elif echo "$os_release" | grep "Ubuntu 12.04" >/dev/null 2>&1; then
        os_release=ubuntu1204
        echo "$os_release"
      elif echo "$os_release" | grep "Ubuntu 14.04" >/dev/null 2>&1; then
        os_release=ubuntu1404
        echo "$os_release"
      elif echo "$os_release" | grep "Ubuntu 16.04" >/dev/null 2>&1; then
        os_release=ubuntu1604
        echo "$os_release"
      elif echo "$os_release" | grep "Ubuntu 18.04" >/dev/null 2>&1; then
        os_release=ubuntu1804
        echo "$os_release"
      elif echo "$os_release" | grep "Ubuntu 20.04" >/dev/null 2>&1; then
        os_release=ubuntu2004
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    #################################Debian###########################################
    os_release=$(grep -i "debian" /etc/issue 2>/dev/null)
    os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)
    if [ "$os_release" ] && [ "$os_release_2" ]; then
      if echo "$os_release" | grep "Linux 7" >/dev/null 2>&1; then
        os_release=debian7
        echo "$os_release"
      elif echo "$os_release" | grep "Linux 8" >/dev/null 2>&1; then
        os_release=debian8
        echo "$os_release"
      elif echo "$os_release" | grep "Linux 9" >/dev/null 2>&1; then
        os_release=debian9
        echo "$os_release"
      elif echo "$os_release" | grep "Linux 10" >/dev/null 2>&1; then
        os_release=debian10
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    #################################EulerOS############################################
    os_release_1=$(grep "SP" /etc/*release 2>/dev/null)
    os_release_2=$(grep "EulerOS" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]  && [ "$os_release_1" ]; then
      if echo "$os_release_2" | grep "EulerOS release 2.0 (SP2)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP2
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP3)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP3
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP5)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP5
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP8)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP8
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP9x86_64)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP9x86_64
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP9)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP9
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP10x86_64)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP10x86_64
        echo "$os_release"
      elif echo "$os_release_2" | grep "EulerOS release 2.0 (SP10)" >/dev/null 2>&1; then
        os_release=EulerOS2.0SP10
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    ############################opensuse################################################
    os_release_2=$(grep -i "opensuse" /etc/*release 2>/dev/null)
    if [ "$os_release_2" ]; then
      if echo "$os_release_2" | grep "13" >/dev/null 2>&1; then
        os_release=opensuse13
        echo "$os_release"
      elif echo "$os_release_2" | grep "15.0" >/dev/null 2>&1; then
        os_release=opensuse1500
        echo "$os_release"
      elif echo "$os_release_2" | grep "42.2" >/dev/null 2>&1; then
        os_release=opensuse4202
        echo "$os_release"
      elif echo "$os_release_2" | grep "42.3" >/dev/null 2>&1; then
        os_release=opensuse4203
        echo "$os_release"
      else
        os_release=""
        echo "$os_release"
      fi
      break
    fi
    break
  done
}

######################################################CentOS && RedHat#####################################
modify_rhel5_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-5.repo
  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_rhel6_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  #rpm -ivh http://mirrors.myhuaweicloud.com/epel/epel-release-latest-6.noarch.rpm
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -qO /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-6.repo
  wget -qO /etc/yum.repos.d/epel.repo http://mirrors.myhuaweicloud.com/repo/epel-6.repo
  yum clean metadata
  yum makecache
  yum install epel-release -y >/dev/null 2>&1
  cd ~ || return
}

modify_rhel7_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  #rpm -ivh http://mirrors.myhuaweicloud.com/epel/epel-release-latest-7.noarch.rpm
  mv /etc/yum.repos.d/*.repo /tmp/
  if [ "$os_arch" = "aarch64" ]; then
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/Centos-altarch-7.repo
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.myhuaweicloud.com/repo/epel-7.repo
  else
    wget -qO /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-7.repo

    wget -qO /etc/yum.repos.d/epel.repo http://mirrors.myhuaweicloud.com/repo/epel-7.repo
  fi
  yum clean metadata
  yum makecache
  yum install epel-release -y >/dev/null 2>&1
  cd ~ || return
}

modify_rhel8_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  #rpm -ivh http://mirrors.myhuaweicloud.com/epel/epel-release-latest-8.noarch.rpm
  mv /etc/yum.repos.d/*.repo /tmp/
  if [ "$os_arch" = "aarch64" ]; then
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/Centos-altarch-8.repo
  else
    wget -qO /etc/yum.repos.d/CentOS-Base.repo http://mirrors.myhuaweicloud.com/repo/CentOS-Base-8.repo
  fi
  wget -qO /etc/yum.repos.d/epel.repo http://mirrors.myhuaweicloud.com/repo/epel-8.repo
  wget -qO /etc/yum.repos.d/epel-modular.repo  http://mirrors.myhuaweicloud.com/repo/epel-modular.repo

  yum clean metadata
  yum makecache
  yum install epel-release -y >/dev/null 2>&1
  cd ~ || return
}
###############################################  Euler OS ################################################
modify_euler22_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -O /etc/yum.repos.d/EulerOS-Base.repo http://mirrors.myhuaweicloud.com/repo/EulerOS_2_2_base.repo
  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_euler23_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -O /etc/yum.repos.d/EulerOS-Base.repo http://mirrors.myhuaweicloud.com/repo/EulerOS_2_3_base.repo
  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_euler25_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -O /etc/yum.repos.d/EulerOS-Base.repo http://mirrors.myhuaweicloud.com/repo/EulerOS_2_5_base.repo
  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_euler28_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  wget -O /etc/yum.repos.d/EulerOS-Base.repo http://mirrors.myhuaweicloud.com/repo/EulerOS_2_8_base.repo
  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_euler29_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  if [ "$os_arch" = "aarch64" ]; then
    cat >/etc/yum.repos.d/EulerOS.repo <<EOF
[base]
name=EulerOS-2.0SP9 base
baseurl=http://mirrors.myhuaweicloud.com/euler/2.9/os/aarch64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/euler/2.9/os/RPM-GPG-KEY-EulerOS
EOF

  else
    cat >/etc/yum.repos.d/EulerOS.repo <<EOF
[base]
name=EulerOS-2.0SP9 base
baseurl=http://mirrors.myhuaweicloud.com/euler/2.9/os/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/euler/2.9/os/RPM-GPG-KEY-EulerOS
EOF

fi
  yum clean metadata
  yum makecache
  cd ~ || return
}
modify_euler210_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  if [ "$os_arch" = "aarch64" ]; then
    cat >/etc/yum.repos.d/EulerOS.repo <<EOF
[base]
name=EulerOS-2.0SP10 base
baseurl=http://mirrors.myhuaweicloud.com/euler/2.10/os/aarch64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/euler/2.10/os/RPM-GPG-KEY-EulerOS
EOF

  else
    cat >/etc/yum.repos.d/EulerOS.repo <<EOF
[base]
name=EulerOS-2.0SP10 base
baseurl=http://mirrors.myhuaweicloud.com/euler/2.10/os/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/euler/2.10/os/RPM-GPG-KEY-EulerOS
EOF

fi
  yum clean metadata
  yum makecache
  cd ~ || return
}

############################################### hce OS ################################################
modify_hce10_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  cat >/etc/yum.repos.d/hce.repo <<EOF
[base]
name=HCE \$releasever base
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/RPM-GPG-KEY-EulerOS
EOF

  yum clean metadata
  yum makecache
  cd ~ || return
}
modify_hce11_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  cat >/etc/yum.repos.d/hce.repo <<EOF
[base]
name=HCE \$releasever base
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/RPM-GPG-KEY-HCE-1

[updates]
name=HCE \$releasever updates
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/updates/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/updates/RPM-GPG-KEY-HCE-1

[debuginfo]
name=HCE \$releasever debuginfo
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/debuginfo/\$basearch/
enabled=0
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/debuginfo/RPM-GPG-KEY-HCE-1

EOF

  yum clean metadata
  yum makecache
  cd ~ || return
}

modify_hce20_yum() {
  echo -e "\033[40;32mBackup the original configuration file to /tmp dir.\n\033[40;37m"
  mv /etc/yum.repos.d/*.repo /tmp/
  cat >/etc/yum.repos.d/hce.repo <<EOF
[base]
name=HCE \$releasever base
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/os/RPM-GPG-KEY-HCE-2

[updates]
name=HCE \$releasever updates
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/updates/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/updates/RPM-GPG-KEY-HCE-2

[debuginfo]
name=HCE \$releasever debuginfo
baseurl=http://mirrors.myhuaweicloud.com/hce/\$releasever/debuginfo/\$basearch/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.myhuaweicloud.com/hce/\$releasever/debuginfo/RPM-GPG-KEY-HCE-2

EOF

  yum clean metadata
  yum makecache
  cd ~ || return
}


###############################################  Ubuntu ##################################################

update_ubuntu1204_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#12.04
deb http://mirrors.myhuaweicloud.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ precise-backports main restricted universe multiverse
EOF
  apt-get update
}

update_ubuntu1404_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#14.04
deb http://mirrors.myhuaweicloud.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.myhuaweicloud.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.myhuaweicloud.com/ubuntu/ trusty-backports main restricted universe multiverse
EOF
  apt-get update
}

update_ubuntu1604_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  wget -qO /etc/apt/sources.list http://mirrors.myhuaweicloud.com/repo/sources.list.xenial

  # ubuntu 16.04 EOS, and expand ESM and actived esm.ubuntu.com, no EIP leading to do not access esm.ubuntu.com; 
  # delete ubuntu-esm-infra.list
  if [ -f "/etc/apt/sources.list.d/ubuntu-esm-infra.list" ]; then
      rm -rf /etc/apt/sources.list.d/ubuntu-esm-infra.list
  fi
  # delete lock file
  if [ -f "/var/lib/apt/lists/lock" ]; then
      rm -rf /var/lib/apt/lists/lock
  fi

  apt-get update
}

update_ubuntu1804_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  if [ "$os_arch" = "aarch64" ]; then
    wget -qO /etc/apt/sources.list http://mirrors.myhuaweicloud.com/repo/aarch64/sources.list.bionic
  else
    wget -O /etc/apt/sources.list http://mirrors.myhuaweicloud.com/repo/sources.list.bionic
  fi
  apt-get update
}

update_ubuntu2004_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  wget -qO /etc/apt/sources.list http://mirrors.myhuaweicloud.com/repo/sources.list.focal
  apt-get update
}

update_debian7_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#debian7
deb http://mirrors.myhuaweicloud.com/debian/ wheezy main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian/ wheezy-proposed-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ wheezy main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ wheezy-proposed-updates main non-free contrib
EOF
  apt-get update
}

update_debian8_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#debian8
deb http://mirrors.myhuaweicloud.com/debian jessie main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian jessie main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian jessie-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian jessie-updates main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian-security jessie/updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian-security jessie/updates main non-free contrib
EOF
  apt-get update
}

update_debian9_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#debian9
deb http://mirrors.myhuaweicloud.com/debian stretch main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian stretch main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian stretch-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian stretch-updates main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian stretch-proposed-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian stretch-proposed-updates main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian stretch-backports main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian stretch-backports main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian-security stretch/updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian-security stretch/updates main non-free contrib
EOF
  apt-get update
}

update_debian10_apt_source() {
  echo -e "\033[40;32mBackup the original configuration file,new name and path is /etc/apt/sources.list.back.\n\033[40;37m"
  cp -fp /etc/apt/sources.list /etc/apt/sources.list.back
  cat >/etc/apt/sources.list <<EOF
#debian10
deb http://mirrors.myhuaweicloud.com/debian/ buster main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ buster main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ buster-updates main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian/ buster-proposed-updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ buster-proposed-updates main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian/ buster-backports main non-free contrib
deb http://mirrors.myhuaweicloud.com/debian-security buster/updates main non-free contrib
deb-src http://mirrors.myhuaweicloud.com/debian-security buster/updates main non-free contrib
EOF
  apt-get update
}

update_opensuse_source13() {
  mv /etc/zypp/repos.d/* /tmp/
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/openSUSE-stable/repo/oss/ openSUSE-13.2-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/openSUSE-stable/repo/non-oss/ openSUSE-13.2-Non-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/update/openSUSE-stable/ openSUSE-13.2-Update-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/update/openSUSE-non-oss-current/ openSUSE-13.2-Update-Non-Oss
}

update_opensuse_source1500() {
  mv /etc/zypp/repos.d/* /tmp/
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/openSUSE-stable/repo/oss/ openSUSE-15.0-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/openSUSE-stable/repo/non-oss/ openSUSE-15.0-Non-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/update/openSUSE-stable/ openSUSE-15.0-Update-Oss
  zypper addrepo -fcg http://mirrors.myhuaweicloud.com/opensuse/update/openSUSE-non-oss-current/ openSUSE-15.0-Update-Non-Oss
}

update_opensuse_source4202() {
  mv /etc/zypp/repos.d/* /tmp/
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/leap/42.2/repo/oss HWCloud:42.2:OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/leap/42.2/repo/non-oss HWCloud:42.3:NON-OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/update/leap/42.2/oss HWCloud:42.2:UPDATE-OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/update/leap/42.2/non-oss HWCloud:42.2:UPDATE-NON-OSS
}

update_opensuse_source4203() {
  mv /etc/zypp/repos.d/* /tmp/
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/leap/42.3/repo/oss HWCloud:42.3:OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/distribution/leap/42.3/repo/non-oss HWCloud:42.3:NON-OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/update/leap/42.3/oss HWCloud:42.3:UPDATE-OSS
  sudo zypper ar -fcg http://mirrors.myhuaweicloud.com/opensuse/update/leap/42.3/non-oss HWCloud:42.3:UPDATE-NON-OSS
}

####################Start###################
#check lock file ,one time only let the script run one time
LOCKfile=/tmp/.$(basename "$0")
if [ -f "$LOCKfile" ]; then
  echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\n\033[0m"
  exit
else
  echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\n\033[40;37m"
  touch "$LOCKfile"
fi

#check user
if [ "$(id -u)" != "0" ]; then
  echo -e "\033[1;40;31mError: You must be root to run this script, please use root to install this script.\n\033[0m"
  rm -rf "$LOCKfile"
  exit 1
fi
echo -e "\033[40;32mStep 2.Begin to check the OS issue.\n\033[40;37m"
os_release=$(check_os_release)
if [ "X$os_release" == "X" ]; then
  echo -e "\033[1;40;31mThe OS does not identify,So this script is not executede.\n\033[0m"
  rm -rf "$LOCKfile"
  exit 0
else
  echo -e "\033[40;32mThis OS is $os_release.\n\033[40;37m"
fi

echo -e "\033[40;32mStep 3.Begin to modify the source configration file and update.\n\033[40;37m"
case "$os_release" in
redhat5 | centos5)
  modify_rhel5_yum
  ;;
redhat6 | centos6)
  modify_rhel6_yum
  ;;
redhat7 | centos7)
  modify_rhel7_yum
  ;;
redhat8 |centos8)
  modify_rhel8_yum
  ;;
hce10)
  modify_hce10_yum
  ;;
hce11)
  modify_hce11_yum
  ;;
hce20)
  modify_hce20_yum
  ;;
EulerOS2.0SP2)
  modify_euler22_yum
  ;;
EulerOS2.0SP3)
  modify_euler23_yum
  ;;
EulerOS2.0SP5)
  modify_euler25_yum
  ;;
EulerOS2.0SP8)
  modify_euler28_yum
  ;;
EulerOS2.0SP9 | EulerOS2.0SP9x86_64)
  modify_euler29_yum
  ;;
EulerOS2.0SP10 | EulerOS2.0SP10x86_64)
  modify_euler210_yum
  ;;
ubuntu1204)
  update_ubuntu1204_apt_source
  ;;
ubuntu1404)
  update_ubuntu1404_apt_source
  ;;
ubuntu1604)
  update_ubuntu1604_apt_source
  ;;
ubuntu1804)
  update_ubuntu1804_apt_source
  ;;
ubuntu2004)
  update_ubuntu2004_apt_source
  ;;
debian7)
  update_debian7_apt_source
  ;;
debian8)
  update_debian8_apt_source
  ;;
debian9)
  update_debian9_apt_source
  ;;
debian10)
  update_debian10_apt_source
  ;;
opensuse13)
  update_opensuse_source13
  ;;
opensuse1500)
  update_opensuse_source1500
  ;;
opensuse4202)
  update_opensuse_source4202
  ;;
opensuse4203)
  update_opensuse_source4203
  ;;
esac
echo -e "\033[40;32mSuccess,exit now!\n\033[40;37m"
  rm -rf "$LOCKfile"
