#!/bin/sh
#
# Build and package mod_php on Slackware.
# by:  David Cantrell <david@slackware.com>
#

GD_VER=1.8.2
PHP_VER=4.0.5
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-mod_php

rm -rf $PKG
mkdir -p $PKG
( cd $PKG ; explodepkg $CWD/_mod_php.tar.gz )

cd $TMP
tar xvzf $CWD/gd-$GD_VER.tar.gz
tar xvzf $CWD/php-$PHP_VER.tar.gz

# compile a static gd that we can link into php
cd $TMP/gd-$GD_VER
make
mkdir lib
mkdir bin
mkdir include
cp libgd.a lib
cp gd.h gdcache.h gd_io.h gdfontg.h gdfontmb.h gdfonts.h gdfontt.h include
cp pngtogd pngtogd2 gdtopng gd2topng gd2copypal gdparttopng webpng \
   bdftogd bin

# compile php
cd $TMP/php-$PHP_VER
./configure --prefix=/usr \
            --with-apxs=/usr/sbin/apxs \
            --with-mod_charset \
            --enable-force-cgi-redirect \
            --enable-discard-path \
            --with-config-file-path=/etc/apache \
            --enable-safe-mode \
            --with-openssl \
            --enable-bcmath \
            --with-bz2 \
            --enable-calendar \
            --enable-ctype \
            --with-gdbm \
            --with-db2 \
            --with-db3 \
            --enable-dbase \
            --enable-ftp \
            --enable-gd-imgstrttf \
            --with-gd=$TMP/gd-$GD_VER \
            --with-jpeg-dir=$TMP/gd-$GD_VER \
            --with-gmp \
            --with-mysql=/usr \
            --with-xml=shared \
            --with-readline=/usr \
            --with-mm=/usr \
            --enable-trans-sid \
            --enable-shmop \
            --enable-sockets \
            --with-regex=php \
            --enable-sysvsem \
            --enable-sysvshm \
            --enable-yp \
            --enable-memory-limit \
            --with-tsrm-pthreads \
            --enable-shared \
            --disable-debug \
            --with-zlib=/usr
            # --with-ttf    # this links with the shlib, need X for that
            # --with-java
            # --with-dom    # requires libxml >= 2.2.7
make

# install php
make install
( cd /usr/bin
  cp -a pear php-config phpextdist phpize $PKG/usr/bin )
( cd /usr/lib/php
  cp -a * $PKG/usr/lib/php )
( cd /usr/include/php
  cp -a * $PKG/usr/include/php )
( cd /usr/libexec
  cp -a libphp4.so $PKG/usr/libexec )
mkdir -p $PKG/usr/doc/php-$PHP_VER
cp -a CODING_STANDARDS CREDITS EXTENSIONS FUNCTION_LIST.txt INSTALL \
   LICENSE NEWS README* TODO *.txt $PKG/usr/doc/php-$PHP_VER
mkdir -p $PKG/etc/apache
cp -a php.ini-dist php.ini-optimized $PKG/etc/apache

# some housekeeping
chown -R root.root $PKG/usr/doc/*
chown root.bin $PKG/usr/bin/*
chown -R root.root $PKG/usr/include/php/*
chown root.root $PKG/etc/apache/*
strip $PKG/usr/bin/*
strip $PKG/usr/libexec/*

# make the package
cd $PKG
echo "n" | makepkg $TMP/mod_php.tgz

# clean up
if [ "$1" = "--cleanup" ]; then
   cd $CWD
   rm -rf $TMP/gd-$GD_VER
   rm -rf $TMP/php-$PHP_VER
   rm -rf $PKG
fi
