#!/bin/sh
#
# Build and package mod_php on Slackware.
# by:  David Cantrell <david@slackware.com>
# Modified for PHP 4.1.x+ by volkerdi@slackware.com
CWD=`pwd`
TMP=/tmp

GD_VER=1.8.4
PHP_VER=4.2.1

# compile a static gd that we can link into php
cd $TMP
rm -rf gd-$GD_VER
tar xvzf $CWD/gd-$GD_VER.tar.gz
cd 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

# Next, we need to compile Pine to get c-client.a for IMAP support:
if [ ! -d /tmp/pine4.44 ]; then
  ( cd $CWD/../pine ; ./SlackBuild )
  ( cd /tmp/pine4.44/imap/c-client
    mkdir include
    cp *.h include
    mkdir lib
    cp *.a lib )
fi

php_configure() {
CFLAGS="-O2 -march=i386 -mcpu=i686" ./configure --prefix=/usr \
            $* \
            --enable-discard-path \
            --with-config-file-path=/etc/apache \
            --enable-safe-mode \
            --with-openssl \
            --enable-bcmath \
            --with-bz2 \
            --enable-pic \
            --enable-calendar \
            --enable-ctype \
            --with-gdbm \
            --with-db2 \
            --with-db3 \
  --with-imap-ssl=/tmp/pine4.44/imap/c-client \
  --with-imap=/tmp/pine4.44/imap/c-client \
            --enable-dbase \
            --enable-ftp \
            --enable-gd-imgstrttf \
            --with-gd=$TMP/gd-$GD_VER \
            --with-jpeg-dir=$TMP/gd-$GD_VER \
            --with-png-dir=$TMP/gd-$GD_VER \
            --with-gmp \
            --with-mysql=/usr \
            --with-xml=shared \
            --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-mod_charset    # only for Russian patched Apache
            # --with-readline=/usr  # this is only for the CGI version
            # --with-ttf            # this links with the shlib, need X for that
            # --with-java           # no thanks
            # --with-dom            # requires libxml >= 2.2.7
}

# compile php
cd $TMP
tar xjvf $CWD/php-$PHP_VER.tar.bz2
cd php-$PHP_VER
# Fixup perms/owners:
chown -R root.root .
find . -perm 775 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;
find . -name "*.h" -exec chmod 644 {} \;
# Make the standalone interpreter:
php_configure --enable-force-cgi-redirect
make
cat php > /usr/bin/php
chmod 755 /usr/bin/php
chown root.bin /usr/bin/php
make distclean
# Make the Apache modules:
php_configure --disable-static --with-apxs=/usr/sbin/apxs
make
make install

mkdir -p /usr/doc/php-$PHP_VER
cp -a \
  CODING_STANDARDS CREDITS EXTENSIONS INSTALL LICENSE NEWS README* RELEASE_PROCESS TODO* *.txt \
  /usr/doc/php-$PHP_VER
chown -R root.root /usr/doc/php-$PHP_VER

mkdir -p /etc/apache
cp -a php.ini-dist php.ini-recommended /etc/apache
cp -a $CWD/mod_php.conf.example /etc/apache
chmod 644 /etc/apache/*
chown root.root /etc/apache/*

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