#!/bin/sh
# From script by hurzli: http://tinycorelinux.net/12.x/x86/tcz/src/mbedtls/mbedtls.build
# Start in directory with the name of the latest version you want to build, eg.
# ~/src/mbedtls_ext/3.6.2

tc=$(grep CPE_NAME /etc/os-release 2>/dev/null | sed "s#CPE_NAME=##g;s#\"##g;s#cpe:/o:tinycore:tinycore_linux:##g;s#\..*#.x#g;"); test ${tc//.*} -ge 12 || exit;
arch=x86; test "$(uname -m 2>/dev/null)" == "x86_64" && arch=x86_64;
current=$(date +%Y/%m/%d);
startdir="`pwd`";
version=${startdir##*/};
last_version=$(ls -d -r -v ../?*.?*.?* | sed -n 2p);
last_version=${last_version#../}
last_version=${last_version%/}
jobs=$(ls -1d /sys/bus/cpu/devices/* | wc -l);
tce-load -i cmake compiletc squashfs-tools zsync || exit;
if [ ! -d "$startdir"/../../mbedtls-$version ]
then
  wget -c -O "$startdir"/../../mbedtls-$version.tar.bz2 https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-$version/mbedtls-$version.tar.bz2 || exit;
  tar -C "$startdir"/../../ -xjf "$startdir"/../../mbedtls-$version.tar.bz2 || exit;
fi
cd "$startdir"/../../mbedtls-$version;
if [ "$arch" == "x86" ]
then
  export CFLAGS='-march=i486 -mtune=i686 -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -fno-asynchronous-unwind-tables -Wno-error=format-truncation';
  export CXXFLAGS='-march=i486 -mtune=i686 -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -Wno-error=format-truncation';
else
  export CFLAGS='-mtune=generic -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -fno-asynchronous-unwind-tables -Wno-error=format-truncation';
  export CXXFLAGS='-mtune=generic -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -Wno-error=format-truncation';
fi
export LDFLAGS='-Wl,-O1';
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DUSE_SHARED_MBEDTLS_LIBRARY=On -DENABLE_TESTING=Off -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_LIBDIR=lib . || exit;
mkdir build;
make -j$jobs DESTDIR=build install;
for i in bin lib; do ( cd build/usr/local/$i && { for o in --strip-all --strip-unneeded --strip-debug -g; do sudo strip $o *; done; }; ); done;
mkdir summary;
mksquashfs build summary/mbedtls3-bin.tcz -all-root -e usr/local/include usr/local/lib;
mksquashfs build summary/mbedtls3-dev.tcz -all-root -e usr/local/bin $(cd build && ls usr/local/lib/*.*);
mksquashfs build summary/mbedtls3-lib.tcz -all-root -e usr/local/bin usr/local/include usr/local/lib/pkgconfig usr/local/lib/cmake $(cd build && ls usr/local/lib/*.a);
cd summary;
for i in mbedtls*.tcz; do md5sum $i > $i.md5.txt; unsquashfs -d '' -lc $i > $i.list; zsyncmake $i 2> /dev/null; done;
for i in bin dev; do echo mbedtls3-lib.tcz > mbedtls3-$i.tcz.dep; done;
cat<<EOF>mbedtls3-bin.tcz.info;
Title:		mbedtls3-bin.tcz
Description:	lightweight crypto and SSL/TLS library
Version:	$version
Author:		see website
Original-site:	https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy:	Apache License 2.0 or GPL-2.0-or-later
Size:		$(du -h mbedtls3-bin.tcz | cut -f 1)
Extension_by:	CNK
Tags:		HTTPS SSL TLS mbedtls PolarSSL
Comments:	binary files for benchmarking, testing and examples
		----
		mbed TLS (formerly known as PolarSSL) is a lean open source
		crypto library for providing SSL and TLS support in your programs.
		----
		Compiled for TC $tc $arch
		----
EOF
cat<<EOF>mbedtls3-dev.tcz.info;
Title:		mbedtls3-dev.tcz
Description:	lightweight crypto and SSL/TLS library
Version:	$version
Author:		see website
Original-site:	https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy:	Apache License 2.0 or GPL-2.0-or-later
Size:		$(du -h mbedtls3-dev.tcz | cut -f 1)
Extension_by:	CNK
Tags:		HTTPS SSL TLS mbedtls PolarSSL dev
Comments:	static libraries and headers
		----
		mbed TLS (formerly known as PolarSSL) is a lean open source
		crypto library for providing SSL and TLS support in your programs.
		----
		Compiled for TC $tc $arch
		----
EOF
cat<<EOF>mbedtls3-lib.tcz.info;
Title:		mbedtls3-lib.tcz
Description:	lightweight crypto and SSL/TLS library
Version:	$version
Author:		see website
Original-site:	https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy:	Apache License 2.0 or GPL-2.0-or-later
Size:		$(du -h mbedtls3-lib.tcz | cut -f 1)
Extension_by:	CNK
Tags:		HTTPS SSL TLS mbedtls PolarSSL
Comments:	shared libraries
		----
		mbed TLS (formerly known as PolarSSL) is a lean open source
		crypto library for providing SSL and TLS support in your programs.
		----
		Compiled for TC $tc $arch
		----
EOF

if [ -f "$startdir"/../$last_version/mbedtls3-dev.tcz.info ]
then
  log="`grep -A 99 ^Change-log: \"$startdir\"/../$last_version/mbedtls3-dev.tcz.info | sed 's/^Current:/\t/'`";
  for i in bin dev lib; do echo -e "${log}\nCurrent:\t$current $last_version -> $version" >> mbedtls3-$i.tcz.info; done;
else
  for i in bin dev lib; do echo -e "Change-log:\t\nCurrent:\t$current First version, $version" >> mbedtls3-$i.tcz.info; done;
fi
mv * "$startdir"/;
echo "Extension build complete. Install new mbedtls3-bin extension and run 'selftest' to confirm functionality.";
