#!/bin/sh
# Script to make a gqview .tce extension from source
# Requires the gtk1.tce, glib1.tce, xorg dev environment, compiletc.tce gdk-pixbuf-0.22.0
# and graphics-libs.tcel extensions.




NAME=gqview
VERSION=1.2.2
DOWNLOAD=http://prdownloads.sourceforge.net/gqview
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=3273d2f39d4383a6b0812afeaccc5606
TMPDIR=/tmp/"$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 "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
	make
	make DESTDIR="$PKG" install
	rm -r "$PKG"/usr/local/{man,share}
	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"
	mkdir -p "$PKG"/tmp/tce.menu
	echo " [exec] (GQView) {/usr/local/bin/gqview}" > "$PKG"/tmp/tce.menu/"$NAME"-"$VERSION"
	chown 1001.50 "$PKG"/tmp/tce.menu/"$NAME"-"$VERSION"
	find . -not -type d > "$LIST"
	tar -T "$LIST" -czvf /home/tc/"$NAME"-"$VERSION".tce
}




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

export CFLAGS="-march=i686 -O2 -pipe"
export CXXFLAGS="-march=i686 -O2 -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."