#!/bin/bash
read -p 'Enter the domain or IP: ' DOMAIN
read -p 'Do you need a nginx configuration?(Y/N): ' nginx_conf
#Create ssl directory
private=/etc/ssl/$DOMAIN
if [ ! -d $private ]; then
        echo -e "\e[32mCreating /etc/ssl/$DOMAIN directory\e[0m"
        mkdir -p /etc/ssl/$DOMAIN
fi
#Generate the crt and key
openssl ecparam -genkey -name secp384r1 -out /etc/ssl/$DOMAIN/${DOMAIN}.key
openssl req -x509 -new -key /etc/ssl/$DOMAIN/${DOMAIN}.key \
  -out /etc/ssl/$DOMAIN/${DOMAIN}.crt \
  -days 365 \
  -subj "/C=US/ST=New York/CN=$DOMAIN/"
echo -e "\e[32mCreating /etc/ssl/$DOMAIN/${DOMAIN}.key\e[0m"
echo -e "\e[32mCreating /etc/ssl/$DOMAIN/${DOMAIN}.crt\e[0m"
#Generate a nginx configuration file
if [[ $nginx_conf == Y ]]; then
        echo -e "\e[32mCreating /etc/nginx/conf.d directory\e[0m"
        mkdir -p /etc/nginx/conf.d
        echo -e "\e[32mCreating /etc/nginx/conf.d/${DOMAIN}.conf\e[0m"
        curl -L sh.lihanzhang.cn/nginx/docker_nginx/conf -o /etc/nginx/conf.d/$DOMAIN.conf
        sed -i "s/domain/$DOMAIN/g" /etc/nginx/conf.d/$DOMAIN.conf
        #Generate the index.html
        echo -e "\e[32mCreating /www/$DOMAIN directory\e[0m"
        mkdir -p /www/$DOMAIN
        echo A test web $(date +"%Y-%m-%d %H:%M:%S") >"/www/$DOMAIN/index.html"
        echo -e "\e[32mGenerated /www/$DOMAIN/index.html\e[0m"
	docker restart nginx
fi

