IGNOREPATH=/tmp:/dev:/root:/var:/etc:/home:/sbin:/bin:/usr/local:/usr/src
STRIPLIB=y
STRIPBIN=y

PHP_VER=4.3.11
PINE=4.58
PROGNAME="PHP $PHP_VER"
DESC="\
php (HTML-embedded scripting language)                                 \n\
                                                                       \n\
PHP is an HTML-embedded scripting language.  It shares syntax          \n\
characteristics with C, Java, and Perl.  The primary objective         \n\
behind this language is to make a fast and easy-to-use scripting       \n\
language for dynamic web sites.                                        \n\
                                                                       \n\
More information can be found online at http://www.php.net/"

BUILD=2
MAINTAINER="Mark Post <mark.post@eds.com>"
SOURCE=http://www.php.net
PKGNAME=php-$PHP_VER-s390-$BUILD
TAG=OPT

compile() {
   # we need to compile Pine to get c-client.a for IMAP support:
   IMAPLIBDIR=/usr/local/lib/c-client
#  if [ ! -f $TMP/pine${PINE}/imap/c-client/lib/c-client.a ]; then
   if [ -r $IMAPLIBDIR/lib/c-client.a ]; then
       echo "Using IMAP library:"
       ls -l $IMAPLIBDIR/lib/c-client.a
       sleep 5
   else
     ( cd $TMP
       if echo $CWD | grep patches >/dev/null; then
         CWD=$CWD/../pine
         else CWD=$CWD/../../../source/n/pine
       fi
       source $CWD/prototype
       compile
       cd $TMP/pine${PINE}/imap/c-client
       strip -g c-client.a
       mkdir -p $IMAPLIBDIR/lib
       cp c-client.a $IMAPLIBDIR/lib
       mkdir -p $IMAPLIBDIR/include
       cp *.h $IMAPLIBDIR/include
#      mkdir include
#      cp *.h include
#      mkdir lib
#      cp *.a lib
     )
   fi

php_configure() {
EXTENSION_DIR=/usr/lib/php/extensions \
CFLAGS=-O2 ./configure --prefix=/usr \
            $* \
            --sysconfdir=/etc \
            --enable-discard-path \
            --with-config-file-path=/etc/apache \
            --enable-safe-mode \
            --with-openssl \
            --enable-bcmath \
            --with-bz2 \
            --with-pic \
            --enable-calendar \
            --enable-ctype \
            --with-gdbm \
            --with-db3 \
            --with-imap-ssl=$IMAPLIBDIR \
            --with-imap=$IMAPLIBDIR \
            --enable-dbase \
            --enable-ftp \
            --with-iconv \
            --with-gd \
            --enable-gd-native-ttf \
            --with-jpeg-dir=/usr \
            --with-png \
            --with-gmp \
            --with-mysql=shared,/usr \
            --with-xml=shared,/usr \
            --with-gettext=shared,/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-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
            # --with-curl           # no requests for this, so we'll avoid the dependancy
}

   # compile php
   cd $TMP
   tar -yxvf $CWD/php-$PHP_VER.tar.bz2

   # Add missing(?) PEAR modules back:
   ( cd php-$PHP_VER/pear/packages
     cp -a $CWD/pear/* .
     bzip2 -dv *.bz2
   )

   cd php-$PHP_VER/

#  zcat $CWD/php.configure.s390.diff.gz | patch -p1

   # Fixup perms/owners:
   chown -R root.root .
   find . -perm 777 -exec chmod 755 {} \;
   find . -perm 775 -exec chmod 755 {} \;
   find . -perm 666 -exec chmod 644 {} \;
   find . -perm 664 -exec chmod 644 {} \;
   find . -name "*.h" -exec chmod 644 {} \;
   # Sometimes they ship a few of these:
   find . -name "*.orig" -exec rm {} \;

   # Patch ini files:
   zcat $CWD/php.ini.diff.gz | patch -p1

   # Make the standalone interpreter:
   php_configure --enable-force-cgi-redirect --enable-fastcgi --enable-pcntl --enable-sigchild
   make -j4

#  Have to save the php module so that the make distclean doesn't erase it
   mv sapi/cgi/php sapi/cgi/php.save

   make distclean
   # Make the Apache module version of PHP:
   php_configure --disable-static --with-apxs=/usr/sbin/apxs
   make -j4

#  # libtool can't seem to figure out how to link in a static library when making
#  # a shared library any more.  So, we need to do it manually.
#  if [ ! -f .libs/libphp4.so ]; then
#    sh $CWD/link.libphp4
#    ls -la .libs
#    cat $CWD/libtool.diff | patch -p0
#    cp -vp .libs/libphp4* libs/
#  fi
}

install() {
   cd $TMP/php-$PHP_VER/
   # This next step is needed to keep the "make install" from complaining about
   # /package-php/etc/apache/httpd.conf not existing.  It gets removed right after that,
   # along with the .bak file that gets created.
   mkdir -p /package-php/etc/apache/
   cat $CWD/dummy.httpd.conf > /package-php/etc/apache/httpd.conf

   make install INSTALL_ROOT=/package-php
   cp -p sapi/cgi/php.save /package-php/usr/bin/php

   rm /package-php/etc/apache/httpd.conf*

   mkdir -p /package-php/etc/apache
   cp -a php.ini-dist php.ini-recommended /package-php/etc/apache/
   cp -a $CWD/mod_php.conf.example /package-php/etc/apache/
#  ( cd /package-php/etc/apache/
#    cat $CWD/mod_php.conf.example.diff | patch -p0
#  )

   mkdir -p /package-php/usr/doc/php-$PHP_VER
   cp -a CODING_STANDARDS CREDITS EXTENSIONS INSTALL LICENSE \
         NEWS README* TODO* sapi/cgi/README.FastCGI \
       /package-php/usr/doc/php-$PHP_VER

   gzip -9 /package-php/usr/man/man1/*.1
}

attributes() {
   chown -R root.bin $PKG/package-php/usr/bin
}

special() {
   cd $PKG
   mv package-php/* ./
   rmdir package-php/

   cd $CTL
   cat $CWD/slack-desc > slack-desc

   zcat $CWD/doinst.sh.gz > doinst.sh
   sed -e 's%package-php/%%g' doinst.sh > doinst.sh.new
   cat doinst.sh.new > doinst.sh
   rm doinst.sh.new
}

