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

tce-load -i compiletc squashfs-tools

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

SRCNAM=dnsmasq-2.82.tar.xz
WRKDIR=dnsmasq-2.82
EXTNAM=dnsmasq
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 $(uname -m) in
	arm*)
		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"
	;;
	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"
	;;
esac

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

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it

cd $WRKDIR

###Fix Makefile
sed -i 's@CFLAGS        =@CFLAGS        +=@' Makefile
sed -i 's@-O2@@g' Makefile

#./configure --prefix=/usr/local

# Compile

make

# Install in base temp dir

touch /tmp/mark
sudo make install 

# Delete compilation work directory

mkdir -p $TMPDIR
find /usr/local -newer /tmp/mark -not -type d > /tmp/tmp.lst

tar -T /tmp/tmp.lst -cf /tmp/tmp.tar
tar -C $TMPDIR -xf /tmp/tmp.tar

mkdir -p $TMPDIR/usr/local/share/dnsmasq

cp COPYING* $TMPDIR/usr/local/share/dnsmasq

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/share
mv $TMPDIR/usr/local/share/man $TMPDIR-doc/usr/local/share

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

