#!/bin/sh

#Clean up old nginx builds
sudo rm -rf ~/rpmbuild/RPMS/*/nginx-*.rpm

#Install required packages for building
sudo yum groupinstall -y 'Development tools'
sudo yum install -y \
    rpm-build \
    rpmdevtools \
    yum-utils \
#    mercurial \
    git \
    wget


#Install source RPM for Nginx
pushd ~
echo """[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/SRPMS/
gpgcheck=0
enabled=1""" > nginx.repo
sudo mv nginx.repo /etc/yum.repos.d/
yumdownloader --source nginx
sudo rpm -ihv nginx*.src.rpm
popd


#Get various add-on modules for Nginx
#XXX git clone -b [tag] isn't supported on git 1.7 (RHEL 6)
pushd ~/rpmbuild/SOURCES

    #Fancy Index module
    git clone https://github.com/aperezdc/ngx-fancyindex.git
    pushd ngx-fancyindex
    git checkout v0.3.5
    popd

popd

#Prep and patch the Nginx specfile for the RPMs
pushd ~/rpmbuild/SPECS
spectool -g -R nginx.spec
yum-builddep -y nginx.spec
rpmbuild -ba nginx.spec

#Test installation and check output
#sudo yum remove -y nginx nginx-debug
#sudo yum install -y ~/rpmbuild/RPMS/*/nginx-*.rpm
#nginx -V
