#!/bin/sh
#

### use KVER and KBITS from build-xapps.sh
KVER=$(uname -r)
test "$(uname -m)" = "x86_64" && export KBITS=64 || export KBITS=32

EXT=xtables-addons-$KVER
TCZ=/tmp/$EXT/TCZ

sudo rm -rf /tmp/$EXT

DEPS="bc netfilter-KERNEL tcl8.6"
test "$KBITS" = "64" && DEPS="$DEPS iptables-dev" || DEPS="$DEPS iptables"

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

sudo rm -f /lib/modules/$KVER/build
sudo ln -s /mnt/sda1/lamp$KBITS/kernel/linux-${KVER%%-*} /lib/modules/$KVER/build

./autogen.sh

for a in $(grep -r -l /usr/share/xt_geoip *); do sed -i -e 's#/usr/share/xt_geoip#/usr/local/share/xt_geoip#g' $a; done

./configure \
	|| exit

bash -c make || exit

make install DESTDIR=$TCZ

gzip $TCZ/lib/modules/$KVER/extra/*.ko
depmod -b $TCZ
strip --strip-unneeded $TCZ/usr/local/lib/xtables/*
strip --strip-unneeded $TCZ/usr/local/lib/*.so*
strip --strip-unneeded $TCZ/usr/local/sbin/iptaccount
cp /mnt/sda1/lamp/contrib/xt_geoip_build.tcl $TCZ/usr/local/libexec/xtables-addons

sudo chown -R root.root $TCZ

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

