#!/bin/sh
#

EXT=pgtcl
TCZ=/tmp/$EXT/TCZ

sudo rm -rf /tmp/$EXT

DEPS="postgresql-9.6-dev postgresql-9.6 tcl8.6 tcl8.6-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

#make clean
./configure \
	--with-tcl=/usr/local/lib \
	--with-tclinclude=/usr/local/include \
	--with-postgres-include=/usr/local/pgsql96/include \
	--with-postgres-lib=/usr/local/pgsql96/lib \
	|| exit

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

if [ -d "$TCZ/usr/local/share" ]; then
	mkdir -p $TCZ-doc/usr/local && mv $TCZ/usr/local/share $TCZ-doc/usr/local
fi
rm -rf $TCZ/usr/local/include
rm -rf $TCZ/usr/local/bin

sudo chown -R root.root $TCZ*

mksquashfs $TCZ /tmp/$EXT/$EXT.tcz -noappend
if [ -d "$TCZ/usr/local/share" ]; then
	mksquashfs $TCZ-doc /tmp/$EXT/$EXT-doc.tcz -noappend
fi

