#!/bin/bash
# Script to make a gtk-gnutella .tcel extension from source
# Requires the compiletc.dsl and libxml2-2.6.32.tcel extensions.
# GTK-Gnutella License: 

CFLAGS="-march=i686 -02 -pipe"
CXXFLAGS="-march=i686 -02 -pipe"

NAME=libxml2
VERSION=2.6.32
DOWNLOAD=ftp://xmlsoft.org/libxml2
SOURCE=$NAME-$VERSION.tar.gz
MD5SUM=2621d322c16f0257e30f0ff2b13384de
TMPDIR=/tmp/"$NAME"51678
PKG=$TMPDIR/pkg
LIST=$TMPDIR/$NAME.list
SRCDIR=/tmp

download() {
cd "$SRCDIR"
if [ -e "$SOURCE" ]; then
	if [ $(md5sum "$SOURCE" | cut -c1-32) != "$MD5SUM" ]; then
		rm "$SOURCE"
	fi
fi
if [ ! -e "$SOURCE" ]; then
	wget "$DOWNLOAD"/"$SOURCE"
fi
if [ $(md5sum "$SOURCE" | cut -c1-32) = "$MD5SUM" ]; then
	echo ""$SOURCE" md5sum passed."
	sleep 2
else
	echo "Download failed. aborting"
	exit 1;
fi
}
build_source() {
tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"

./configure --prefix=/usr/local
make
make DESTDIR="$PKG" install

cd "$PKG"
find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null
cd "$TMPDIR"
}

remove_docs() {
if [ -e "$PKG"/usr/local/share/man ]; then
	rm -r "$PKG"/usr/local/share/man
fi
if [ -e "$PKG"/usr/local/share/doc ]; then
	rm -r "$PKG"/usr/local/share/doc
fi
if [ -e "$PKG"/usr/local/share/gtk-doc ]; then
	rm -r "$PKG"/usr/local/share/gtk-doc
fi
}

make_extension() {
cd "$PKG"
find . -not -type d > "$LIST"
tar -T "$LIST" -czvf /home/dsl/"$NAME"-"$VERSION".tcel
cd "$TMPDIR"
}

if [ -e "$TMPDIR" ]; then
rm -rf "$TMPDIR" 
fi
mkdir -p "$PKG" &&
download &&
build_source &&
remove_docs &&
make_extension &&
echo "$NAME-$VERSION.tcel is now in your home directory."