#!/bin/bash

case "`grep DISTRIB_CODENAME /etc/*-release | awk -F '=' '{print $2}'`" in
      bionic)
             if [ "$(whoami)" != "root" ]
             then
                 echo "You should Login as root to use this script!";
                 echo "sudo -i";
                 exit 1
             fi

             if [ -d "/nginx/" ]; then
                 echo "We've detect a folder '/nginx/'"
                 echo "Please to a fresh reinstallation of your server this is a whole new release you can't update!"
                 exit 1
             fi

             if [ -d "/etc/nginx" ]; then
                 echo "We've detect a folder '/etc/nginx' which means"
                 echo "Maybe you have installed nginx on your system!"
                 echo "Please remove everything related to nginx apache or any other web server using port 80/443"
                 exit 1
             fi

             if [ -d "/opt/nginx/" ]; then
                 echo "We've detect a folder '/opt/nginx/' which means"
                 echo "Maybe you have use this script before!"
                 exit 1
             fi

             apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y
             apt-get install build-essential libssl-dev curl nano wget zip unzip git -y

             cd /; wget https://github.com/theraw/The-World-Is-Yours/raw/v2/OS/Ubuntu/18.04/source/nginx16.zip; unzip nginx16.zip; rm -Rf nginx16.zip
             curl -s https://raw.githubusercontent.com/theraw/The-World-Is-Yours/v2/OS/Ubuntu/18.04/nginx.service > /etc/systemd/system/nginx.service
             killall nginx
             systemctl daemon-reload
             sudo systemctl start nginx.service && sudo systemctl enable nginx.service
             service nginx stop
             service nginx start
      ;;
esac
