#!/bin/bash
# Script to make a links .tce extension from source
# Requires the compiletc.tcel and X_headers.tce extension, graphics-libs-1.tce., and openssl-0.9.8h.tcel
# Links License: GPL


CFLAGS="-march=i686 -02 -pipe"
CXXFLAGS="-march=i686 -02 -pipe"


NAME=links
VERSION=2.1
DOWNLOAD=http://links.twibright.com/download/
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=3da367d77c91802d3001bbebd3da18e3
TMPDIR=/tmp/"$NAME"
PKG="$TMPDIR"/pkg
LIST="$TMPDIR"/"$NAME".list
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 "md5sum passed."
else
echo "Download failed. aborting"
exit 1;
fi
}

build_source() {
tar xzvf "$SRCDIR"/"$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
./configure --prefix=/usr/local --with-libjpeg --enable-graphics
make
make DESTDIR="$PKG" install
rm -r "$PKG"/usr/local/man
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
cd "$TMPDIR"
}

make_extension() {
mkdir -p $PKG/tmp/tce.menu
echo " [exec] (Links2) {/usr/local/bin/links -g}" > "$PKG"/tmp/tce.menu/links-2.1
chown 1001.50 "$PKG"/tmp/tce.menu/links-2.1
cd "$PKG"
find . -not -type d > "$LIST"
tar -T "$LIST" -czvf /home/tc/"$NAME"-"$VERSION".tce
}

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"