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

######################################################
# Make sure Dependencies for Script are loaded       #
######################################################
tce-load -i compiletc squashfs-tools git 

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

WRKDIR=rpi-vc
EXTNAM=rpi-vc
TMPDIR=/tmp/rpi-vc

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

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR
rm -r -f $TMPDIR-dev

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

# Export variables needed for compilation
# rpi-vc just pulls the binaries from the rpi github
#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"
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

#@git clone --depth 1 https://github.com/raspberrypi/firmware.git $WRKDIR
wget https://github.com/raspberrypi/firmware/archive/master.zip
unzip master.zip -q -x firmware-master/boot/* firmware-master/modules/* \
firmware-master/documentation/* firmware-master/extra/* firmware-master/opt/*

cd firmware-master/hardfp/opt/vc

# Install in base temp dir

mkdir -p $TMPDIR/usr/local/share/licences/videocore

cp LICENCE $TMPDIR/usr/local/share/licences/videocore/LICENCE
cp -a bin $TMPDIR/usr/local
cp -a sbin $TMPDIR/usr/local

find ./lib -name "*.so" -not -type d > /tmp/tmp.lst
tar -T /tmp/tmp.lst -cf /tmp/tmp.tar
tar -C $TMPDIR/usr/local -xf /tmp/tmp.tar
rm -f /tmp/tmp.tar

# Install Headers and Static Libs into dev dir

mkdir -p $TMPDIR-dev/usr/local
cp -a include $TMPDIR-dev/usr/local

find ./lib -name "*.a" -not -type d > /tmp/tmp.lst
find ./lib -name "*.pc" -not -type d >> /tmp/tmp.lst
tar -T /tmp/tmp.lst -cf /tmp/tmp.tar
tar -C $TMPDIR-dev/usr/local -xf /tmp/tmp.tar
rm -f /tmp/tmp.tar

# Delete compilation work directory

cd ../../../..
rm -r -f firmware-master

# 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

###################################################
# Create dev extension in temp dir                #
###################################################

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

# Create md5 file

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

# Cleanup temp directory

rm -r -f usr

