#!/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.0.tar.xz
WRKDIR=perl-5.32.0
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=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-Os -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

tar xf $SRCNAM

cd $WRKDIR

# Patch it

#Configure it

export BUILD_ZLIB=False
export BUILD_BZIP2=0

sh ./Configure -de -Dcc="gcc -Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp" \
-Dprefix=/usr/local -Dvendorprefix=/usr/local -Dman1dir=/usr/local/share/man/man1 \
-Dman3dir=/usr/local/share/man/man3 -Dpager="/usr/bin/less -isR" -Duseshrplib \
-Dusethreads -Duse64bitint -Darchname=arm-linux-gnueabihf-thread-multi-64int -des

# Compile
find . -name Makefile -type f -exec sed -i 's/-O2//g' {} \;
find . -name makefile -type f -exec sed -i 's/-O2//g' {} \;

make -j4

touch /tmp/mark

sudo -E 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

unset BUILD_ZLIB BUILD_BZIP2

# Adjust directory access rigths

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

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

# 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

