#!/bin/bash

# Lets Encrypt
if [ -z "$WEBROOT" ] || [ -z "$GIT_EMAIL" ] || [ -z "$DOMAIN" ]; then
 echo "You need the \$WEBROOT, \$GIT_EMAIL and the \$DOMAIN Variables"
else
 certbot certonly --webroot -w $WEBROOT -d $DOMAIN --email $GIT_EMAIL --agree-tos --quiet
 ln -s /etc/nginx/sites-available/default-ssl.conf /etc/nginx/sites-enabled/

 # change nginx for webroot and domain name
 sed -i "s/##DOMAIN##/${DOMAIN}/g" /etc/nginx/sites-enabled/default-ssl.conf
 sed -i "s#root /var/www/html;#root ${WEBROOT};#g" /etc/nginx/sites-available/default-ssl.conf

 supervisorctl restart nginx
 
fi
