#!/bin/bash

set -o nounset
set -o errexit

# 安装docker
_install_docker() {

	# 判断安装环境
	local cmd='apt'
	local name='docker-ce'
	if [[ $(command -v yum) ]]; then
		cmd='yum'
		name='docker'
	fi

	# 安装Docker
	${cmd} install -y ${name}
	systemctl enable --now docker
}

# 安装docker-compose
_insatll_docker_compose() {

	curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
	ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
	chmod +x /usr/local/bin/docker-compose
}

init() {

	if [[ -z $(command -v docker) ]]; then
		_install_docker
	fi

	if [[ -z $(command -v docker-compose) ]]; then
		_insatll_docker_compose
	fi
}

# 安装Docker
init

# 创建文件夹
if [[ ! -x '/wordpress' ]]; then
  mkdir -p /wordpress/{conf.d,php-conf}
fi

# 创建docker-compose
cat > /wordpress/docker-compose.yml << EOF
version: '3.5'
services:

  mysql:
    tty: true
    privileged: true
    image: mysql
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime
      - ./data-mysql:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: \${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: \${MYSQL_DATABASE}
      MYSQL_USER: \${MYSQL_USER}
      MYSQL_PASSWORD: \${MYSQL_PASSWORD}
    container_name: "mysql"

  wordpress:
    tty: true
    privileged: true
    image: wordpress
    restart: always
    links:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: \${MYSQL_USER}
      WORDPRESS_DB_PASSWORD: \${MYSQL_PASSWORD}
      WORDPRESS_DB_NAME: \${MYSQL_DATABASE}
    volumes:
      - /etc/localtime:/etc/localtime
      - ./data-html:/var/www/html
      - ./php-conf/php.ini:/usr/local/etc/php/conf.d/php.ini
    container_name: "wordpress"

  nginx:
    tty: true
    privileged: true
    image: nginx
    restart: always
    links:
      - wordpress
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime
      - ./conf.d:/etc/nginx/conf.d
      - ./cert:/etc/nginx/cert
    container_name: "nginx"

  phpmyadmin:
    tty: true
    privileged: true
    image: phpmyadmin
    restart: always
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_CONTROLUSER: root
      PMA_CONTROLPASS: \${PMA_CONTROLPASS}
    container_name: "phpmyadmin"
EOF

# 创建配置文件
cat > /wordpress/.env << EOF
MYSQL_ROOT_PASSWORD=Everway@2023
MYSQL_DATABASE=wordpress
MYSQL_USER=wordpress
MYSQL_PASSWORD=wordpress
PMA_CONTROLPASS=Microud@2023
EOF

# 创建nginx配置
cat > /wordpress/conf.d/default.conf << EOF
server {

    listen 80;
    # server_name wp.microud.com;

    # listen 443 ssl;
    # ssl_certificate /etc/nginx/cert/cert.crt;
    # ssl_certificate_key /etc/nginx/cert/cert.key;

    location / {

	client_max_body_size 100M;
        proxy_pass http://wordpress;

        proxy_http_version 1.1;
        proxy_cache_bypass \$http_upgrade;

        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto \$scheme;
        proxy_set_header X-Forwarded-Host \$host;
        proxy_set_header X-Forwarded-Port \$server_port;
    }
}
EOF

# 配置PHP文件
cat > /wordpress/php-conf/php.ini << EOF
upload_max_filesize = 100M
max_file_uploads = 100M
EOF

docker pull mysql
docker pull wordpress
docker pull nginx
docker pull phpmyadmin

echo "========================================================"
echo 
echo "1. 成功生成配置，可使用 vi /wordpress/.env 修改默认密码"
echo 
echo "2. 前往 /wordpress 目录并进行安装, 命令如下: "
echo "cd /wordpress && docker-compose up -d"
echo
echo "========================================================"
