#!/bin/bash
# Script to make a xchat-2.8.6 extension from source
# Requires openssl-0.9.8h, the gtk+-2.12.11 and Xorg development environments.


export CFLAGS="-march=i686 -O2 -pipe"
export CXXFLAGS="-march=i686 -O2 -pipe"

NAME=xchat
VERSION=2.8.6
DOWNLOAD=http://www.xchat.org/files/source/2.8/
SOURCE="$NAME"-"$VERSION".tar.bz2
MD5SUM=1f2670865d43a23a9abc596dde999aca
TMPDIR=/usr/local/"$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 xjvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
export LDFLAGS="-L/usr/local/lib"
export CPPFLAGS="-I/usr/local/include"
./configure --prefix=/usr/local --disable-nls 
make 
make DESTDIR="$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
find . | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null

}

make_extension() {
mkdir -p "$PKG"/tmp/tce.menu
echo " [exec] (Xchat) {/usr/local/bin/xchat}" > "$PKG"/tmp/tce.menu/xchat-2.8.6
chown 1001.50 "$PKG"/tmp/tce.menu/xchat-2.8.6
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."
