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

STD_DEPS="compiletc squashfs-tools autoconf automake"
BUILD_DEPS=""
TCZ_DEPS="libnl"
DEV_TCZ_DEPS="libnl-dev"
FULL_DEPS="$STD_DEPS $BUILD_DEPS $TCZ_DEPS $DEV_TCZ_DEPS"

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

######################################################
# Configure extension creation parameters            #
######################################################
SRCPATH="../../../sources"

#SRC https://www.kernel.org/pub/software/network/iw/iw-6.7.tar.xz

SRCNAM=iw-6.7.tar.xz
WRKDIR=iw-6.7
EXTNAM=iw
TMPDIR=/tmp/$EXTNAM

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

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

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

# Crete temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation

case $(find /lib | grep ld-linux) in
	*armhf*)
		export CFLAGS="-flto=auto -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
		export CXXFLAGS="-flto=auto -Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
		BIT32="linux32"
	;;
	*aarch64*)
		export CFLAGS="-flto=auto -Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
		export CXXFLAGS="-flto=auto -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

# Unpack source in current directory

tar -xf $SRCPATH/$SRCNAM

# Configure it

cd $WRKDIR

#${BIT32} ./configure --prefix=/usr/local

# Compile

PREFIX=/usr/local ${BIT32} make -j4

# Install in base temp dir

PREFIX=/usr/local ${BIT32} make install DESTDIR=$TMPDIR

if [ -f COPYING ]; then
	mkdir -p $TMPDIR/usr/local/share/$EXTNAM
	cp COPYING $TMPDIR/usr/local/share/$EXTNAM
fi

# Adjust directory access rigths

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

# Strip executables

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

# Move files to doc extension

rm -rf $TMPDIR/usr/local/share/man
rm -rf $TMPDIR/usr/local/share/info

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

sudo chown -R root:root $TMPDIR
sudo chown tc $TMPDIR
if [ -f $EXTNAM.ins ]; then
	mkdir -p $TMPDIR/usr/local/tce.installed
	cp screen.ins $TMPDIR/usr/local/tce.installed/$EXTNAM
	sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
	sudo chmod -R 775 $TMPDIR/usr/local/tce.installed/screen
fi

###################################################
# 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

sudo rm -r -f usr

###################################################
# Create doc extension in temp dir                #
###################################################

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

# Create md5 file

#md5sum $EXTNAM-doc.tcz > $EXTNAM-doc.tcz.md5.txt

# Cleanup temp directory

#rm -r -f usr
