#!/bin/sh

# $Id: buildall-with-xml,v 1.53 2012-06-26 22:38:31 moko Exp $

install_directory=$HOME/parser3install
sendmail_command="/usr/sbin/sendmail -i -t -f postmaster"
cflags=""
#cflags="--with-pic" #required for apache module on x64

echo "buildall-with-xml"
echo "Script author: Alexander Petrosian <paf@design.ru> (http://paf.design.ru)"
echo
echo "Building..."

download=`which fetch 2>/dev/null`
if test -f "$download"; then
	download="fetch -p"
else
	download="wget -c --passive-ftp"
fi

parser3_directory=`pwd`
cd ..
project_directory=`pwd`
mkdir src >/dev/null 2>&1

if test ! -f "$project_directory/gc/lib/libgc.a"; then
  cd $project_directory/src
# libgc="gc6.8" # FreeBSD 4.X is not supported in newer gc version
  libgc="gc-7.1"
  if test ! -f "$libgc.tar.gz"; then
    echo "Downloading gc [1 lib of 4]..."
    $download http://www.hpl.hp.com/personal/Hans_Boehm/gc/gc_source/$libgc.tar.gz
  fi
  echo "Unpacking gc..."
  rm -rf $libgc
  gunzip -c $libgc.tar.gz | tar vxf - >/dev/null
  cd $libgc
  echo "Configuring gc..."
  CPPFLAGS="-DUSE_LIBC_PRIVATES -DUSE_MMAP -DDONT_ADD_BYTE_AT_END" \
  ./configure --prefix=$project_directory/gc \
    --disable-threads \
    --disable-shared \
    --silent $cflags
  echo "Building gc..."
  make
  make install
  cd ..
  rm -rf $libgc
fi

if test ! -f "$project_directory/pcre/lib/libpcre.a"; then
  cd $project_directory/src
  libpcre="pcre-8.30"
  if test ! -f "$libpcre.tar.gz"; then
    echo "Downloading pcre [2 lib of 4]..."
    $download ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/$libpcre.tar.gz
  fi
  echo "Unpacking pcre..."
  rm -rf $libpcre
  gunzip -c $libpcre.tar.gz | tar vxf - >/dev/null
  cd $libpcre
  echo "Configuring pcre..."
  ./configure --prefix="$project_directory/pcre" \
    --with-match-limit-recursion=10000 \
    --enable-utf8 \
    --enable-unicode-properties \
    --disable-shared \
    --disable-cpp \
    --disable-pcregrep-libz \
    --disable-pcregrep-libbz2 \
    --silent $cflags
  echo "Building pcre..."
  make install
  cd ..
  rm -rf $libpcre
fi

if test ! -f "$project_directory/gnome/lib/libxml2.a"; then
  cd $project_directory/src
  libxml2="libxml2-2.8.0"
  if test ! -f "$libxml2.tar.gz"; then
    echo "Downloading libxml [3 lib of 4]..."
    $download ftp://xmlsoft.org/libxml2/$libxml2.tar.gz
  fi
  echo "Unpacking libxml2... (be patient)"
  rm -rf $libxml2
  gunzip -c $libxml2.tar.gz | tar vxf - >/dev/null
  cd $libxml2
  #sax1, output, tree, xinclude[in libxslt], html[in libxslt, mode=html?], xptr[xinclude], pattern -- needed!
  echo "Configuring libxml..."
  ./configure --prefix=$project_directory/gnome \
    --without-catalog \
    --without-iconv \
    --without-threads \
    --without-debug \
    --without-iso8859x \
    --without-legacy \
    --without-push \
    --without-python \
    --without-reader \
    --without-writer \
    --without-readline \
    --without-regexps \
    --without-schemas \
    --without-schematron \
    --without-modules \
    --without-ftp \
    --without-http \
    --without-docbook \
    --without-zlib \
    --without-lzma \
    --disable-shared \
    --silent $cflags
  echo "int main(){return 0;}">testapi.c
  echo "int main(){return 0;}">runtest.c
  echo "Building libxml..."
  make
  make install
  cd ..
  rm -rf $libxml2
fi

if test ! -f "$project_directory/gnome/lib/libxslt.a"; then
  cd $project_directory/src
  libxslt="libxslt-1.1.26"
  if test ! -f "$libxslt.tar.gz"; then
    echo "Downloading libxslt [4 lib of 4]..."
    $download ftp://xmlsoft.org/libxslt/$libxslt.tar.gz
  fi
  echo "Unpacking libxslt... (be patient)"
  rm -rf $libxslt
  gunzip -c $libxslt.tar.gz | tar vxf - >/dev/null
  cd $libxslt
  echo "Configuring libxslt..."
  ./configure --prefix=$project_directory/gnome \
     --with-libxml-prefix=$project_directory/gnome \
     --without-debug \
     --without-debugger \
     --without-crypto \
     --without-plugins \
     --disable-shared \
     --silent $cflags
  echo "Building libxslt..."
  make
  make install
  cd ..
  rm -rf $libxslt
fi

cd $parser3_directory

if test ! -f "Makefile"; then
  options="$@"
  options="$options --with-gc=$project_directory/gc/lib"
  options="$options --with-pcre=$project_directory/pcre"
  options="$options --with-xml=$project_directory/gnome"
#  options="$options --with-apache"
#  options="$options --disable-safe-mode"
#  options="$options --disable-stringstream"
  options="$options --with-included-ltdl"
  options="$options --silent $cflags"
  echo "Configuring parser3..."
  ./configure --prefix=$install_directory "--with-sendmail=$sendmail_command" $options
fi

echo "Building parser3..."
make install
if test $? -ne 0; then exit 1; fi

# remove debug info
# strip ${install_directory}/bin/parser3

echo "DONE"

echo
echo
echo "********************************************************************************************************"
echo "Now you can copy $install_directory/bin to your cgi-bin directory"
echo "  -Parser3 with XML support-"
echo "Read more about installing Parser here:"
echo "  http://www.parser.ru/en/docs/lang/install4apachecgi.htm in English"
echo "  http://www.parser.ru/docs/lang/install4apachecgi.htm in Russian"
echo "********************************************************************************************************"
