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

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

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

SRCNAM=perl-5.32.1.tar.xz
WRKDIR=perl-5.32.1
EXTNAM=perl5
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
#rm -r -f $TMPDIR-dev

# Crete temporary directory

mkdir -p $TMPDIR

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

# Export variables needed for compilation


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"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

tar xf $SRCNAM

cd $WRKDIR

# Patch it

#Configure it

./Configure -des -Dcc="gcc -march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" -Dprefix=/usr/local -Dvendorprefix=/usr/local \
 -Dusethreads -Duselargefiles -Dusemultiplicity -Darchname=aarch64-linux-thread-multi


# Compile

make -j4

touch /tmp/mark

sudo make install

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

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Adjust directory access rigths

# Remove the duplicat perl binary
rm -f $TMPDIR/usr/local/bin/perl5.32.0

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

# Strip executables

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | sudo 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/*.a $TMPDIR-dev/usr/local/lib
#mv $TMPDIR/usr/local/lib/*.la $TMPDIR-dev/usr/local/lib
#mv $TMPDIR/usr/local/lib/pkgconfig $TMPDIR-dev/usr/local/lib


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

# Cleanup temp directory

sudo rm -r -f usr

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

#cd $TMPDIR-doc
#cd ..
#mksquashfs $TMPDIR-doc $EXTNAM-doc.tcz -all-root -noappend
#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

#sudo rm -r -f usr

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

#cd $TMPDIR-dev
#cd ..
#mksquashfs $TMPDIR-dev $EXTNAM-dev.tcz -all-root -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

# Cleanup temp directory

#sudo rm -r -f usr

