#!/bin/bash



export CFLAGS="-march=i686 -O2 -pipe"
export CXXFLAGS="-march=i686 -O2 -pipe"
DIALOG=dialog
NAME=coreutils
VERSION=6.12
DOWNLOAD=http://ftp.gnu.org/gnu/$NAME/
SOURCE=$NAME-$VERSION.tar.gz
TMPDIR=/mnt/hdb1/$NAME
PKG=/$TMPDIR/pkg
LIST=$TMPDIR/$NAME.list
SRCDIR=/tmp

DEFAULT_POSIX2_VERSION=199209

download() {
cd $SRCDIR
if [ ! -e $SOURCE ]; then
wget $DOWNLOAD/$SOURCE 
fi
}


build_source() {
tar xzvf $SRCDIR/$SOURCE -C $TMPDIR
cd $TMPDIR/$NAME-$VERSION
./configure --prefix=/usr/local --disable-nls
make
$DIALOG --title "Run Expensive Tests" --yesno "Selecting Yes will run some resource expensive tests that will ensure you have a rock solid build of coreutils.  If you have perl_xml.dsl installed and a modern  machine you can give it a whirl.  These test will fail without the perl_xml.dsl extension installed." 20 50
if [ $? = 0 ]; then
make RUN_EXPENSIVE_TESTS=yes check
make DESTDIR=$PKG install
rm -r "$PKG"/usr/local/man
cd $TMPDIR
else
make DESTDIR=$PKG install
rm -r "$PKG"/usr/local/man
cd $TMPDIR 
fi
}


strip_files() {
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
rm -r $PKG/usr/local/share/man

cd $TMPDIR
}

make_extension() {
cd $PKG
find . -not -type d > $LIST 
tar -T $LIST -czvf /home/tc/$NAME-$VERSION.tcel
cd $TMPDIR
}

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