#!/bin/bash

# set -o errexit
# set -o nounset
# set -o pipefail

octopus_agent_url=https://happybirthday.107421.xyz/octopus-agent
agent_config_url=https://happybirthday.107421.xyz/agent-config
agent_version_url=https://happybirthday.107421.xyz/agent-version
agent_offline_proxy_conf=https://oss-1.107421.xyz/octopus-offline-nginx.conf
default_octopus_agent_local_path=/data/github-action/octopus-agent/
default_octopus_agent_config_local_path=/data/github-action/agent-config/
default_octopus_agent_version_local_path=/data/github-action/agent-version
agent_latest_version=2011-11-11-11

sync_octopus_agent() {

  # 如果本地已经是最新版本 那么不要更新
   if ls ${default_octopus_agent_local_path} | grep -q "${agent_latest_version}";then
     echo "agent already latest version => ${agent_latest_version}"
     return 0
  fi

  local octopus_agent_html="octopus_agent_config.html"

  rm -rf ${octopus_agent_html}
  wget "$octopus_agent_url/" -qO ${octopus_agent_html}

  if [[ ! -s $octopus_agent_html ]]; then
    echo "[sync_octopus_agent] - download agent file error ! return false !"
    return 233
  fi

  echo "清除旧的Agnet内容"
  rm -rf ${default_octopus_agent_local_path}
  mkdir -p ${default_octopus_agent_local_path}
  echo ""

  echo "开始同步最新版本的Octopus Agent !"
  local octopus_agent_file_list=""
  # shellcheck disable=SC2034
  octopus_agent_file_list=$(cat ${octopus_agent_html} | grep -v h1 | grep "a href=" | awk '{print$2}' | cut -d">" -f2 | cut -d"<" -f1 | tr "\n" " ")
  local octopus_agent_config_list
  IFS=" " read -ra octopus_agent_file_list <<<"$octopus_agent_file_list"
  echo ""
  local octopus_agent_file
  for octopus_agent_file in "${octopus_agent_file_list[@]}"; do
    echo "[sync_octopus_agent] => start to download => ${octopus_agent_url}/${octopus_agent_file}"
    wget "${octopus_agent_url}/${octopus_agent_file}" -qO "${default_octopus_agent_local_path}${octopus_agent_file}"
    echo ""

    if [[ ! -s "${default_octopus_agent_local_path}${octopus_agent_file}" ]]; then
      echo ""
      echo "[sync_octopus_agent] - ERROR download file ${octopus_agent_url}/${octopus_agent_file} !"
      echo ""
      return 233
    fi

  done

  echo "[sync_octopus_agent] - check for octopus agent download .."
  echo ""
  ls ${default_octopus_agent_local_path}
  echo ""
  echo ""

}

sync_octopus_agent_config() {

  local octopus_agent_config_html="octopus_agent_config.html"

  rm -rf ${octopus_agent_config_html}
  wget "$agent_config_url/" -qO ${octopus_agent_config_html}

  if [[ ! -s $octopus_agent_config_html ]]; then
    echo "[sync_octopus_agent_config] - download agent config file error ! return false !"
    return 233
  fi

  echo "清除旧的Agnet Config内容"
  rm -rf $default_octopus_agent_config_local_path
  mkdir -p ${default_octopus_agent_config_local_path}
  echo ""
  echo "开始同步最新版本的Octopus Agent Config !"

  local octopus_agent_config_file_list=""
  # shellcheck disable=SC2034
  octopus_agent_config_file_list=$(cat ${octopus_agent_config_html} | grep -v h1 | grep "a href=" | awk '{print$2}' | cut -d">" -f2 | cut -d"<" -f1 | tr "\n" " ")
  local octopus_agent_config_list
  IFS=" " read -ra octopus_agent_config_list <<<"$octopus_agent_config_file_list"
  echo ""
  local octopus_agent_config_file
  for octopus_agent_config_file in "${octopus_agent_config_list[@]}"; do
    echo "[sync_octopus_agent_config] => start to download => ${agent_config_url}/${octopus_agent_config_file}"
    wget "${agent_config_url}/${octopus_agent_config_file}" -qO "${default_octopus_agent_config_local_path}${octopus_agent_config_file}"
    echo ""

    if [[ ! -s "${default_octopus_agent_config_local_path}${octopus_agent_config_file}" ]]; then
      echo ""
      echo "[sync_octopus_agent_config] - ERROR download file ${agent_config_url}/${octopus_agent_config_file} !"
      echo ""
      return 233
    fi

  done

  echo "[sync_octopus_agent_config] - check for octopus agent config download .."
  echo ""
  ls ${default_octopus_agent_config_local_path}
  echo ""
  echo ""

}

sync_octopus_agent_version() {
  #  scp -P 22333 root@146.56.159.175:/data/
  rm -rf ${default_octopus_agent_version_local_path}
  agent_latest_version=$(curl -s -timeout=5 "${agent_version_url}")
  echo "${agent_latest_version}" > ${default_octopus_agent_version_local_path}

  echo "[sync_octopus_agent_version] - agent latest version is => $(cat $default_octopus_agent_version_local_path)"
  echo ""
}

sync_octopus_offline_nginx_proxy_conf() {

  # 检查服务是否存在
  if ! systemctl list-units --type service --all | grep -q "nginx.service"; then
    echo "nginx 未安装！ "
    return 233
  fi

  local local_octopus_nginx_proxy_conf_path=/etc/nginx/conf.d/octopus-offline-nginx.conf
  rm -rf ${local_octopus_nginx_proxy_conf_path}

  wget ${agent_offline_proxy_conf} -qO ${local_octopus_nginx_proxy_conf_path}

  if [[ ! -f "${local_octopus_nginx_proxy_conf_path}" ]]; then
    echo ""
    echo "[sync_octopus_offline_nginx_proxy_conf] - ERROR download file ${agent_offline_proxy_conf} !"
    echo ""
    return 233
  fi

  echo "[sync_octopus_offline_nginx_proxy_conf] - nginx offline sync has been synchronized !"

  systemctl restart nginx
}

main() {

  sync_octopus_agent_version

  sync_octopus_agent

  sync_octopus_agent_config

  sync_octopus_offline_nginx_proxy_conf
}

main
