#!/bin/sh
#
######################################################
# Build script for RPI                               #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=RPi.GPIO-0.5.11.tar.gz
WRKDIR=RPi.GPIO-0.5.11
EXTNAM=python-RPi.GPIO
TMPDIR=/tmp/python-RPi.GPIO
PYTDIR=/usr/local/lib/python2.7/site-packages

######################################################
# Prepare extension creation                         #
######################################################

# Remove dirs and files left from previous creation

rm -r -f $TMPDIR

# Crete temporary directory

mkdir -p $TMPDIR$PYTDIR

######################################################
# Compile extension                                  #
######################################################

# Export variables needed for compilation

export CFLAGS="-O2 -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-O2 -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it

cd $WRKDIR

# Build

python setup.py build

# Install in place

python setup.py install

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Move files to tmp dir

cp -r $PYTDIR/RPi* $TMPDIR/$PYTDIR

# Delete *.pyc files

find $TMPDIR/ -name *.pyc | xargs rm -r

# Adjust directory access rigths

find $TMPDIR/ -type d | xargs chmod -v 755;

# Strip executables

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

###################################################
# Create base extension in temp dir               #
###################################################

cd $TMPDIR
cd ..
mksquashfs $TMPDIR $EXTNAM.tcz
cd $TMPDIR
find usr -not -type d > $EXTNAM.tcz.list
mv ../$EXTNAM.tcz .

# Create md5 file

md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

