#!/bin/bash

. /root/IdeaProjects/ProjectOctopus/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-log.sh

. /root/IdeaProjects/ProjectOctopus/source/src/main/java/io/wdd/source/shell/lib/wdd-lib-env.sh

warn "开始清除所有版本的Nginx！"

log "开始下载最新版本的Nginx"

wget https://nginx.org/download/nginx-1.22.1.tar.gz
tar -zvxf nginx-1.22.1.tar.gz

# sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

log "开始添加稳定版本的Nginx源！"

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
  | sudo tee /etc/apt/preferences.d/99nginx

sudo apt update

sudo apt install nginx

sudo apt upgrade nginx

allTable=(app_appdomain_relation
  app_info
  appdomain_info
  domain_info
  execution_log
  manage_info
  provider_info
  script_scheduler
  server_app_relation
  server_domain_relation
  server_info
  server_manage_relation)

for table in {$allTable[*]}; do
    #echo "table is $table"

  #    echo "create table ${table} (id tinyint);"
  #    echo "ALTER TABLE $table DISCARD TABLESPACE;"
    echo "ALTER TABLE $table IMPORT TABLESPACE;"

done

export DOMAIN_NAME=wmm.107421.xyz
export CF_Token="oXJRP5XI8Zhipa_PtYtB_jy6qWL0I9BosrJEYE8p"
export CF_Account_ID="dfaadeb83406ef5ad35da02617af9191"
export CF_Zone_ID="511894a4f1357feb905e974e16241ebb"

acme.sh   --issue   --dns dns_cf -d ${DOMAIN_NAME} --keylength ec-256
acme.sh --install-cert -d ${DOMAIN_NAME} --ecc \
  --key-file     /etc/nginx/conf.d/ssl_key/${DOMAIN_NAME}.key.pem \
  --fullchain-file /etc/nginx/conf.d/ssl_key/${DOMAIN_NAME}.cert.pem \
  --reloadcmd   "systemctl restart nginx --force"
