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

tce-load -i compiletc squashfs-tools

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

VERSION=4.7
SRCNAM=chrony-${VERSION}.tar.gz
WRKDIR=chrony-$VERSION
EXTNAM=chrony
TMPDIR=/tmp/$EXTNAM

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

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

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

# Unpack source in current directory

tar xf ${SRCPATH}/${SRCNAM}

# Configure it

cd $WRKDIR

./configure --prefix=/usr/local --sysconfdir=/usr/local/etc

# Compile

${BIT32} make -j8

# Install in base temp dir

${BIT32} make install DESTDIR=$TMPDIR
mkdir -p $TMPDIR/usr/local/share/chrony
cp examples/chrony.conf.example* $TMPDIR/usr/local/share/chrony

# 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 : | ${BIT32} xargs strip --strip-unneeded

## copy script
mkdir -p $TMPDIR/usr/local/tce.installed
cp chrony.ins $TMPDIR/usr/local/tce.installed/chrony

sudo chown -R root:staff $TMPDIR/usr/local/tce.installed
sudo chmod -R 775 $TMPDIR/usr/local/tce.installed

# Move files to doc extension

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

###################################################
# 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
echo $VERSION >  $EXTNAM.tcz.ver

# Cleanup temp directory

rm -r -f usr
rm -r -f var
