#!/bin/bash
# Script to make a gqview .tce extension from source
# Requires the gtk+-2.12.11 and Xorg development environments.


CFLAGS="-march=i486 -mtune=i686 -0s -pipe"
CXXFLAGS="-march=i486 -mtune=i686 -0s -pipe"

NAME=gqview
VERSION=2.0.4
DOWNLOAD=http://prdownloads.sourceforge.net/gqview
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=7196deab04db94cec2167637cddc02f9
TMPDIR=/mnt/hda2/"$NAME"5467
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 --disable-nls 
make 
make DESTDIR="$PKG" install 
rm -r "$PKG"/usr/local/man
rm -r "$PKG"/usr/local/share/{doc,locale}
strip --strip-unneeded $PKG/usr/local/bin/gqview
}

make_extension() {

cd "$PKG"
find . -not -type d > "$LIST"
tar -T "$LIST" -czvf /home/tc/"$NAME"-"$VERSION".tce
cd "$TMPDIR"
}

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