#!/bin/bash


RED="31m"      ## 姨妈红
GREEN="32m"    ## 水鸭青
YELLOW="33m"   ## 鸭屎黄
PURPLE="35m"   ## 基佬紫
BLUE="36m"     ## 天依蓝

######## 颜色函数方法很精妙 ############
colorEcho() {
    echo -e "\033[${1}${@:2}\033[0m" 1>&2
}

check_root() {
    if [[ $EUID != 0 ]]; then
        colorEcho ${RED} "当前非root账号(或没有root权限)，无法继续操作，请更换root账号!"
        colorEcho ${YELLOW} "使用sudo -命令获取临时root权限（执行后可能会提示输入root密码）"
        exit 1
    fi
}

backupOriginAPT(){
  echo "正在备份现有的源文件…………"
  mv /etc/apt/sources.list /etc/apt/sources.list.backup
}

changeAPTMirrot(){
  local $MirrorAddress=aliyun
  if [ $1 -ne "" ] 
  then
  
    colorEcho $BLUE "开始将源文件替换为( $1 )的镜像源……"
    


  cat >>/etc/apt/sources.list <<EOF
deb $MirrorAddress bionic main restricted universe multiverse
deb $MirrorAddress bionic-security main restricted universe multiverse
deb $MirrorAddress bionic-updates main restricted universe multiverse
deb $MirrorAddress bionic-proposed main restricted universe multiverse
deb $MirrorAddress bionic-backports main restricted universe multiverse
deb-src $MirrorAddress bionic main restricted universe multiverse
deb-src $MirrorAddress bionic-security main restricted universe multiverse
deb-src $MirrorAddress bionic-updates main restricted universe multiverse
deb-src $MirrorAddress bionic-proposed main restricted universe multiverse
deb-src $MirrorAddress bionic-backports main restricted universe multiverse
EOF
  fi

}

changeToAliMirror(){
  colorEcho $BLUE "开始将源文件替换为阿里云的镜像源……"
  cat >>/etc/apt/sources.list <<EOF
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
}

changeToTunaMirror(){
  colorEcho $BLUE"开始将源文件替换为清华的镜像源……"
  cat >>/etc/apt/sources.list <<EOF
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
}

changeToUSTCMirror(){
  colorEcho $BLUE "开始将源文件替换为中科大的镜像源……"
  cat >>/etc/apt/sources.list <<EOF
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
EOF
}

postChange(){
  echo ""
  colorEcho $GREEN "请查看当前的source.list文件中的内容："
  colorEcho $GREEN "----------------------------------------------------------------------------------------"
  cat /etc/apt/sources.list
  colorEcho $GREEN "----------------------------------------------------------------------------------------"

  echo ""
  colorEcho $BLUE "开始执行更新程序……"
  apt-get update
}

main(){
  check_root

  backupOriginAPT

  ## 更换为阿里云的apt
  changeToAliMirror
  ## 清华大学源
  #changeToTunaMirror

  ## 中科大源
  #changeToUSTCMirror
  
  postChange
}


main