#!/bin/bash
# Script to make a gtk-gnutella .tce extension from source
# Requires the compiletc.tce, gtk1.tce, glib.tce X_headers.tce/xorg 




NAME=gtk-gnutella
VERSION=0.96.5
DOWNLOAD=http://prdownloads.sourceforge.net/gtk-gnutella
SOURCE=$NAME-$VERSION.tar.bz2
MD5SUM=539fbdf65c6038d1da2e5cebede52e43
TMPDIR=/usr/local/"$NAME"
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 ""$NAME"-"$VERSION".tar.bz2md5sum passed."
		sleep 2
	else
		echo "Download failed. aborting"
		exit 1;
	fi
}

build_source() {
	tar xjvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"-"$VERSION"
	export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
	./build.sh --prefix=/usr/local --disable-nls --gtk1 
	make
	make INSTALL_PREFIX="$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

}

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

###########Main#################

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
set -e

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