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

######################################################
# Prepare extension creation                         #
######################################################
#download and install dependencies
tce-load -wi python3.11.tcz
tce-load -wi openssl-dev
tce-load -i c-ares-dev
tce-load -wi jansson-dev.tcz
tce-load -wi libxml2-dev.tcz
#download and install the compile tools
tce-load -wi libffi7.tcz
tce-load -wi llvm-bin.tcz
# tce-load -wi bash.tcz
tce-load -wi autoconf.tcz
tce-load -wi automake.tcz
tce-load -wi libtool-dev.tcz
tce-load -wi compiletc.tcz
tce-load -wi squashfs-tools.tcz

######################################################
# Configure extension creation parameters            #
######################################################
# Variables
TODAY=`date +%Y/%m/%d`
PACKAGE="nghttp2"
VERSION="1.65.0"
DESCRIPTION="Implementation of http/2 and its header compression algorithm."
DOCDESCRIPTION="Documentation part of nghttp2"
DEVDESCRIPTION="Development files part of nghttp2"
AUTHORS="https://github.com/nghttp2/nghttp2/blob/master/AUTHORS"
HOMEPAGE="https://nghttp2.org/"
LICENSE="https://github.com/nghttp2/nghttp2/blob/master/COPYING"
ME="rhermsen"
TAGS="nghttp2 HTTP2"
DOCTAGS="man pages nghttp2 HTTP2"
DEVTAGS="development nghttp2 HTTP2"
DESTDIR=/mnt/mmcblk0p2/tce/dest/${PACKAGE}
TMPDIR=/mnt/mmcblk0p2/tce/submit/${PACKAGE}
SRCDIR=/mnt/mmcblk0p2/tce/${PACKAGE}
ARCH=`uname -m`

# Workdir
sudo rm -r ${SRCDIR}/${PACKAGE}-${VERSION} 2>/dev/null
[ ! -d ${SRCDIR} ] && mkdir ${SRCDIR}

# Source
cd ${SRCDIR}
[ ! -f   ${SRCDIR}/${PACKAGE}-${VERSION}.tar.gz ] && wget https://github.com//${PACKAGE}//${PACKAGE}/releases/download/v${VERSION}/${PACKAGE}-${VERSION}.tar.gz
tar xzf ${PACKAGE}-${VERSION}.tar.gz

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

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

# Export variables needed for compilation
ARCH=`uname -m`
if [ $ARCH == "armhf" ]
then
echo
export CFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-Os -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
elif [ $ARCH == "aarch64" ]
then
export CPPFLAGS="-DNDEBUG"
export CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
export CFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72 -flto -fuse-linker-plugin -fno-exceptions"
export CXXFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72"
export CXXFLAGS="-Os -pipe -march=armv8-a+crc -mtune=cortex-a72 -flto -fuse-linker-plugin -fno-exceptions -fno-rtti"
echo
else
exit 1
fi
export LDFLAGS="-Wl,-O1"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

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

./configure --prefix=/usr/local \
--localstatedir=/var \
--disable-static \
--enable-lib-only

# --verbose

# make
time make -j4
mkdir -p $DESTDIR
time 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/lib
mkdir -p  ${TMPDIR}/usr/local/share/doc/${PACKAGE}

cp ${SRCDIR}/${PACKAGE}-${VERSION}/COPYING ${TMPDIR}/usr/local/share/doc/${PACKAGE}/
# mv ${DESTDIR}/usr/local/bin/* ${TMPDIR}/usr/local/bin/
mv ${DESTDIR}/usr/local/lib/libnghttp2.so* ${TMPDIR}/usr/local/lib/


###################################################
# Create info file                                #
###################################################
cd /mnt/mmcblk0p2/tce/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:   polikuo
Tags:           ${TAGS}
Comments:       nghttp2 is an implementation of HTTP/2 and its header compression
                algorithm HPACK in C.

Change-log:     2020/06/30 first version (juanito)
                2023/08/22 1.41.0 --> 1.55.1 (polikuo)
                ${TODAY} Updated version, ${VERSION} (${ME})
Current:        ${TODAY} Updated 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:   polikuo
Tags:           ${TAGS}
Comments:       nghttp2 is an implementation of HTTP/2 and its header compression
                algorithm HPACK in C.

Change-log:     2020/08/21 first version (juanito)
                2022/03/04 1.41.0 --> 1.47.0 (polikuo)
                ${TODAY} Updated version, ${VERSION} (${ME})
Current:        ${TODAY} Updated version, ${VERSION} (${ME})
EOF
fi


###################################################                                                              
# Create .dep file                                #                                                              
###################################################
cat <<EOF> /mnt/mmcblk0p2/tce/submit/${PACKAGE}.tcz.dep
libxml2.tcz
EOF

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

mv ${DESTDIR}/usr/local/share/man ${TMPDIR}-doc/usr/local/share/${PACKAGE}/
mv ${DESTDIR}/usr/local/share/doc ${TMPDIR}-doc/usr/local/share/${PACKAGE}/
mv ${DESTDIR}/usr/local/share/nghttp2 ${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:       nghttp2 is an implementation of HTTP/2 and its header compression
                algorithm HPACK in C.

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


# Move files to dev extension
mkdir -p $TMPDIR-dev/usr/local/lib/pkgconfig

mv ${DESTDIR}/usr/local/include ${TMPDIR}-dev/usr/local/
# mv ${DESTDIR}/usr/local/lib/*.a ${TMPDIR}-dev/usr/local/lib
mv ${DESTDIR}/usr/local/lib/pkgconfig/*.pc ${TMPDIR}-dev/usr/local/lib/pkgconfig

###################################################
# Create info file                                #
###################################################
if [ $ARCH == "armhf" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:          ${PACKAGE}-dev.tcz
Description:    ${DEVDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   polikuo
Tags:           ${DEVTAGS}
Comments:       nghttp2 is an implementation of HTTP/2 and its header compression
                algorithm HPACK in C.

Change-log:     2020/06/30 first version (juanito)
                2023/08/22 1.41.0 --> 1.55.1 (polikuo)
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
elif [ $ARCH == "aarch64" ]
then
cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:          ${PACKAGE}-dev.tcz
Description:    ${DEVDESCRIPTION}
Version:        ${VERSION}
Author:         ${AUTHORS}
Original-site:  ${HOMEPAGE}
Copying-policy: ${LICENSE}
Size:           ${size}
Extension_by:   polikuo
Tags:           ${DEVTAGS}
Comments:       nghttp2 is an implementation of HTTP/2 and its header compression
                algorithm HPACK in C.

Change-log:     2020/08/21 first version (juanito)
                2022/03/04 1.41.0 --> 1.47.0 (polikuo) --enable-static
                ${TODAY} updated version, ${VERSION} (${ME})
Current:        ${TODAY} updated version, ${VERSION} (${ME})
EOF
fi

###################################################                                                              
# Create .dep file                                #                                                              
###################################################
cat <<EOF> /mnt/mmcblk0p2/tce/submit/${PACKAGE}-dev.tcz.dep
${PACKAGE}.tcz
libxml2-dev.tcz
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 /mnt/mmcblk0p2/tce/submit/

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

cd $TMPDIR
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${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 | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-doc.tcz.list"
sudo mv ../${PACKAGE}-doc.tcz* .
#sudo mv /mnt/mmcblk0p2/tce/${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

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

llvm-strip --strip-debug $TMPDIR-dev/usr/local/lib/libnghttp2.a

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

cd $TMPDIR-dev
sudo sh -c "find usr -not -type d | sed 's/\(.*$\)/\/\1/g' > ${PACKAGE}-dev.tcz.list"
sudo mv ../${PACKAGE}-dev.tcz* .
#sudo mv /mnt/mmcblk0p2/tce/${PACKAGE}/${PACKAGE}-dev.tcz.info .

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

# Cleanup temp directory
sudo rm -r -f usr

