####### NOTE belongs to post config #####
nusr=radiobases
domain='radiobases.com'

avail=/etc/nginx/sites-available/${nusr}
hosts="/etc/hosts"
lp="/home/${nusr}/public_html/index.html"

#create new user
#sudo useradd -m -s $(which bash) -G sudo <USERNAME>
#id -u "${nusr}" >/dev/null 2>&1 || sudo useradd -m -c "${nusr}" "${nusr}" -s /bin/bash
id -u "${nusr}" >/dev/null 2>&1 || sudo useradd -m -c "${nusr}" "${nusr}" -G www-data


#quota on
sudo setquota -u "${nusr}" 28G 34G 0 0 /home
sudo quota -As -u ${nusr}

sudo chgrp www-data /home/${nusr}/public_html/

sudo chmod 755 /home/${nusr}
sudo chmod 750 /home/${nusr}/public_html
sudo chmod +s /home/${nusr}/public_html
sudo chmod 744 /home/${nusr}/public_html/index.html


#sudo printf "[global]\nbreak-system-packages = true\n" | sudo tee --append ${pipconf}
#
#add entry in /etc/nginx
#
## Generate $HTTP_CONF
if [ ! -f "${avail}" ]
then
    sudo touch ${avail}
    sudo cat << HTTP_CREATE | sudo tee --append ${avail}
server {
	listen 80 ;
	listen [::]:80;

	root /home/${nusr}/public_html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name ${domain};

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files \$uri \$uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php8.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}
HTTP_CREATE
fi


sudo ln -s /etc/nginx/sites-available/${nusr} /etc/nginx/sites-enabled/

sudo sed -i 's/\(^127.0.0.1.*$\)/& '${domain}'/' ${hosts}

## Generate $welcome
if [ ! -f "${lp}" ]
then
    sudo -u ${nusr} touch ${lp}
    sudo -u ${nusr} cat << HTTP_CREATE_LP | sudo tee --append ${lp}
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to ${domain}</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
HTTP_CREATE_LP
fi

# Set Cavern Enviroment
sudo -u ${nusr} mkdir -p /home/${nusr}/.config/pip/

if [[ ! -f /home/${nusr}/.config/pip/pip.conf ]]; then	
   sudo -u ${nusr} printf "[global] \nbreak-system-packages = true\n" > /home/${nusr}/.config/pip/pip.conf
fi

# Add python3 path 

# TODO search the line and  add if not exists
sudo -u ${nusr} printf "export PATH=\"$PATH:${nusr}/.local/bin\"" >> /home/${nusr}/.bashrc



sudo systemctl restart nginx.service
