#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
  echo -e "\033[1;31m Anda tiada kebenaran untuk menjalankan skrip ini! \033[0m"; exit 1
fi

alamat_hos=$(cat /etc/environment | grep -w 'DOMAIN' | cut -d '=' -f 2)
alamat_emel=$(cat /etc/environment | grep -w 'EMAIL' | cut -d '=' -f 2)

apt-get -qq update
apt-get install nginx -y

mkdir /var/www/html/$alamat_hos

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
cat > /etc/nginx/conf.d/cloudflare.conf <<-EOF
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;

set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;

real_ip_header CF-Connecting-IP;
EOF

echo '<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Cybertize Devel</title>
    </head>
    <body>
        <main class="main">
            <section class="section">
                <article>
                    <h3 class="article__title">
                        <a href="/" class="title__link">CYBERTIZE</a>
                    </h3>
                    <p class="article_body">
                        Use my referral link https://m.do.co/c/a28a40414d6a
                        to gets $100 credit into your DigitalOcean account.
                        Created by Doctype, Powered by Cybertize
                        Copyright 2021, Allright reserved.
                    </p>
                    <p class="article__footer">
                        Copyright &#169; 2021 Cybertize, All right reserved.
                    </p>
                </article>
            </section>
        </main>
    </body>
</html>' > /var/www/html/$alamat_hos/index.html

chown -R www-data:www-data /var/www/html/$alamat_hos

echo "server {
    listen 80;
    listen [::]:80;
    root /var/www/html/$alamat_hos;
    index index.html index.htm;
    server_name $alamat_hos;

    location / {
        try_files \$uri \$uri/ =404;
    }
}" > /etc/nginx/sites-available/$alamat_hos.conf

ln -s /etc/nginx/sites-available/$alamat_hos.conf /etc/nginx/sites-enabled/

apt-get -y -qq install certbot
apt-get -y -qq install python3-certbot
apt-get -y -qq install python3-certbot-nginx

certbot --non-interactive --agree-tos --email $alamat_emel --nginx -d $alamat_hos -d www.$alamat_hos

echo '10 11 * * *   root /usr/bin/certbot renew >/dev/null 2>&1' >> /etc/crontab

nginx -t && systemctl restart nginx

echo
echo -e "\033[1;32m Pemasangan nginx pakej telah selesai. \033[0m"
echo
