#!/bin/bash
# Script to make a gdk-pixbuf .tcel extension from source
# Requires the gtk1.tce, glib1.tce, xorg dev environment, compiletc.tce
# and graphics-libs.tcel extensions.
# Gdk-pixbuf License: LGPL
# Build process largely taken from Romster's Crux port.


NAME=gdk-pixbuf
VERSION=0.22.0
DOWNLOAD=http://ftp.gnome.org/pub/GNOME/sources/"$NAME"/0.22
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=4db0503b5a62533db68b03908b981751
TMPDIR=/tmp/"$NAME"
PKG="$TMPDIR"/pkg
DEV="$TMPDIR"/dev
LIST="$TMPDIR"/"$NAME".list
LIST1="$TMPDIR"/"$NAME".list1
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 "md5sum passed."
	else
	  echo "Download failed. aborting"
	  exit 1;
	fi
}

build_source() {
	tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
	cd "$TMPDIR"/"$NAME"-"$VERSION"

	export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
	./configure --prefix=/usr/local --disable-gnome
	make
	make DESTDIR="$PKG" install
	rm -r "$PKG"/usr/local/share/gnome
	sed -i -e "s|-L"$PKG"/usr/lib||g" "$PKG"/usr/lib/*.la
	chmod 755 $PKG/usr/local/bin/gdk-pixbuf-config	
	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

}

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.tcel
}

split_extension() {
	mv "$PKG"/usr/local/lib/{*.a,*.la,*.sh} "$DEV"/usr/local/lib/
	mv "$PKG"/usr/local/include "$DEV"/usr/local/
	mv "$PKG"/usr/local/share/aclocal "$DEV"/usr/local/share/
	mv "$PKG"/usr/local/bin/gdk-pixbuf-config "$DEV"/usr/local/bin/
	mv "$PKG"/usr/local/lib/gdk-pixbuf/loaders/{*.a,*.la} "$DEV"/usr/local/lib/gdk-pixbuf/loaders/
}




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

export CFLAGS="-march=i686 -O2 -pipe -L"$PKG"/usr/local/lib"

set -e
if [ -e "$TMPDIR" ]; then
	rm -rf "$TMPDIR" 
fi
mkdir -p "$PKG" || exit 1
mkdir -p "$DEV"/usr/local/{lib/gdk-pixbuf/loaders,bin,share} || exit 1
download || exit 1
build_source || exit 1
split_extension || exit 1
make_extension || exit 1
echo ""$NAME"-"$VERSION".tcel and "$NAME"-"$VERSION"-devs.tcel is now in your home directory."