#!/bin/bash
# Script to make a bash .tce extension source
# Requires the compiletc.tce extension


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

NAME=bash
VERSION=3.2
DOWNLOAD=http://ftp.gnu.org/gnu/bash/
SOURCE=$NAME-$VERSION.tar.gz
MD5SUM=00bfa16d58e034e3c2aa27f390390d30
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
	
	touch configure
	./configure --prefix=/usr --exec-prefix= --mandir=/usr/man \
	--disable-nls --with-curses --without-bash-malloc
	make -j1
	make DESTDIR=$PKG install

	rm -r $PKG/usr/man	
	strip --strip-unneeded $PKG/bin/bash

}

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


###############Main#############

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

set -e

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."
