#!/bin/bash

# Build and package an EAPI-enabled Apache on Slackware.
# by:  David Cantrell <david@slackware.com>
# Maintained by:  Patrick Volkerding <volkerdi@slackware.com>
# Modified for ARMedslack by Stuart Winter <stuart@armedslack.org>
# 24-Oct-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Versions of included stuff - these will need to be modified
# with new releases.
APACHE_VER=$VERSION
MODSSL_VER=2.8.28-1.3.37

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Explode the package framework and some Slackware scripts:
( cd $PKG && explodepkg $CWD/_apache.tar.gz )

# Extract sources:
tar zxvvf $CWD/mm-$MM_VER.tar.gz
tar zxvvf $CWD/apache_$APACHE_VER.tar.gz
tar zxvvf $CWD/../mod_ssl/mod_ssl-$MODSSL_VER.tar.gz
slackhousekeeping

############################################################

# EAPI is not part of Apache by default, it comes from mod_ssl.  It
# allows us to build and package mod_ssl seperately.  We pull out the
# EAPI patch and apply it to Apache so that our build will have that
# support enabled.  We also apply a few other patches from mod_ssl.
( cd $TMPBUILD/apache_$APACHE_VER
  cat $TMPBUILD/mod_ssl-$MODSSL_VER/pkg.eapi/eapi.patch | patch -p0
  cat $TMPBUILD/mod_ssl-$MODSSL_VER/pkg.addon/addon.patch | patch -p0 )
( cd $TMPBUILD/mod_ssl-$MODSSL_VER/pkg.eapi
  cp -a *.h $TMPBUILD/apache_$APACHE_VER/src/include
  cp -a *.c $TMPBUILD/apache_$APACHE_VER/src/ap )
( cd $TMPBUILD/mod_ssl-$MODSSL_VER/pkg.addon
  cp -a *.c $TMPBUILD/apache_$APACHE_VER/src/modules/extra
  cp -a *.html $TMPBUILD/apache_$APACHE_VER/htdocs/manual/mod )

############ Build Apache ###################################

cd $TMPBUILD/apache_$APACHE_VER

# Stop using old obsolete DB1.
zcat $CWD/apache.dbm.diff.gz | patch -p1 --verbose || failpatch

# Add the standard Slackware Apache layout:
cat $CWD/config.layout.slack >> config.layout

# Configure:
EAPI=SYSTEM \
./configure \
   --with-layout=Slackware \
   --enable-module=most \
   --enable-shared=max \
   --manualdir=/var/www/htdocs/manual \
   --enable-rule=eapi || failconfig

# Build:
make || failmake

############ Install Apache ###################################

cd $TMPBUILD/apache_$APACHE_VER
make install root=$PKG
mkdir -p $PKG/usr/doc/$PACKAGE-$APACHE_VER
cp -a ABOUT_APACHE Announcement INSTALL LICENSE README README.configure \
      $PKG/usr/doc/$PACKAGE-$APACHE_VER
cp -a $TMPBUILD/mod_ssl-$MODSSL_VER/pkg.eapi/README.EAPI \
      $PKG/usr/doc/$PACKAGE-$APACHE_VER

# we do not want these in the package, if they don't exist on the system
# then we copy the default one in place as the real file
( cd $PKG/etc/apache
  rm -rf access.conf httpd.conf magic mime.types srm.conf )

# we add a block to the end of httpd.conf and touch zero length files
# for the SSL and PHP module packages
cat << EOF >> $PKG/etc/apache/httpd.conf.default

# By default, all external Apache modules are disabled.  To enable a particular
# module for Apache, make sure the necessary packages are installed.  Then
# uncomment the appropriate Include line below, save the file, and restart
# Apache.  Note that some modules may need additional configuration steps.  For
# example, mod_ssl requires a site certificate which you may need to generate.
#
# Lastly, if you remove a module package, be sure to edit this file and comment
# out the appropriate Include line.

# ==> mod_php configuration settings <==
#
# PACKAGES REQUIRED:  openssl-solibs (A series) and/or openssl (N series),
#                     mysql (AP series), gmp (L series), mhash (L series),
#                     and apache (N series)
#
#Include /etc/apache/mod_php.conf

# ==> mod_ssl configuration settings <==
#
# PACKAGES REQUIRED:  apache (N series) and openssl (N series)
#
#Include /etc/apache/mod_ssl.conf

EOF

# This is a point of overlap with the mod_ssl package, so we'll make it
# a symlink so that it's less of a trap for the unsuspecting admin:
( cd $PKG/usr/sbin
  mv apachectl apachectl-standard
  # Better to use some custom code in doinst.sh.
  #ln -sf apachectl-standard apachectl
)

############################################################

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
