# Copyright 2010-2023 Avouch Authors
# Distributed under the terms of the GNU General Public License v2
# Maintainer: Qurban Ullah <qurbanullah@avouch.org>
# Contributor: Qurban Ullah <qurbanullah@avouch.org>

pkgname=apache
_realname=httpd
pkgver=2.4.58
pkgrel=1
pkgdesc='A high performance Unix-based HTTP server'
arch=('x86_64')
groups=('www-servers')
url='https://www.apache.org/dist/httpd'
license=('APACHE')
backup=(
    etc/httpd/conf/httpd.conf
    etc/httpd/conf/extra/httpd-{autoindex,dav,default,info,languages}.conf
    etc/httpd/conf/extra/httpd-{manual,mpm,multilang-errordoc}.conf
    etc/httpd/conf/extra/httpd-{ssl,userdir,vhosts}.conf
    etc/httpd/conf/extra/proxy-html.conf
    etc/httpd/conf/{mime.types,magic}
    etc/logrotate.d/httpd
)
provides=('mod_proxy_uwsgi')
depends=('zlib' 'apr-util' 'pcre' 'libnghttp2' 'openssl' 'libxcrypt')
makedepends=('libxml2' 'lua' 'curl' 'brotli' 'jansson')
optdepends=(
    'lua: for mod_lua module'
    'libxml2: for mod_proxy_html, mod_xml2enc modules'
    'curl: for mod_md module'
    'jansson: for mod_md module'
    'brotli: for mod_brotli module'
    'uwsgi: for mod_proxy_uwsgi module'
    'lynx: apachectl status'
    'perl: for apxs and dbmmanage'
)
source=(
    https://www.apache.org/dist/httpd/httpd-${pkgver}.tar.bz2{,.asc}
    openssl-malloc-init.patch
    apache.tmpfiles.conf
    httpd.logrotate
    httpd.service
    avouch.layout
)

prepare() {
  cd httpd-${pkgver}

  # set default user
  sed -e 's#User daemon#User http#' \
      -e 's#Group daemon#Group http#' \
      -i docs/conf/httpd.conf.in

  cat "${srcdir}/avouch.layout" >> config.layout

  # https://github.com/openssl/openssl/issues/2865
  patch -Np1 -i ../openssl-malloc-init.patch
}

build() {
  cd httpd-${pkgver}

  ./configure --sbindir=/usr/bin \
      --enable-layout=Avouch \
      --enable-mpms-shared=all \
      --enable-modules=all \
      --enable-mods-shared=all \
      --enable-so \
      --enable-suexec \
      --with-suexec-caller=http \
      --with-suexec-docroot=/srv/http \
      --with-suexec-logfile=/var/log/httpd/suexec.log \
      --with-suexec-bin=/usr/bin/suexec \
      --with-suexec-uidmin=99 --with-suexec-gidmin=99 \
      --enable-ldap --enable-authnz-ldap --enable-authnz-fcgi \
      --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache \
      --enable-ssl --with-ssl \
      --enable-deflate --enable-cgi --enable-cgid \
      --enable-proxy --enable-proxy-connect \
      --enable-proxy-http --enable-proxy-ftp \
      --enable-dbd --enable-imagemap --enable-ident --enable-cern-meta \
      --enable-lua --enable-xml2enc --enable-http2 \
      --enable-proxy-http2 --enable-md --enable-brotli \
      --with-apr=/usr/bin/apr-1-config \
      --with-apr-util=/usr/bin/apu-1-config \
      --with-pcre=/usr

  make
}

package() {
  cd httpd-${pkgver}

  make DESTDIR="${pkgdir}" install
   
  install -D -m644 "${srcdir}/httpd.logrotate" "${pkgdir}/etc/logrotate.d/httpd"
  install -D -m644 "${srcdir}/apache.tmpfiles.conf" "${pkgdir}/usr/lib/tmpfiles.d/apache.conf"
  install -D -m644 "${srcdir}/httpd.service" "${pkgdir}/usr/lib/systemd/system/httpd.service"	
  install -D -m644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"

  # symlinks for /etc/httpd
  # do we really need this symlink?
  ln -fs /usr/lib/httpd/modules "${pkgdir}/etc/httpd/modules"

  # set sane defaults
  sed -e 's#/usr/lib/httpd/modules/#modules/#' \
      -e 's|#\(LoadModule negotiation_module \)|\1|' \
      -e 's|#\(LoadModule include_module \)|\1|' \
      -e 's|#\(LoadModule userdir_module \)|\1|' \
      -e 's|#\(LoadModule slotmem_shm_module \)|\1|' \
      -e 's|#\(Include conf/extra/httpd-multilang-errordoc.conf\)|\1|' \
      -e 's|#\(Include conf/extra/httpd-autoindex.conf\)|\1|' \
      -e 's|#\(Include conf/extra/httpd-languages.conf\)|\1|' \
      -e 's|#\(Include conf/extra/httpd-userdir.conf\)|\1|' \
      -e 's|#\(Include conf/extra/httpd-default.conf\)|\1|' \
      -e 's|#\(Include conf/extra/httpd-mpm.conf\)|\1|' \
      -i "${pkgdir}/etc/httpd/conf/httpd.conf"

  # cleanup
  rm -r "${pkgdir}/usr/share/httpd/manual"
  rm -r "${pkgdir}/etc/httpd/conf/original"
  rm -r "${pkgdir}/srv/"
  rm -r "${pkgdir}/run"
}