#!/bin/bash

echo ""
echo ""
lib_file_list=(wdd-lib-log.sh)
# shellcheck disable=SC2068
for lib_file in ${lib_file_list[@]}; do
    # shellcheck disable=SC2154
    wget "${oss_url_prefix}/${lib_file}" -qO "${octopus_agent_path}/lib/${lib_file}"
    echo "加载库文件 < ${octopus_agent_path}/lib/${lib_file}"
    # shellcheck disable=SC1090
    . "${octopus_agent_path}/lib/${lib_file}"
done

# 系统相关
linux_release_version=""
linux_release=""
os_bit=""
cmd_install="apt-get -y -qq --allow-downgrades install"
cmd_update="apt-get -qq update"
cmd_remove="apt-get -y remove"
software_updated=0

# 判断命令是否存在
command_exists() {
  command -v "$@" >/dev/null 2>&1
}

# 判定是否是root用户
check_root() {
  FunctionStart "判定Root用户！"
  if [[ $EUID != 0 ]]; then
    colorEchoRed "当前非root账号(或没有root权限)，无法继续操作，请更换root账号!"
    colorEchoYellow "使用sudo -命令获取临时root权限（执行后可能会提示输入root密码）"
    exit 1
  else
    log "当前为root账号 ！"
  fi
  FunctionEnd
}

#######   获取系统版本及64位或32位信息
check_sys() {
  FunctionStart "获取系统参数"

  # 获取当前终端的宽度，动态调整分割线的长度
  # local shell_width=$(stty size | awk '{print $2}')
  # SplitLine=$(yes "-" | sed "${shell_width}"'q' | tr -d '\n')

  local sys_bit
  sys_bit=$(uname -m)
  case $sys_bit in
    i[36]86)
      os_bit="32"
      linux_release="386"
      ;;
    x86_64)
      os_bit="64"
      linux_release="amd64"
      ;;
    *armv6*)
      os_bit="arm"
      linux_release="arm6"
      ;;
    *armv7*)
      os_bit="arm"
      linux_release="arm7"
      ;;
    *aarch64* | *armv8*)
      os_bit="arm64"
      linux_release="arm64"
      ;;
    *)
      error "
       哈哈……这个 辣鸡脚本 不支持你的系统。 (-_-) \n
       备注: 仅支持 Ubuntu 16+ / Debian 8+ / CentOS 7+ 系统
       " && exit 1
      ;;
  esac
  log "获取的系统信息如下： OS_BIT => ${os_bit} LinuxRelease => $linux_release"

  ## 判定Linux的发行版本
  if [ -f /etc/redhat-release ]; then
    linux_release_version="centos"
  elif cat /etc/issue | grep -Eqi "debian"; then
    linux_release_version="debian"
  elif cat /etc/issue | grep -Eqi "ubuntu"; then
    linux_release_version="ubuntu"
  elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
    linux_release_version="centos"
  elif cat /proc/version | grep -Eqi "debian"; then
    linux_release_version="debian"
  elif cat /proc/version | grep -Eqi "ubuntu"; then
    linux_release_version="ubuntu"
  elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
    linux_release_version="centos"
  else
    linux_release_version=""
  fi
  log "系统参数信息如下：  linux_release_version => $linux_release_version"

  # 判断系统的包管理工具  apt, yum, or zypper
  get_package_manage_tool() {
    if [[ -n $(command -v apt-get) ]]; then
      cmd_install="apt-get -y -qq install"
      cmd_update="apt-get -qq update"
      cmd_remove="apt-get -y remove"
    elif [[ -n $(command -v yum) ]]; then
      cmd_install="yum -y -q install"
      cmd_update="yum -q makecache"
      cmd_remove="yum -y remove"
    elif [[ -n $(command -v zypper) ]]; then
      cmd_install="zypper -y install"
      cmd_update="zypper ref"
      # shellcheck disable=SC2034
      cmd_remove="zypper -y remove"
    else
      return 1
    fi

  }

  # 检查系统包管理方式，更新包
  get_package_manage_tool
  if [[ $? -eq 1 ]]; then
    error "系统的包管理不是 APT or YUM, 请手动安装所需要的软件."
    return 1
  fi

  log "系统包管理工具为 =>  $cmd_install"

  # shellcheck disable=SC2154
  if [[ $is_offline == 1 ]]; then
      FunctionEnd
      return 0
  fi


  ### 更新程序引索
  if [[ $software_updated -eq 0 ]]; then
    log "正在更新软件包管理...可能花费较长时间…………"
    $cmd_update
    software_updated=1
  fi

  FunctionEnd
}

# main
main() {
    log "sys脚本被调用了！"
}

main
