#!/bin/bash
#
#  nginx.SlackBuild is free software; you can redistribute it and/or modify it under the
#  terms of the GNU General Public License as published by the Free Software
#  Foundation; either version 2 of the License, or any later version.
#
#  nginx.SlackBuild is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#

# default settings
PACKAGE="nginx"
PACKAGE_EXT="gz"
VERSION=${VERSION:=1.24.0}
BUILD=${BUILD:=1}

if [ -f ~/.slackbuildrc ]; then
  . ~/.slackbuildrc
elif [ -f ../../runtime/slackbuildrc ]; then
  . ../../runtime/slackbuildrc
fi

check_required

SRC="$PACKAGE-$VERSION.tar.$PACKAGE_EXT"
URL="http://nginx.org/download/$SRC"

get_source $SRC $URL
unpack_source $SRC $PACKAGE-$VERSION

_cfgdir=/etc/nginx
_tmpdir=/var/tmp

CFLAGS=$SLKCFLAGS \
CXXFLAGS="$SLKCFLAGS" \
  ./configure \
  --prefix=/usr \
  --conf-path=$_cfgdir/nginx.conf \
  --sbin-path=/usr/sbin/nginx \
  --pid-path=/run/nginx.pid \
  --lock-path=/var/lock/nginx.lock \
  --user=apache \
  --group=apache \
  --http-log-path=/var/log/nginx/access.log \
  --error-log-path=/var/log/nginx/error.log \
  --with-file-aio \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_addition_module \
  --with-http_sub_module \
  --with-http_dav_module \
  --with-http_flv_module \
  --with-http_mp4_module \
  --with-http_gunzip_module \
  --with-http_gzip_static_module \
  --with-http_auth_request_module \
  --with-http_secure_link_module \
  --with-http_degradation_module \
  --with-http_slice_module \
  --with-http_stub_status_module \
  --http-client-body-temp-path=$_tmpdir/nginx_client_body_temp \
  --http-proxy-temp-path=$_tmpdir/nginx_proxy_temp \
  --http-fastcgi-temp-path=/dev/shm \
  --http-scgi-temp-path=$_tmpdir/nginx_scgi \
  --http-uwsgi-temp-path=$_tmpdir/nginx_uwsgi \
  --without-mail_pop3_module \
  --without-mail_imap_module \
  --without-mail_smtp_module || exit 1

#  --with-http_xslt_module \
#  --with-http_image_filter_module \
#--with-http_random_index_module \
# --with-http_geoip_module \

make $NUMJOBS || exit 2
make DESTDIR=$PKG install

# DOCS
mkdir -p $DOCS
cp -a CHANGES* LICENSE README $DOCS

# Add init script, use apache's httpd script name on Slackware.
mkdir -p $PKG/etc/rc.d 
install -m 644 $CWD/rc.nginx $PKG/etc/rc.d/rc.httpd.new

# Don't clobber config files
for file in fastcgi_params fastcgi.conf mime.types nginx.conf \
	    koi-utf koi-win scgi_params uwsgi_params win-utf ; do
            mv $PKG/etc/nginx/$file $PKG/etc/nginx/$file.new ;
done
rm -f $PKG/etc/nginx/*.default

mkdir -p $PKG/etc/logrotate.d
cat $CWD/nginx.logrotate > $PKG/etc/logrotate.d/nginx.new

# Create the log dir and set the permissions
mkdir -p $PKG/var/log/nginx
chown apache $PKG/var/log/nginx
chmod 750 $PKG/var/log/nginx

# Install vim support
mkdir -p $PKG/usr/share/vim/vim82/indent/
cat contrib/vim/indent/nginx.vim > $PKG/usr/share/vim/vim82/indent/nginx.vim
mkdir -p $PKG/usr/share/vim/vim82/syntax/
cat contrib/vim/syntax/nginx.vim > $PKG/usr/share/vim/vim82/syntax/nginx.vim

# Remove some other empty and/or unnecessary directories from the package
rm -rf $PKG/usr/html

fixup_package $PKG

# Clean up the extra stuff:
set +u
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PACKAGE-$VERSION
  rm -rf $PKG
fi