#! /bin/ksh -x

[ $# -eq 1 ] || {
  echo "Usage: OSR5BUILD /path/to/xenv"
  exit 1
}

if [ "$1" = "/" ] ; then
  OSDAPX=/usr/lib/apache/bin/apxs
else
  OSDAPX=$1/bin/osdapxsinc
fi

rm -fr tmpmsql
zcat ${apachesrcdir:-..}${apachesrcdir:+/php5}/mysqllibs.osr5.tar.Z | tar xf -
msqllibdir=`pwd`/tmpmsql/usr

: ${GWXCC:='gcc -L/usr/gnu/lib'}
: ${GWXCXX:='g++ -L/usr/gnu/lib'}
: ${GWXCFLAGS:="-O6 -fomit-frame-pointer -mcpu=i586 -march=i586"}
: ${GWXCXXFLAGS:="-O6 -fomit-frame-pointer -mcpu=i586 -march=i586"}
: ${GWXSHELL:=/usr/bin/bash}
GWXCFLAGS="$GWXCFLAGS -D_REENTRANT -DFD_SETSIZE=11000 -I${GWXXENVDIR}/usr/include/gd2 -I${GWXXENVDIR}/usr/include/sasl"
GWXCXXFLAGS="$GWXCXXFLAGS -D_REENTRANT -DFD_SETSIZE=11000 -I${GWXXENVDIR}/usr/include/gd2 -I${GWXXENVDIR}/usr/include/sasl"
SHELL="${GWXSHELL}" CONFIG_SHELL="${GWXSHELL}" CC="${GWXCC}" CXX="${GWXCXX}" \
CFLAGS="${GWXCFLAGS}" CXXFLAGS="${GWXCXXFLAGS}" \
${GWXSHELL} ${apachesrcdir:-..}${apachesrcdir:+/php5}/configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --datadir=/usr/lib \
  --infodir=/usr/gnu/info \
  --with-apxs=$OSDAPX \
  --with-exec-dir=/usr/lib/php/bin \
  --with-config-file-path=/usr/lib/php \
  --with-config-file-scan-dir=/usr/lib/php/cf.d \
  --enable-sigchild \
  --enable-fastcgi \
  --enable-safe-mode \
  --disable-ipv6 \
  --with-png-dir=${GWXXENVDIR}/usr \
  --with-xpm-dir=${GWXXENVDIR}/usr/X11R6 \
  --with-zlib-dir=${GWXXENVDIR}/usr \
  --with-jpeg-dir=${GWXXENVDIR}/usr \
  --with-tiff-dir=${GWXXENVDIR}/usr \
  --with-freetype-dir=${GWXXENVDIR}/usr \
  --with-ttf-dir=${GWXXENVDIR}/usr \
  --with-expat-dir=${GWXXENVDIR}/usr \
  --with-pcre-regex=${GWXXENVDIR}/usr \
  --with-pcre-dir=${GWXXENVDIR}/usr \
  --with-libxml-dir=${GWXXENVDIR}/usr \
  --with-expat-dir=${GWXXENVDIR}/usr \
  --with-openssl-dir=${GWXXENVDIR}/usr \
  --with-openssl=shared,${GWXXENVDIR}/usr \
  --with-zlib=shared,${GWXXENVDIR}/usr \
  --with-bz2=shared,${GWXXENVDIR}/usr \
  --enable-calendar=shared \
  --with-curl=shared,${GWXXENVDIR}/usr \
  --enable-dba=shared \
    --with-gdbm=${GWXXENVDIR}/usr \
    --with-ndbm=yes \
    --with-db4=${GWXXENVDIR}/usr \
    --with-db1=${GWXXENVDIR}/usr \
    --with-inifile=yes \
    --with-flatfile=yes \
    --with-dbm=yes \
  --enable-dbase=shared \
  --enable-exif=shared \
  --enable-ftp=shared \
  --with-gd=shared,${GWXXENVDIR}/usr \
    --with-ttf=shared,${GWXXENVDIR}/usr \
  --enable-gd-native-ttf \
  --with-gettext=shared,${GWXXENVDIR}/usr/gnu \
  --with-iconv=shared \
  --with-ldap=shared,${GWXXENVDIR}/usr \
  --with-ldap-sasl=${GWXXENVDIR}/usr \
  --enable-mbstring=shared \
  --with-mysql=shared,$msqllibdir \
  --with-mysqli=shared,$msqllibdir/bin/mysql_config \
  --with-mysql-sock=/tmp/mysql.sock \
  --with-ncurses=shared \
  --with-pdo=shared \
    --with-pdo-mysql=shared,$msqllibdir \
    --with-pdo-pgsql=shared \
    --with-pdo-sqlite=shared \
  --with-pgsql=shared,/usr \
  --with-readline=shared,${GWXXENVDIR}/usr \
  --with-mm=shared,${GWXXENVDIR}/usr \
  --enable-simplexml \
  --enable-soap=shared \
  --enable-sockets=shared \
  --with-sqlite=shared \
    --enable-sqlite-utf8 \
  --enable-sysvmsg=shared \
  --enable-sysvsem=shared \
  --enable-sysvshm=shared \
  --enable-tokenizer=shared \
  --enable-wddx=shared \
  --with-xsl=shared,${GWXXENVDIR}/usr \
  --enable-magic-quotes \
  --with-regex=php \
  --host=i586-pc-sco3.2v5.0 || exit $?

#
# we need to adjust build-defs.h so that it refers to the real apxs
# not our wrapper.
#
[ "$1" = "/" ] || {
  sed -e "s,$OSDAPX,/usr/lib/apache/bin/apxs," main/build-defs.h > /tmp/$$.sed
  cp -f /tmp/$$.sed main/build-defs.h
  rm -f /tmp/$$.sed
}
exit 0
