#!/bin/sh
#

EXT=apr-util
TCZ=/tmp/$EXT/TCZ

sudo rm -rf /tmp/$EXT

DEPS="apr-dev openssl-dev openldap-dev gdbm-dev postgresql-9.6-dev
 oracle-12.2-client libtool-dev expat2-dev sqlite3-dev unixODBC-dev"

test "$KBITS" = "64" && DEPS="$DEPS mariadb-10.1-dev" || DEPS="$DEPS mariadb-dev"

NOTFOUND=""
for a in compiletc bash file squashfs-tools $DEPS
         do tce-load -i $a || tce-load -iwl $a || NOTFOUND=x
done
test -z "$NOTFOUND" || exit

sed -i -e 's/lnnz11/lnnz12/g' configure
sed -i -e 's/lnnz10/lnnz/g' configure

make clean
./configure \
	--prefix=/usr/local \
	--localstatedir=/var \
	--with-crypto \
	--with-openssl \
	--with-expat=/usr/local \
	--with-gdbm \
	--with-dbm=gdbm \
	--without-berkeley-db \
	--with-apr=/usr/local \
	--with-oracle=/usr/local/oracle \
	--with-oracle-include=/usr/local/oracle/sdk/include \
	|| exit

mv config.log config-ora.log

make || exit

make install DESTDIR=$TCZ-ora
for a in $(find $TCZ-ora -type f); do file -b $a | grep -q '^ELF .*not stripped$' && strip --strip-unneeded $a; done

make clean
./configure \
	--prefix=/usr/local \
	--localstatedir=/var \
	--with-crypto \
	--with-openssl \
	--with-expat=/usr/local \
	--with-gdbm \
	--with-dbm=gdbm \
	--without-berkeley-db \
	--with-pgsql=/usr/local/pgsql96 \
	--with-mysql=/usr/local/mysql \
	--with-sqlite3=/usr/local \
	--with-odbc=/usr/local \
	--with-apr=/usr/local \
	--with-ldap \
	|| exit

make || exit

make install DESTDIR=$TCZ-dev
for a in $(find $TCZ-dev -type f); do file -b $a | grep -q '^ELF .*not stripped$' && strip --strip-unneeded $a; done

mkdir -p $TCZ/usr/local/lib/apr-util-1

mv $TCZ-dev/usr/local/lib/*.so* $TCZ/usr/local/lib
mv $TCZ-dev/usr/local/lib/apr-util-1/*.so* $TCZ/usr/local/lib/apr-util-1
mv $TCZ-ora/usr/local/lib/apr-util-1/*oracle*.so* $TCZ/usr/local/lib/apr-util-1
mv $TCZ-ora/usr/local/lib/apr-util-1/*oracle* $TCZ-dev/usr/local/lib/apr-util-1

sed -i -e 's/APU_HAVE_ORACLE.*$/APU_HAVE_ORACLE\t11/' $TCZ-dev/usr/local/include/apr-1/apu.h

rm -rf $TCZ-ora/

sudo chown -R root.root $TCZ*

mksquashfs $TCZ /tmp/$EXT/$EXT.tcz -noappend
mksquashfs $TCZ-dev /tmp/$EXT/$EXT-dev.tcz -noappend

