#!/bin/sh

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

CFLAGS="-mtune=generic -Os -pipe"
CXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=iucode-tool
V=2.3.1
SRC=$P-$V # unpack changes from understem to hyphen
URL=https://gitlab.com/iucode-tool/releases/raw/master

USER=`cat /etc/sysconfig/tcuser`
LIST1="compiletc libcpuid-dev submitqc xz"
for Z in $LIST1
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "wget -nc --no-check-certificate \
$URL/iucode-tool_2.3.1.tar.xz" $USER

echo 'unpack source and build it'
xz -d $P*xz && tar xvf $P*tar
cd $SRC
./configure --prefix=/usr/local --with-default-firmware-dir=/lib/firmware/intel-ucode --sbindir=/usr/local/bin
make -j5 # seconds on a modern PC
make install-strip DESTDIR=/tmp/$P
cd /tmp

# change name of executable from understem to hyphen
mv $P/usr/local/bin/iucode* $P/usr/local/bin/$P
# strings $P/usr/local/bin/$P | grep cpuid  gives hits so looks OK

# no need for man page, create short licence
rm -rf $P/usr/local/share/man
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING

LIST2="$P "
for Z in $LIST2
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
done

ls -hal

echo 'Title:          iucode-tool.tcz
Description:    tool for Intel microcodes
Version:        2.3.1
Author:         Henrique de Moraes Holschuh
Original-site:  https://gitlab.com/iucode-tool
Copying-policy: GPL v2
Size:           32K          		
Extension_by:   aus9
Tags:           intel Intel microcodes
Comments:       see http://forum.tinycorelinux.net/index.php/topic,26142.0.html
                online non-TC man page here
                http://man.he.net/man8/iucode-tool

                start with $iucode-tool -S
            
Change-log:     2023/03/13  first version 2.3.1
Current:        2023/03/13  ' > $P.tcz.info

readelf -d $P/usr/local/bin/$P | grep 'NEEDED'
# 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6] -> which is  TCB
# but add libcpuid

echo 'libcpuid.tcz' > $P.tcz.dep

submitqc --libs