#!/bin/sh
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);
version=2.26.0;
tce-load -wi cmake compiletc squashfs-tools submitqc;
wget -O /tmp/mbedtls-$version.tar.gz https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/v$version.tar.gz;
sudo rm -rf /tmp/mbedtls-$version; tar -C /tmp -xzf /tmp/mbedtls-$version.tar.gz;
cd /tmp/mbedtls-$version;
export CFLAGS='-mtune=generic -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -Wno-error=format-truncation';
export CXXFLAGS='-mtune=generic -Os -pipe -fuse-linker-plugin -flto -fno-exceptions -fno-rtti -Wno-error=format-truncation';
export LDFLAGS='-Wl,-O1';
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DUSE_SHARED_MBEDTLS_LIBRARY=On -DENABLE_TESTING=Off -DCMAKE_BUILD_TYPE=Release .;
mkdir build;
make 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/mbedtls-bin.tcz -e usr/local/include usr/local/lib;
mksquashfs build summary/mbedtls-dev.tcz -e usr/local/bin $(cd build && ls usr/local/lib/*.so*);
mksquashfs build summary/mbedtls-lib.tcz -e usr/local/bin usr/local/include $(cd build && ls usr/local/lib/*.a);
cd summary;
for i in mbedtls*.tcz; do md5sum $i > $i.md5.txt; unsquashfs -l $i > $i.list; done;
for i in bin dev; do echo mbedtls-lib.tcz > mbedtls-$i.tcz.dep; done;
cat<<EOF>mbedtls-bin.tcz.info;
Title:          mbedtls-bin.tcz
Description:    lightweight crypto and SSL/TLS library
Version:        $version
Author:         see list of sites below
Original-site:  https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy: Apache License 2.0
Size:           $(du -h mbedtls-bin.tcz | cut -f 1)
Extension_by:   hurzli
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.
                ----
                This extension contains:
                mbedtls-$version - Apache License 2.0 - https://github.com/ARMmbed/mbedtls/archive/refs/tags/v$version.tar.gz
                ----
                Compiled running TC $tc $arch
Change-log:     ----
Current:        $current First version, $version
EOF
cat<<EOF>mbedtls-dev.tcz.info;
Title:          mbedtls-dev.tcz
Description:    lightweight crypto and SSL/TLS library
Version:        $version
Author:         see list of sites below
Original-site:  https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy: Apache License 2.0
Size:           $(du -h mbedtls-dev.tcz | cut -f 1)
Extension_by:   hurzli
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.
                ----
                This extension contains:
                mbedtls-$version - Apache License 2.0 - https://github.com/ARMmbed/mbedtls/archive/refs/tags/v$version.tar.gz
                ----
                Compiled running TC $tc $arch
Change-log:     ----
Current:        $current First version, $version
EOF
cat<<EOF>mbedtls-lib.tcz.info;
Title:          mbedtls-lib.tcz
Description:    lightweight crypto and SSL/TLS library
Version:        $version
Author:         see list of sites below
Original-site:  https://www.trustedfirmware.org/projects/mbed-tls
Copying-policy: Apache License 2.0
Size:           $(du -h mbedtls-lib.tcz | cut -f 1)
Extension_by:   hurzli
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.
                ----
                This extension contains:
                mbedtls-$version - Apache License 2.0 - https://github.com/ARMmbed/mbedtls/archive/refs/tags/v$version.tar.gz
                ----
                Compiled running TC $tc $arch
Change-log:     ----
Current:        $current First version, $version
EOF
