#!/usr/bin/env bash


# env
LinuxReleaseVersion=""
CMD_INSTALL=""
CMD_UPDATE=""
CMD_REMOVE=""
CPU_ARCH="unknown"
OFFLINE_PACKAGE_NAME_SUFFIX=""
# env


#######   获取系统版本及64位或32位信息
check_sys() {
  ## 判定Linux的发行版本
  if [ -f /etc/redhat-release ]; then
    LinuxReleaseVersion="centos"
  elif cat /etc/issue | grep -Eqi "debian"; then
    LinuxReleaseVersion="debian"
  elif cat /etc/issue | grep -Eqi "ubuntu"; then
    LinuxReleaseVersion="ubuntu"
  elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
    LinuxReleaseVersion="centos"
  elif cat /proc/version | grep -Eqi "debian"; then
    LinuxReleaseVersion="debian"
  elif cat /proc/version | grep -Eqi "ubuntu"; then
    LinuxReleaseVersion="ubuntu"
  elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
    LinuxReleaseVersion="centos"
  elif cat /etc/os-release | grep -Eqi "openEuler"; then
    LinuxReleaseVersion="euler"
  else
    LinuxReleaseVersion=""
  fi

  cpu_arch=$(uname -m)

  if [[ $cpu_arch == "x86_64" || $cpu_arch == "amd64" ]]; then
      CPU_ARCH="amd64"
  elif [[ $cpu_arch == "aarch64" || $cpu_arch == "arm64" ]]; then
      CPU_ARCH="arm64"
  fi

  echo "CPU architecture is $CPU_ARCH"

  # 判断系统的包管理工具  apt, yum, or zypper
  getPackageManageTool() {
    if [[ -n $(command -v apt-get) ]]; then
      CMD_INSTALL="apt-get -y -qq --allow-downgrades 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"
      CMD_REMOVE="zypper -y remove"
    else
      return 1
    fi
    return 0
  }

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

  ### 更新程序引索
  if [[ $SOFTWARE_UPDATED -eq 0 ]]; then
    echo "正在更新软件包管理..."
    $CMD_UPDATE
    SOFTWARE_UPDATED=1
  fi
  return 0
}

build_for_package_name_suffix(){

    # 从输出中提取PRETTY_NAME字段
    pretty_name=$(grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"')

    # 将提取的字段转换为 Ubuntu-20-04-06 的格式
    formatted_name=$(echo $pretty_name | sed 's/ /-/g')

    OFFLINE_PACKAGE_NAME_SUFFIX="$CPU_ARCH-$formatted_name"

}

package_nfs_client(){
  echo ""
  echo "[package_nfs_client] - start !"
  echo ""

  if [ "$LinuxReleaseVersion" = "ubuntu" ]; then
        echo ""
        $CMD_INSTALL "apt-rdepends"
        $CMD_INSTALL "debconf"

        rm -rf /tmp/nfs-client/
        mkdir -p /tmp/nfs-client/
        cd /tmp/nfs-client/

        echo "[package_nfs_client] - change to folder /tmp/nfs-client/"
        echo "[package_nfs_client] - start to download all needed nfs-client offline packages !"
        apt-get download $(apt-rdepends nfs-common| grep -v "^ "| sed 's/debconf-2.0/debconf/g')

        echo ""
        echo "[package_nfs_client] - ls nfs client offline packages !"
        ls /tmp/nfs-client/
        echo ""
        echo ""

        rm -rf /tmp/nfs-client/nfs-client-$CPU_ARCH-$formatted_name.tar.gz

        tar -czvf nfs-client-"$OFFLINE_PACKAGE_NAME_SUFFIX" /tmp/nfs-client/*
        mv nfs-client-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz /root/

    fi

}


package_nfs_server(){
  echo ""
  echo "[package_nfs_server] - start !"
  echo ""

  if [ "$LinuxReleaseVersion" = "ubuntu" ]; then
      echo ""
      $CMD_INSTALL "apt-rdepends"
      $CMD_INSTALL "debconf"

      rm -rf /tmp/nfs-server/
      mkdir -p /tmp/nfs-server/
      cd /tmp/nfs-server/

      echo "[package_nfs_server] - change to folder /tmp/nfs-server/"
      echo "[package_nfs_server] - start to download all needed nfs-server offline packages !"
      apt-get download $(apt-rdepends nfs-kernel-server | grep -v "^ " | sed 's/debconf-2.0/debconf/g')

      echo ""
      echo "[package_nfs_server] - ls nfs server offline packages !"
      ls /tmp/nfs-server/
      echo ""
      echo ""



      rm -rf /tmp/nfs-server/nfs-server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz

      tar -czvf nfs-server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz /tmp/nfs-server/*
      mv nfs-server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz /root/

  fi

}

package_chrony_server(){

  echo ""
  echo "[package_chrony_server] - start !"
  echo ""

  if [ "$LinuxReleaseVersion" = "ubuntu" ]; then
      echo ""
      $CMD_INSTALL "apt-rdepends"
      $CMD_INSTALL "debconf"

      rm -rf /tmp/chrony_server/
      mkdir -p /tmp/chrony_server/
      cd /tmp/chrony_server/

      echo "[package_chrony_server] - change to folder /tmp/chrony_server/"
      echo "[package_chrony_server] - start to download all needed chrony_server offline packages !"
      apt-get download $(apt-rdepends chrony | grep -v "^ "| sed 's/debconf-2.0/debconf/g')


      echo ""
      echo "[package_chrony_server] - ls nfs server offline packages !"
      ls /tmp/chrony_server/
      echo ""
      echo ""

      rm -rf /tmp/chrony_server/chrony_server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz

      tar -czvf chrony_server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz /tmp/chrony_server/*

      mv chrony_server-"$OFFLINE_PACKAGE_NAME_SUFFIX".tar.gz /root/

  fi
}

main(){

    check_sys
    build_for_package_name_suffix

    package_nfs_server

    package_nfs_client

    package_chrony_server

}


main