#!/bin/bash
###============================================================
##      Ubuntu 18.04 Brotli updater
###============================================================
# Make sure to install all Dependencies
#
##=============================================================

#Setting versions variabless
newver=$(apt-show-versions -a nginx:amd64 | grep -o -P '(?<=amd64 ).*(?=~bionic bionic nginx.org)')
curver=$(cat /var/log/nginxVersion.txt)
echo curent=$curver

if [ $newver \> $curver ]; then
  #updating stored versions number
  echo $newver > /var/log/nginxVersion.txt
  # building nginx module
    #source Nginx
    cd /usr/local/src
    apt source nginx
    apt build-dep nginx -y

    # building nginx
    cd /usr/local/src/nginx-*/
    sed -i -e 's/ssl_preread_module --with-c/ssl_preread_module --add-module=\/usr\/local\/src\/ngx_brotli --with-c/g' debian/rules
    sudo dpkg-buildpackage -b -uc -us
    #DEB files are in /usr/local/src
    mv *.deb /var/www/brotli-builds
    rm -rf /usr/local/src/nginx-*/
fi
