#!/bin/sh
#
######################################################
# Build script for piCore64-15.x                     #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi bash
#download and install the compile tools
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="tree"
VERSION="2.2.1"
DESCRIPTION="recursive directory listing program"
DOCDESCRIPTION="Documentation part of tree"
DEVDESCRIPTION="Development files part of tree"
AUTHORS="Steve Baker"
HOMEPAGE="http://oldmanprogrammer.net/source.php?dir=projects/tree"
LICENSE="GPLv2"
ME="rhermsen"
TAGS="recursive directory listing program"
DOCTAGS="man pages recursive directory listing program"
DEVTAGS="development recursive directory listing program"
DESTDIR=/tmp/dest/${PACKAGE}
TMPDIR=/tmp/submit/${PACKAGE}
SRCDIR=/tmp/${PACKAGE}
ARCH=`uname -m`

# Workdir
sudo rm -r ${SRCDIR} 2>/dev/null
mkdir ${SRCDIR}

# Source
cd ${SRCDIR}
wget http://oldmanprogrammer.net/tar/${PACKAGE}/${PACKAGE}-${VERSION}.tgz
tar xzf ${PACKAGE}-${VERSION}.tgz

cd ${SRCDIR}/${PACKAGE}-${VERSION}


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

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "armhf" ]
then
echo
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" BITS="linux32"
# no specific arch dependent parameters
elif [ $ARCH == "aarch64" ]
then
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" BITS=""
echo
# no specific arch dependent parameters
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

# Compile
cd /${SRCDIR}/${PACKAGE}-${VERSION}

# variable expansion with sed
# https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command
sed -i "s/^PREFIX=\/usr\/local/PREFIX=\/tmp\/dest\/tree\/usr\/local/g"  /${SRCDIR}/${PACKAGE}-${VERSION}/Makefile


make
mkdir -p $DESTDIR
make DESTDIR=$DESTDIR install


# Below lines just for savekeeping in case make is used in a build script, and needs troubleshooting.
# https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands
# make -n install
# make SHELL='sh -x' DESTDIR=$DESTDIR install 

######################################################
# Base extension                                     #
######################################################
sudo rm -r ${TMPDIR}* 2>/dev/null

mkdir -p  ${TMPDIR}/usr/local/bin
mkdir -p  ${TMPDIR}/usr/local/share/doc/${PACKAGE}

cp ${SRCDIR}/${PACKAGE}-${VERSION}/LICENSE ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
mv ${DESTDIR}/tree ${TMPDIR}/usr/local/bin


###################################################
# Create info file                                #
###################################################
cd /tmp/submit/
if [ $ARCH == "armhf" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       Tree is a recursive directory listing program that
                produces a depth indented listing of files.
                Also supports output to file and HTML formatted output
                Run tree --help for all options
                This extension is PPI compatible

Change-log:     ${TODAY} First version, ${VERSION} (${ME})
Current:        ${TODAY} First version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "aarch64" ]
then
cat <<EOF> ${PACKAGE}.tcz.info
Title:          ${PACKAGE}.tcz
Description:    ${DESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${TAGS}
Comments:       Tree is a recursive directory listing program that
                produces a depth indented listing of files.
                Also supports output to file and HTML formatted output
                Run tree --help for all options
                This extension is PPI compatible

Change-log:     ${TODAY} First version, ${VERSION} (${ME})
Current:        ${TODAY} First version, ${VERSION} (${ME})
EOF
fi

# Move files to doc extension
mkdir -p $TMPDIR-doc/usr/local/share/${PACKAGE}

mv ${DESTDIR}/usr/local/man ${TMPDIR}-doc/usr/local/share/${PACKAGE}/

###################################################
# Create info file                                #
###################################################
cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:          ${PACKAGE}-doc.tcz
Description:    ${DOCDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   ${ME}
Tags:           ${DOCTAGS}
Comments:       Tree is a recursive directory listing program that
                produces a depth indented listing of files.
                Also supports output to file and HTML formatted output
                Run tree --help for all options
                This extension is PPI compatible

Change-log:     ${TODAY} first version, ${VERSION} (${ME})
Current:        ${TODAY} first version, ${VERSION} (${ME})
EOF

###################################################
# Create base extension in temp dir               #
###################################################
find $TMPDIR/ -type d | xargs chmod -v 755;

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

cd $TMPDIR
find $TMPDIR -perm 777 -exec chmod 755 {} \;
find $TMPDIR -perm 555 -exec chmod 755 {} \;
find $TMPDIR -perm 444 -exec chmod 644 {} \;
find $TMPDIR -perm 666 -exec chmod 644 {} \;
find $TMPDIR -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR

cd /tmp/submit/

# mksquashfs $TMPDIR ${PACKAGE}.tcz
mksquashfs $TMPDIR ${PACKAGE}.tcz

cd $TMPDIR
sudo sh -c "find usr -not -type d > ${PACKAGE}.tcz.list"
sudo mv ../${PACKAGE}.tcz .
# sudo mv ../${PACKAGE}.tcz.dep .
sudo mv ../${PACKAGE}.tcz.info .

# Create md5 file
sudo sh -c "md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr

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

cd $TMPDIR-doc
find $TMPDIR-doc -perm 777 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 555 -exec chmod 755 {} \;
find $TMPDIR-doc -perm 444 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 666 -exec chmod 644 {} \;
find $TMPDIR-doc -perm 664 -exec chmod 644 {} \;
sudo chown -R root:root $TMPDIR-doc
cd ..
# mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz
mksquashfs $TMPDIR-doc ${PACKAGE}-doc.tcz

cd $TMPDIR-doc
sudo sh -c "find usr -not -type d > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz* .
#sudo mv /tmp/${PACKAGE}/${PACKAGE}-doc.tcz.info .

# Create md5 file
sudo sh -c "md5sum ${PACKAGE}-doc.tcz > ${PACKAGE}-doc.tcz.md5.txt"

# Cleanup temp directory
sudo rm -r -f usr

