#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

export CFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe" 
export CXXFLAGS="-march=armv8-a+crc -mtune=cortex-a72 -Os -pipe \
-fno-exceptions -fno-rtti"

P=pcre2
V=10.35
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync bzip2-dev readline-dev automake "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done
# make failed without automake for aclocal

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://ftp.pcre.org/pub/pcre/$SRC.zip" $USER
mkdir $P
unzip $SRC*zip
cd $SRC
mkdir build && cd  build
../configure --prefix=/usr/local --disable-static --localstatedir=/var \
--enable-unicode --enable-pcre2-16 --enable-pcre2-32 --enable-pcre2grep-libz \
--enable-pcre2grep-libbz2 --enable-pcre2test-libreadline --enable-jit
###########################################################################
# pcre2-10.35 configuration summary:
# Install prefix ..................... : /usr/local
# C preprocessor ..................... : gcc -E
# C compiler ......................... : gcc
# Linker ............................. : /usr/local/bin/ld
# C preprocessor flags ............... :
# C compiler flags  : -pthread -march=armv8-a+crc -mtune=cortex-a72 \
#######################-Os -pipe -fvisibility=hidden
# Linker flags ................... :
# Extra libraries .................... :
# Build 8-bit pcre2 library .......... : yes
# Build 16-bit pcre2 library ......... : yes
# Build 32-bit pcre2 library ......... : yes
# Include debugging code ............. : no
# Enable JIT compiling support ....... : yes
# Use SELinux allocator in JIT ....... : no
# Enable Unicode support ............. : yes
# Newline char/sequence .............. : lf
# \R matches only ANYCRLF ............ : no
# \C is disabled ..................... : no
# EBCDIC coding ...................... : no
# EBCDIC code for NL ................. : n/a
# Rebuild char tables ................ : no
# Internal link size ................. : 2
# Nested parentheses limit ........... : 250
# Heap limit ......................... : 20000000 kibibytes
# Match limit ........................ : 10000000
# Match depth limit .................. : MATCH_LIMIT
# Build shared libs .................. : yes
# Build static libs .................. : no
# Use JIT in pcre2grep ............... : yes
# Enable callouts in pcre2grep ....... : yes
# Enable fork in pcre2grep callouts .. : yes
# Initial buffer size for pcre2grep .. : 20480
# Maximum buffer size for pcre2grep .. : 1048576
# Link pcre2grep with libz ........... : yes
# Link pcre2grep with libbz2 ......... : yes
# Link pcre2test with libedit ........ : no
# Link pcre2test with libreadline .... : yes
# Valgrind support ................... : no
# Code coverage ...................... : no
# Fuzzer support ..................... : no
# Use %zu and %td .................... : auto
make -j5 # takes 1.5 minutes
make install-strip DESTDIR=/tmp/$P  # takes seconds
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
mkdir -p $P-dev/usr/local/bin
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a
# based on TC64 TCE below I do
mv $P/usr/local/bin/pcre2-config $P-dev/usr/local/bin

# main
#####
rm -rf $P/usr/local/share/man
cd $P/usr/local/share/doc/$P
rm -rf AUTHORS COPYING ChangeLog NEWS README html *txt
cd /tmp

# TCZ them
#######
LIST="$P $P-dev "
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
        zsyncmake $Z.tcz
done

ls -hal

echo 'Title:          pcre2-dev.tcz
Description:    pcre2 devs
Version:        10.35
Author:         (main) Philip Hazel (jit) Zoltan Herczeg
Original-site:  https://ftp.pcre.org
Copying-policy: see LICENCE in main TCE
Size:           16K
Extension_by:   aus9
Tags:           perl
Comments:       dev files

Change-log:     2020/09/10 Original v 10.35 12x
Current:        2020/09/10 ' > $P-dev.tcz.info

echo 'Title:          pcre2.tcz
Description:    perl5 compatible libraries
Version:        10.35
Author:         (main) Philip Hazel (jit) Zoltan Herczeg
Original-site:  https://ftp.pcre.org
Copying-policy: see LICENCE
Size:           536K
Extension_by:   aus9
Tags:           perl
Comments:       perl compatible regular expression libraries
                can optionally use the bzip2-lib and readline extensions

Change-log:     2020/09/10 Original v 10.35 12x
Current:        2020/09/10 ' > $P.tcz.info

echo 'pcre2.tcz
bzip2-dev.tcz 
readline-dev.tcz 
automake.tcz ' > $P-dev.tcz.dep
