#!/bin/bash
# Script to make a python .tce extension from source
# Requires the compiletc.tce, bash.tce extensions to build.
# License: Python license.  www.python.org/psf/license/

# -03 is the Python default
export CFLAGS="-march=i486 -mtune=i686 -03 -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -03 -pipe"

NAME=python
VERSION=2.6.4
DOWNLOAD=http://www.python.org/ftp/python/2.6.4
SOURCE=Python-"$VERSION".tar.bz2
MD5SUM=245db9f1e0f09ab7e0faaa0cf7301011
TMPDIR=/mnt/hda2/"$NAME"21654
PKG=/mnt/hda2/python/pkg
LIST="$TMPDIR"/"$NAME".list
PWD=`pwd`

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 "$SOURCE"
cd Python-"$VERSION"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/local/lib/pkgconfig
./configure --prefix=/usr/local --enable-shared --with-ncurses --with-threads 
sleep 20
make CFLAGS="-march=i486 -mtune=i686 -O3 -pipe"
make CFLAGS="-march=i486 -mtune=i686 -O3 -pipe" 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
rm -r "$PKG"/usr/local/share
}



make_extension() {
# Use package splitting script, but first:
# do cd pkg && find . -type d -name test | xargs rm -r && find . -type d -name tests | xargs rm -r

cd /mnt/hda2/python
mksquashfs pkg python.tczl

}

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