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

# Make sure dependant extensions are loaded (Quiet if everything is already loade)
STD_DEPS="compiletc squashfs-tools"
BUILD_DEPS=""
TCZ_DEPS=""
DEV_TCZ_DEPS=""
FULL_DEPS="$STD_DEPS $BUILD_DEPS"

for deps in $FULL_DEPS; do
	tce-status -i | grep -q ${deps%.tcz}
	if [ $? -ne 0 ]; then
		tce-load -i $deps >/dev/null 2>&1
		if [ $? -eq 0 ]; then
        	echo "Loaded $deps..."
		else
			echo "Failed to load $deps... exiting"
			exit 1
		fi
	fi
done

######################################################
# Configure extension creation parameters            #
######################################################
#Source="https://git.kernel.org/pub/scm/utils/dtc/dtc.git/"

SRCPATH="../../../sources"

SRCNAM=dtc-1.7.0.tar.gz
WRKDIR=dtc-1.7.0
EXTNAM=dtc
LIBNAM=libfdt
TMPDIR=/tmp/$EXTNAM
LIBTMP=/tmp/$LIBNAM

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

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

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

# Crete temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation

case $(uname -m) in
    armv*)
       export EXTRA_CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
#       export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
    ;;
    aarch64)
       export EXTRA_CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
#       export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv8-a+crc -mtune=cortex-a72"
    ;;
esac

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Unpack source in current directory

tar xf ${SRCPATH}/${SRCNAM}

# Configure it

cd $WRKDIR

# Compile

make -j6 NO_PYTHON=1

# Install in base temp dir

make install DESTDIR=$TMPDIR PREFIX=/usr/local NO_PYTHON=1

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# 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

# Move files to doc extension

#mkdir -p $TMPDIR-doc/usr/local
#mv $TMPDIR/usr/local/share $TMPDIR-doc/usr/local

# Move files to dev extension
rm -f $TMPDIR/usr/local/lib/*.a
rm -f $TMPDIR/usr/local/lib/*.la


# Move fdt library
mkdir -p $LIBTMP/usr/local
mv $TMPDIR/usr/local/lib $LIBTMP/usr/local

#Make lib -dev 

mkdir -p $LIBTMP-dev/usr/local
mv $TMPDIR/usr/local/include $LIBTMP-dev/usr/local
mv $TMPDIR/usr/local/lib/pkgconfig $LIBTMP-dev/usr/local/lib


###################################################
# 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 $LIBTMP-dev
cd ..
mksquashfs $LIBTMP-dev $LIBNAM-dev.tcz
cd $LIBTMP-dev
find usr -not -type d > $LIBNAM-dev.tcz.list
mv ../$LIBNAM-dev.tcz .

# Create md5 file

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

# Cleanup temp directory

rm -r -f usr

###################################################
# Create bin extension in temp dir                #
###################################################

cd $LIBTMP
cd ..
mksquashfs $LIBTMP $LIBNAM.tcz
cd $LIBTMP
find usr -not -type d > $LIBNAM.tcz.list
mv ../$LIBNAM.tcz .

# Create md5 file

md5sum $LIBNAM.tcz > $LIBNAM.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

