#!/usr/bin/bash

add_line () {
grep -qxF "$1" $2 || echo -e "\n$1" >> $2
}

#yes | cp -i /etc/resolv.conf /root/resolv.conf.0
#add_line "nameserver 127.0.0.1" "/etc/resolv.conf"
#add_line "nameserver 10.104.254.254" "/etc/resolv.conf"
#add_line "nameserver 9.9.9.10" "/etc/resolv.conf"
#add_line "nameserver 77.88.8.8" "/etc/resolv.conf"

apt install -y nginx libnginx-mod-stream dnsmasq certbot

wget https://github.com/joohoi/acme-dns-certbot-joohoi/raw/master/acme-dns-auth.py
sed -i 's/env python$/env python3/g' ./acme-dns-auth.py
chmod +x acme-dns-auth.py
mv acme-dns-auth.py /etc/letsencrypt/

cp /etc/dnsmasq.conf /etc/dnsmasq.conf.0
cp /etc/nginx/sites-available/default /etc/nginx/default.0
yes | cp -i ${target_in}/dnsmasq/dnsmasq.conf /etc/dnsmasq.conf
yes | cp -i ${target_in}/nginx/default /etc/nginx/sites-available/default

add_line "www-data    hard    nofile    50000" "/etc/security/limits.conf"
add_line "www-data    soft    nofile    50000" "/etc/security/limits.conf"

dnsmasq --test
systemctl enable --now dnsmasq
systemctl status dnsmasq

#openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt -subj "/C=RU/ST=Tyumen/L=Tyumen/O=its/OU=its/CN=al.tar44046@gmail.com"
#openssl dhparam -out /etc/nginx/dhparam.pem 4096

systemctl enable --now nginx
systemctl restart nginx
systemctl status nginx
