#!/bin/sh

PATH=$PATH:/usr/local/apache2/bin
export PATH
SHLIB_PATH=/usr/local/lib
export SHLIB_PATH
PACKAGE=subversion
VERSION=1.2.3
BASEDIR=/home/formtrap
SRCDIR=$BASEDIR/$PACKAGE-$VERSION
CONFIG_ARGS="--with-ssl \
  --with-expat \
  --with-egd \
  --with-zlib \
  --with-apxs \
  --without-gnu-ld \
  --with-editor=nano \
  --with-apr=/usr/local/apache2/bin/apr-config \
  --with-apr-util=/usr/local/apache2/bin/apu-config \
  --with-berkeley-db=/usr/local/BerkeleyDB.4.3"

# use bash during configure phase
CONFIG_SHELL=/usr/local/bin/bash
export CONFIG_SHELL

# Add /usr/local/{lib,include} to relevant compiler paths
CFLAGS="-I/usr/local/include -I/usr/local/ssl -I/usr/local/ssl/include -I/usr/local/BerkeleyDB.4.3.27"
CPPFLAGS="$CFLAGS"
LDFLAGS="-L/usr/local/lib -L/usr/local/ssl/lib"
export CFLAGS CPPFLAGS LDFLAGS

# increase resource limits
for lim in c d f m n p s t u v ; do ulimit -$lim unlimited ; done

# clean out tmp
find /tmp/ -type f -name sh\* -exec rm -f {} \;

# check source is present
if [ ! -d "$SRCDIR" ] ; then
  echo "Source dir '$SRCDIR' not found"
  exit 1
fi

cd "$SRCDIR" || exit 1

# clean source dir
#if [ -f Makefile ] ; then
#  make distclean || exit 1
#fi
# really clean source dir
find . -name config.cache -exec rm -f {} \;

# configure sources
echo "Running './configure' $CONFIG_ARGS"
CFLAGS="-I/usr/local/include -I/usr/local/ssl -I/usr/local/ssl/include"
CPPFLAGS="$CFLAGS"
LDFLAGS="-L/usr/local/lib -L/usr/local/ssl/lib"
export CFLAGS CPPFLAGS LDFLAGS
./configure $CONFIG_ARGS CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS" CPPFLAGS="$CPPFLAGS"
if [ $? ] ; then
  true
else
  echo "Configure failed"
  exit 1
fi

# compile
echo "Running make"
make
if [ $? ] ; then
  true
else
  echo "Make failed"
  exit 1
fi

echo "$0 done."
