#!/bin/bash
#
# Slackware build script for nginx
#
# Copyright 2017-2026 Corrado Franco (https://corradofranco.it)
# All rights reserved.
#
# This program 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
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.

# This program 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. Compiling,
# interpreting, executing or merely reading the text of the program
# may result in lapses of consciousness and/or very being, up to and
# including the end of all existence and the Universe as we know it.
# See the GNU General Public License for more details.

# You may have received a copy of the GNU General Public License along
# with this program (most likely, a file named COPYING).  If not, see
# <http://www.gnu.org/licenses/>.
#
# Latest version of this SlackBuild at https://github.com/conraid/SlackBuilds

# Based on https://slackbuilds.org/slackbuilds/14.2/network/nginx/nginx.SlackBuild

set -euo pipefail

cd $(dirname $0) ; CWD=$(pwd)

PKGNAM=nginx
VERSION=1.29.5
BUILD=${BUILD:-1}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}

DOCFILES="CHANGES* LICENSE README"

if [ -z ${ARCH:=""} ]; then
  case "$( uname -m )" in
    i?86) ARCH=i686 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME:-""}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-${BUILD}${TAG}.txz"
  exit 0
fi

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=pentium4 -mtune=generic"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86-64 -mtune=generic -fPIC -pipe -fomit-frame-pointer"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PKGNAM-$VERSION
tar xvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION

chown -R root:root .
find -L . \
  \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;

# Remove -Werror from CFLAGS.
# https://trac.nginx.org/nginx/ticket/1259
sed -i '/-Werror/d' auto/cc/gcc

# Fix the nginx.conf file for html and virtual server directory.
sed -i -e '/root[ ]*html/s|html;|/var/www/&|' \
       -e '$s|.*|    include /etc/nginx/conf.d/\*.conf;\n&|' \
       -e 's|^events.*|include /etc/nginx/modules/\*.conf;\n\n&|' \
       conf/$PKGNAM.conf

./configure \
  --with-cc-opt="$SLKCFLAGS" \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --pid-path=/run/nginx.pid \
  --lock-path=/var/lock/subsys \
  --modules-path=/usr/lib${LIBDIRSUFFIX}/nginx/modules \
  --user=nobody \
  --group=nogroup \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --http-client-body-temp-path=/var/lib/nginx/client_body \
  --http-proxy-temp-path=/var/lib/nginx/proxy \
  --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
  --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
  --http-scgi-temp-path=/var/lib/nginx/scgi \
  --with-file-aio \
  --with-select_module \
  --with-poll_module \
  --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_random_index_module \
  --with-http_secure_link_module \
  --with-http_degradation_module \
  --with-http_slice_module \
  --with-http_stub_status_module \
  --with-mail=dynamic \
  --with-mail_ssl_module \
  --with-stream_ssl_module \
  --with-stream_realip_module \
  --with-stream_ssl_preread_module \
  --with-cpp_test_module \
  --with-compat \
  --with-pcre \
  --with-pcre-jit \
  --with-libatomic \
  --with-http_geoip_module=dynamic \
  --with-stream_geoip_module=dynamic \
  --with-http_perl_module=dynamic \
  --with-http_image_filter_module=dynamic \
  --with-http_xslt_module=dynamic \
  --with-stream=dynamic \
  --with-threads

make
make DESTDIR=$PKG INSTALLDIRS=vendor INSTALLVENDORMAN3DIR=/usr/man/man3 install

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
find $PKG | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip -g 2> /dev/null || true


mkdir -p $PKG/usr/man/man8
install -m 0644 objs/$PKGNAM.8 $PKG/usr/man/man8
find $PKG/usr/man -type f -exec gzip -9 {} \;

# Remove perllocal.pod and other special files
find $PKG -name perllocal.pod -o -name ".packlist" -o -name "*.bs" | xargs rm -f || true

find $PKG -perm 444 -exec chmod 0644 {} \;
find $PKG -perm 555 -exec chmod 0755 {} \;

# Make the temp path.
mkdir -p $PKG/var/lib/$PKGNAM
chmod 0700 $PKG/var/lib/$PKGNAM
chown nobody $PKG/var/lib/$PKGNAM

# Move html directory
mkdir -p $PKG/var/www
mv $PKG/usr/html $PKG/var/www

# Add an init script
mkdir -p $PKG/etc/rc.d
cat $CWD/files/rc.nginx > $PKG/etc/rc.d/rc.nginx.new

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

# Set the permissions for the log directory.
chown nobody $PKG/var/log/$PKGNAM
chmod 750 $PKG/var/log/$PKGNAM

# Create sub-directory for virtual servers.
mkdir $PKG/etc/$PKGNAM/conf.d
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION

# Don't clobber config files
for i in $(find $PKG/etc/$PKGNAM -type f ! -name "*\.default"); do
  mv "$i" "$i".new
done

# Install vim files
for i in ftdetect ftplugin indent syntax; do
install -Dm644 contrib/vim/$i/nginx.vim $PKG/usr/share/vim/vimfiles/$i/nginx.vim
done

cp -a $DOCFILES $PKG/usr/doc/$PKGNAM-$VERSION || true
cat $CWD/$PKGNAM.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}
