#!/bin/sh

tce-load -w -i git squashfs-tools compiletc
ln -s /lib /lib64
mkdir src_package
DESTDIR='/tmp/package'
mkdir /tmp/source_package
mkdir -p $DESTDIR/usr/local/

# Create package directory
. /etc/init.d/tc-functions
PKGDIR=/tmp/`getMajorVer`.x/`getBuild`
mkdir -p $PKGDIR/tar.gz $PKGDIR/tcz

cd /tmp/source_package
git clone https://github.com/joewalnes/websocketd.git
cd websocketd/


# Compile
export CFLAGS="-mtune=generic -Os -pipe"
export CXXFLAGS="-mtune=generic -Os -pipe"
export LDFLAGS="-Wl,-O1"
make -j4
strip ./websocketd

# Create websocketd.tcz package
mkdir -p $DESTDIR/usr/local/bin/
cp ./websocketd $DESTDIR/usr/local/bin/
cd $DESTDIR
tar -czf $PKGDIR/tar.gz/websocketd.tcz.tar.gz *
mksquashfs $DESTDIR $PKGDIR/tcz/websocketd.tcz -all-root -info
find $DESTDIR -not -type d | sed -e "s#^${DESTDIR}##" > $PKGDIR/tcz/websocketd.tcz.list
cd $PKGDIR/tcz
md5sum websocketd.tcz > websocketd.tcz.md5.txt

# Cleanup
cd /tmp
rm -rf $DESTDIR
rm -rf /tmp/source_package/
