#! /bin/sh
# This is a hand-written shell script

if test ! -d src/SQLite; then
  (cd src; gzip -dc ../sqlite-3*.tar.gz | tar -xf -)
  (cd src; ln -s sqlite-3* SQLite)
  (cd src/SQLite; mv src/sqliteInt.h src/sqliteInt.h.orig; sed -e 's/MAX_ATTACHED 10/MAX_ATTACHED 31/' src/sqliteInt.h.orig > src/sqliteInt.h)
fi
if test ! -f src/SQLite/Makefile; then
: ${R_HOME=`R RHOME`}
  if test -z "${R_HOME}"; then
    echo "could not determine R_HOME"
    exit 1
  fi
  CC=`${R_HOME}/bin/R CMD config CC`
  # we need to force a PIC build of a static library
  CFLAGS=`${R_HOME}/bin/R CMD config CFLAGS`
  CPICFLAGS=`${R_HOME}/bin/R CMD config CPICFLAGS`
  CFLAGS="${CFLAGS} ${CPICFLAGS}"
  export CC CFLAGS
  (cd src/SQLite; sed -e '/^#include.*stdlib.h/a\
#include <unistd.h>' tool/lemon.c > lemon.c.tmp; mv lemon.c.tmp tool/lemon.c; ./configure --disable-shared)
fi
(cd src/SQLite; make)
# we need to pick up the library containing fdatasync
TLIBS=`sed -n -e'/^TLIBS =/p' src/SQLite/Makefile | sed -e's/^TLIBS = //' `
sed -e "s/@TLIBS@/${TLIBS}/" src/Makevars.in > src/Makevars