#!/bin/bash

# mod_ssl.SlackBuild
# Build and package mod_ssl on Slackware.
# by:  David Cantrell <david@slackware.com>
# Currently maintained by:  PJV <volkerdi@slackware.com>
# Modified for ARMedslack by Stuart Winter <stuart@armedslack.org>
# 24-Oct-2004

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

# The version of Apache will need to be changed with newer builds:
MODSSL_VER=$VERSION
APACHE_VER=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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Explode package framework:
( cd $PKG ; explodepkg $CWD/_mod_ssl.tar.gz )
# Install sample config file:
cat $CWD/mod_ssl.conf.example > $PKG/etc/apache/mod_ssl.conf.new

# Extract sources:
tar zxvvf $CWD/../apache/apache_$APACHE_VER.tar.gz
tar zxvvf $CWD/$PACKAGE-$MODSSL_VER-$APACHE_VER.tar.gz
slackhousekeeping

# Build mod_ssl:
cd $PACKAGE-*
./configure \
   --with-apxs=/usr/sbin/apxs \
   --with-crt=/etc/apache/mod_ssl/server.crt \
   --with-key=/etc/apache/mod_ssl/server.key \
   --with-patch=/usr/bin/patch || failconfig

# Build:
echo "SSL_CFLAGS= -DSSL_ENGINE -DSSL_USE_SDBM" >> pkg.sslmod/Makefile
make CC="gcc $SLKCFLAGS" || failmake

# go back to the Apache tree and generate the additional package components
cd $TMPBUILD/apache_$APACHE_VER
cat $TMPBUILD/mod_ssl-$MODSSL_VER-$APACHE_VER/pkg.sslcfg/sslcfg.patch | patch -p0
cat $TMPBUILD/mod_ssl-$MODSSL_VER-$APACHE_VER/pkg.ssldoc/ssldoc.patch | patch -p0
cat $TMPBUILD/mod_ssl-$MODSSL_VER-$APACHE_VER/pkg.sslsup/sslsup.patch | patch -p0
zcat $CWD/../apache/apache.dbm.diff.gz | patch -p1
( cd $TMPBUILD/apache_$APACHE_VER/src/support
  sed -i -e "s|PIDFILE=/usr/local/apache/logs/httpd.pid|PIDFILE=/var/run/httpd.pid|g" \
         -e "s|HTTPD='/usr/local/apache/src/httpd'|HTTPD=/usr/sbin/httpd|g" apachectl )

# Install mod_ssl:
cd $TMPBUILD/$PACKAGE-*
make || failmake
make install

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$MODSSL_VER-$APACHE_VER
cp -a ANNOUNCE CREDITS INSTALL LICENSE NEWS README* \
      $PKG/usr/doc/$PACKAGE-$MODSSL_VER-$APACHE_VER

# Install the other components for this package:
cd $TMPBUILD/$PACKAGE-$MODSSL_VER-$APACHE_VER
( cd pkg.sslcfg
  cp -a README.CRT Makefile.crt ca-bundle.crt snakeoil-ca-rsa.crt \
     snakeoil-ca-dsa.crt snakeoil-rsa.crt snakeoil-dsa.crt \
     server.crt $PKG/etc/apache/ssl.crt )
( cd pkg.sslcfg
  cp -a README.CSR server.csr $PKG/etc/apache/ssl.csr )
( cd pkg.sslcfg
  cp -a README.PRM snakeoil-ca-dsa.prm snakeoil-dsa.prm \
     $PKG/etc/apache/ssl.prm )
( cd pkg.sslcfg
  cp -a Makefile.crl README.CRL $PKG/etc/apache/ssl.crl )
( cd pkg.sslcfg
  cp -a README.KEY snakeoil-ca-rsa.key snakeoil-ca-dsa.key snakeoil-rsa.key \
     snakeoil-dsa.key server.key $PKG/etc/apache/ssl.key )

( cd pkg.ssldoc
  cp -a index.html ssl_* $PKG/var/www/htdocs/manual/mod/mod_ssl )
( cd pkg.ssldoc
  cp -a apache_pb.gif feather.jpg mod_ssl_sb.gif openssl_ics.gif \
     $PKG/var/www/htdocs/manual/images )

cd $TMPBUILD/apache_$APACHE_VER
( cd htdocs
  cp -a index.html.en $PKG/var/www/htdocs )
( cd htdocs/manual/mod
  cp -a index.html.en index-bytype.html.en directives.html.en \
     $PKG/var/www/htdocs/manual/mod )
( cd src/support
  cp -a apachectl $PKG/usr/sbin )
# This is a point of overlap with the apache 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-mod_ssl
  ln -sf apachectl-mod_ssl apachectl )

# Make key files that we wouldn't want overwritten use .new:
for file in \
$PKG/etc/apache/ssl.crt/server.crt \
$PKG/etc/apache/ssl.csr/server.csr \
$PKG/etc/apache/ssl.key/server.key ; do
  mv $file ${file}.new
done

# get the module in the package
mkdir -p $PKG/usr/libexec/apache
cp -a /usr/libexec/apache/libssl.so $PKG/usr/libexec/apache

# attributes
chmod 700 $PKG/etc/apache/ssl.key

# 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
