#!/bin/bash
# Script to make a file .tcel extension from source

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
set -e

NAME=file
VERSION=4.21
DOWNLOAD=ftp://ftp.astron.com/pub/file
SOURCE="$NAME"-"$VERSION".tar.gz
MD5SUM=cdf1f90d2704e71118353c16c19898ba
TMPDIR=/mnt/hda2/"$NAME"21651646844
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 "$SOURCE" -C "$TMPDIR"
cd "$TMPDIR"/"$NAME"-"$VERSION"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
./configure --prefix=/usr/local --sysconfdir=/usr/local/etc 
make 
make install DESTDIR="$PKG" 
cd "$PKG"
rm "$PKG"/usr/local/lib/{*.a,*.la}
rm -r "$PKG"/usr/local/include
rm -r "$PKG"/usr/local/man
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".tcel
}

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