#!/bin/sh
#

EXT=cyrus-sasl
TCZ=/tmp/$EXT/TCZ

sudo rm -rf /tmp/$EXT

DEPS="openssl-dev openldap-dev gdbm-dev postgresql-9.6-dev sqlite3-dev
 automake libtool-dev groff"

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

export CC="gcc -mtune=generic -Os -pipe"
export CXX="g++ -mtune=generic -Os -pipe"

##patch -Np1 -i ../cyrus-sasl-2.1.26-fixes-3.patch
##autoreconf -fi

#make clean
./autogen.sh \
	--prefix=/usr/local \
	--localstatedir=/var \
	--sysconfdir=/usr/local/etc/sasl2/ \
	--with-plugindir=/usr/local/lib/sasl2/ \
	--with-configdir=/usr/local/etc/sasl2/ \
	--with-saslauthd=/var/run/saslauthd \
	--enable-shared \
	--disable-static \
	--with-openssl \
	--with-gdbm \
	--with-dblib=gdbm \
	--with-dbpath=/usr/local/etc/sasl2/db/ \
	--enable-sql \
	--with-mysql=/usr/local/mysql \
	--with-pgsql=/usr/local/pgsql96 \
	--with-sqlite3=/usr/local \
	--with-ldap \
	--enable-ldapdb \
	|| exit

find . -name Makefile -type f -exec sed -i 's/-g -O2//g' {} \;

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/etc/sasl2/db
mkdir -p $TCZ/usr/local/lib/sasl2

mv $TCZ-dev/usr/local/sbin $TCZ/usr/local
mv $TCZ-dev/usr/local/lib/*.so* $TCZ/usr/local/lib
mv $TCZ-dev/usr/local/lib/sasl2/*.so* $TCZ/usr/local/lib/sasl2

##cp ../cyrus-sasl-pc.patched $TCZ-dev/usr/local/lib/pkgconfig/libsasl2.pc

sudo chown -R root.root $TCZ*

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

