#!/bin/bash
# Script to make a atk .tcel extension source
# requires glib-2.16.4.tcel and -devs to be installed.

export CFLAGS="-march=i486 -Os -pipe"
export CXXFLAGS="-march=i486 -Os -pipe"

NAME=atk
VERSION=1.23.5
DOWNLOAD=ftp://ftp.gnome.org/pub/gnome/sources/"$NAME"/1.23
SOURCE="$NAME"-"$VERSION".tar.bz2
MD5SUM=0b7d2800deb08b82c12f535b7d4199fd
TMPDIR=/mnt/hda2/"$NAME"56543
SRCDIR=/tmp
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
LIST1="$TMPDIR"/"$NAME".list1
SRCDIR=/tmp
DIALOG=dialog

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 "atk-1.23.5.tar.bz2 md5sum 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
./configure --prefix=/usr/local --disable-gtk-doc --sysconfdir=/usr/local/etc --disable-docs 
make
make install
make DESTDIR="$PKG" install
rm -r "$PKG"/usr/local/share
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
}

seperate_extension()  {
mkdir -p "$DEV"/usr/local/lib
mv "$PKG"/usr/local/include "$DEV"/usr/local/
mv "$PKG"/usr/local/lib/pkgconfig "$DEV"/usr/local/lib/
mv "$PKG"/usr/local/lib/*.la "$DEV"/usr/local/lib/
}

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

if [ -e "$TMPDIR" ]; then
rm -r "$TMPDIR"
fi
mkdir -p "$PKG"
mkdir -p "$DEV"
#download || exit 1
build_source || exit 1
seperate_extension || exit 1
make_extension || exit 1
echo "Done."
