#!/bin/bash

version_suffix="$(cat /etc/*-release | grep -E 'PRETTY_NAME(_LIKE)?=' | cut -d "=" -f2 | tr " " "-" | sed 's/["()]//g').tar.gz"
offline_folder_prefix="/root/wdd/offline/"

centos_common_tool_offline() {
  local common_tool_folder_prefix="/root/wdd/offline/common_tool"

  mkdir -p $common_tool_folder_prefix

  local item_list=(deltarpm net-tools iputils bind-utils lsof curl wget vim mtr htop screen unzip git openssl iftop tar zsh)
  local item
  for item in "${item_list[@]}"; do
    echo "current tool is => ${item}"
    sudo yum install -y --downloadonly --downloaddir=$common_tool_folder_prefix "${item}"
    echo ""
  done

  ls $common_tool_folder_prefix

}

centos_nfs_utils_offline() {
  local nfs_utils_folder_prefix="${offline_folder_prefix}nfs_utils"

  mkdir -p $nfs_utils_folder_prefix

  sudo yum install -y --downloadonly --downloaddir=$nfs_utils_folder_prefix nfs-utils

  ls $nfs_utils_folder_prefix

}

ubuntu_nfs_server_offline(){

  local nfs_utils_folder_prefix="${offline_folder_prefix}nfs_kernel_server"

  mkdir -p $nfs_utils_folder_prefix

  cd $nfs_utils_folder_prefix
  pwd
  apt-get download $(apt-rdepends nfs-kernel-server | grep -v "^ "  | sed 's/debconf-2.0/debconf/g')

  ls $nfs_utils_folder_prefix
}

ubuntu_nfs_common_offline(){
  local nfs_utils_folder_prefix="${offline_folder_prefix}nfs_common"

  mkdir -p $nfs_utils_folder_prefix

  cd $nfs_utils_folder_prefix
  pwd
  apt-get download $(apt-rdepends nfs-common | grep -v "^ "  | sed 's/debconf-2.0/debconf/g')

  ls $nfs_utils_folder_prefix
}

centos_download_tool() {
  local tool_name=$1
  if [ "$tool_name" == "" ]; then
    echo "[centos_download_and_gzip_tool] no tool specific ! exits! ${tool_name}"
    return
  fi

  local tool_folder_prefix="/root/wdd/offline/$tool_name"
  mkdir -p "$tool_folder_prefix"
  rm -rf "$tool_folder_prefix"/*

  yum install -y --downloadonly --downloaddir="$tool_folder_prefix" "${tool_name}"

  echo ""
  ls "${tool_folder_prefix}"
  echo ""
}

gzip_default_folder_split() {

  if ! command -v "tar" &>/dev/null; then
    rpm -ivh $offline_folder_prefix/common_tool/tar*.rpm
  fi

  cd ${offline_folder_prefix} || return
  pwd
  echo ""

  local folder
  for folder in $(ls $offline_folder_prefix); do
    echo "the current folder is ${folder}"
    if [[ ! -d $folder ]]; then
        continue
    fi
    echo ""
    cd ${offline_folder_prefix} || exit
    tar -czvf $folder-$version_suffix ./$folder/*
    echo ""
    echo ""
    ls | grep $version_suffix

  done

}

test_base_command_exits() {
  local base_command_list=(ifconfig mtr vgdisplay nslookup vim htop tar unzip iftop curl wget netstat git zsh)
  local command
  for command in "${base_command_list[@]}"; do
    if command -v "$command" &>/dev/null; then
      echo "$command exists"
    else
      echo "ERROR $command does not exist!"
    fi
    echo ""
  done
}

test_service_exists(){
  local base_service_list=(ntpd chronyd nginx nfs-server rpcbind docker)
  local service
  for service in "${base_service_list[@]}"; do
    if ! systemctl list-unit-files | grep "$service.service"; then
      echo "ERROR $service.service does not exist!"
    fi
    echo ""
  done
}

mkdir -p ${offline_folder_prefix}
#centos_common_tool_offline
#centos_nfs_utils_offline
#
#centos_download_tool "ntp"
#centos_download_tool "nginx"

ubuntu_nfs_server_offline
ubuntu_nfs_common_offline
#gzip_default_folder_split

#test_base_command_exits
#test_service_exists
