#!/bin/bash

function createProxy {

cat <<EOF > /etc/nginx/sites-available/$1
server {

    server_name $1;

    location /ticket {
        proxy_pass https://$2;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
EOF

ln -s /etc/nginx/sites-available/$1 /etc/nginx/sites-enabled

echo "Proxy created"
}

created=no

while true; do

    if [ "$created" == yes ]; then
        read -p "Do you want to create another proxy? (yes/no): " create
    else
        read -p "Do you want to create proxy? (yes/no): " create
    fi

    if [ "$create" == no ]; then
        break
    fi

    if [ "$create" == yes ]; then

        read -p "Enter your domain address: " proxy

        if [ ! -z "$proxy" ]; then

            read -p "Enter your ESXI IP address: " esxi

            if [ ! -z "$esxi" ]; then

                createProxy $proxy $esxi
                certbot --nginx -d $proxy --non-interactive --agree-tos --register-unsafely-without-email --quiet --redirect
                created=yes
            fi
        fi
    fi

done
