#!/bin/bash
#   ___      _               _  _      _     __  __
# / __| ___| |_ _  _ _ __  | \| |__ _(_)_ _ \ \/ /
# \__ \/ -_)  _| || | '_ \ | .` / _` | | ' \ >  <
# |___/\___|\__|\_,_| .__/ |_|\_\__, |_|_||_/_/\_\
#                   |_|         |___/
#
# References:
#
# * https://www.nginx.com/resources/wiki/start/topics/tutorials/install/
# * https://www.svennd.be/gpg-nginx-signatures-verified-public-key-not-available/
#

echo "Setup NginX"

release=`lsb_release -a 2>/dev/null | grep "Codename" | awk '{print $2}'`

sources_location="/etc/apt/sources.list.d/nginx.list"
temporary_location="./nginx.list"  # Where script is run
echo "Current release: ${release}"

rm -f ${temporary_location}
#
# Locations from nginx's installation link
#
echo "deb https://nginx.org/packages/ubuntu/ ${release} nginx" >> ${temporary_location}
echo "deb-src https://nginx.org/packages/ubuntu/ ${release} nginx" >> ${temporary_location}
echo "" >> ${temporary_location}

sudo mv ${temporary_location} ${sources_location}
sudo chown root: ${sources_location}

ls -lah ${sources_location}

#
# Add signing key from Sven's blog, installation instructions cannot be scripted,
# eg: "if there is an error copy 'n' paste this."
#
curl -O http://nginx.org/packages/keys/nginx_signing.key
cat nginx_signing.key | sudo apt-key add -
rm nginx_signing.key

sudo DEBIAN_FRONTEND=noninteractive apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nginx

echo ""
echo "Version of NginX installed:"
echo ""
nginx -v