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

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


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

WRKDIR=WiringPi
EXTNAM=wiringpi
TMPDIR=/tmp/wiringpi

######################################################
# 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 $(find /lib | grep ld-linux) in
    *armhf*)
       export 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"
       BIT32="linux32"
    ;;
    *aarch64*)
       export 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"
       BIT32=""
    ;;
esac

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



git clone --depth 1 https://github.com/WiringPi/WiringPi

# Patch it

cd $WRKDIR

VERSION=$(git describe --tags)

# Compile

# Build script fails if this directory does not exist
#[ -d /usr/local/include ] || sudo mkdir -p /usr/local/include

rm -f /tmp/instpaths.txt
rm -f /tmp/instfiles.txt
inotifywait --quiet --monitor --recursive -e create,modify,move --format '%w%f' /usr/local > /tmp/instlist.txt &

# Build will compile and install all
${BITS} ./build

pkill inotifywait

# This sorts the file in place while removing duplicate entries.
echo "$(sort -u /tmp/instlist.txt)" > /tmp/instlist.txt

while read -r ENTRY
do
    # Keep file entries, skip entries that are only paths.
    if [ -f "$ENTRY" ]
    then
        echo "$ENTRY" >> /tmp/instfiles.txt
        # since we found a file, we want its path.
        echo "${ENTRY%/*}" >> /tmp/instpaths.txt
    fi
done < /tmp/instlist.txt

# This sorts the file in place while removing duplicate entries.
echo "$(sort -u /tmp/instpaths.txt)" > /tmp/instpaths.txt

while read -r ENTRY
do
    # Keep file entries, skip entries that are only paths.
    if [ -f "$ENTRY" ]
    then
        echo "$ENTRY" >> /tmp/instfiles.txt
        # since we found a file, we want its path.
        echo "${ENTRY%/*}" >> /tmp/instpaths.txt
    fi
done < /tmp/instlist.txt

# This sorts the file in place while removing duplicate entries.
echo "$(sort -u /tmp/instpaths.txt)" > /tmp/instpaths.txt
echo "$(sort -u /tmp/instfiles.txt)" > /tmp/instfiles.txt

# Install in base temp dir
# Create the directory tree to move the installed files to.
while read -r ENTRY
do
    mkdir -p "$TMPDIR/$ENTRY"
done < /tmp/instpaths.txt

# Move the files from my system to the package directories where they belong.
while read -r ENTRY
do
    sudo mv "$ENTRY" "$TMPDIR/$ENTRY"
done < /tmp/instfiles.txt

sudo mkdir -p $TMPDIR/usr/local/share/licences/wiringpi
sudo cp COPYING.LESSER $TMPDIR/usr/local/share/licences/wiringpi/

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Adjust directory access rigths

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

# Strip executables

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

# Move files to doc extension

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

# Move files to dev extension

mkdir -p $TMPDIR-dev/usr/local/lib
mv $TMPDIR/usr/local/include $TMPDIR-dev/usr/local
mv $TMPDIR/usr/local/lib/pkgconfig $TMPDIR-dev/usr/local/lib
rm -f $TMPDIR/usr/local/lib/*.a
rm -f $TMPDIR/usr/local/lib/*.la

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

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

# Create md5 file

md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt
echo $VERSION >  $EXTNAM.tcz.ver

# Cleanup temp directory

sudo rm -r -f usr

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

cd $TMPDIR-dev
cd ..
mksquashfs $TMPDIR-dev $EXTNAM-dev.tcz -noappend
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
echo $VERSION >  $EXTNAM-dev.tcz.ver

# Cleanup temp directory

sudo rm -r -f usr

