#!/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=libusb
V=1.0.23
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync libudev-dev  "
for Z in $LIST    
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://github.com/$P/$P/releases/download/v$V/$SRC.tar.bz2" $USER
mkdir $P
tar jxvf $SRC*bz2
cd $SRC
./configure --prefix=/usr/local --enable-static=no --enable-udev
make check # takes seconds 
make install-strip DESTDIR=/tmp/$P  # takes seconds
cd /tmp

# dev
#####
mkdir -p $P-dev/usr/local/lib
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 

# 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:          libusb-dev.tcz
Description:    library to access USB devices 
Version:        1.0.23
Author:         https://github.com/libusb/libusb/blob/master/AUTHORS
Original-site:  https://github.com/libusb/libusb
Copying-policy: LGPL v 2.1
Size:           20K
Extension_by:   aus9
Tags:           usb 
Comments:       dev files

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

echo 'Title:          libusb.tcz
Description:    library to access USB devices 
Version:        1.0.23
Author:         https://github.com/libusb/libusb/blob/master/AUTHORS
Original-site:  https://github.com/libusb/libusb
Copying-policy: LGPL v 2.1
Size:           44K
Extension_by:   aus9
Tags:           usb
Comments:       usb 

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

echo 'libusb.tcz
libudev-dev.tcz ' > $P-dev.tcz.dep
echo 'libudev.tcz' > $P.tcz.dep

