#!/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=usbutils
V=007
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc wget squashfs-tools zsync xz libusb-dev automake bash libtool git "
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://mirrors.edge.kernel.org/pub/linux/utils/usb/$P/$SRC.tar.xz \
http://www.linux-usb.org/usb.ids " $USER
xz -d $P*xz && tar xvf $P*tar
cd $SRC
./configure --prefix=/usr/local --disable-static --localstatedir=/var --datadir=/usr/local/share/hwdata
#####################
# prefix:      /usr/local
# datarootdir: ${prefix}/share
# datadir:     /usr/local/share/hwdata
# mandir:      ${datarootdir}/man
# usb.ids:     /usr/local/share/hwdata/usb.ids
# zlib:              
# compiler:    gcc
# cflags:      -march=armv8-a+crc -mtune=cortex-a72 -Os -pipe
# ldflags:  
make check # takes seconds 
make install-strip DESTDIR=/tmp/$P  # takes seconds
cd /tmp
rm -rf $P/usr/local/share/man
rm -rf $P/usr/local/share/hwdata
rm -rf $P/usr/local/share/pkgconfig
# compress the ids
gzip usb.ids
mkdir -p $P/usr/local/share/$P
mv usb.ids.gz $P/usr/local/share/$P

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
echo '#!/bin/sh
HW=/usr/local/share/hwdata
LOOP=/tmp/tcloop/usbutils/usr/local/share/usbutils
[ -d $HW ] || mkdir -p $HW
[ -f $HW/usb.ids.gz ] || cp -p $LOOP/usb.ids.gz $HW/ 
' > $P/usr/local/tce.installed/$P 
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# TCZ them
#######  
LIST="$P "
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:          usbutils.tcz
Description:    usb tools 
Version:        1.0.23
Author:         Greg Kroah-Hartman..usbutils
                Stephen J. Gowdy....usb.ids
Original-site:  https://github.com/gregkh/usbutils
                http://www.linux-usb.org/usb.ids
Copying-policy: GPL v 3
Size:           292K
Extension_by:   aus9
Tags:           usb usb.ids lsusb
Comments:       USB utilities including lsusb 
                
                usb.ids updated to 20200820  
                If you wish to update run
                $ sudo update-usbids.sh
                To persist add to backup
                usr/local/share/hwdata/usb.ids

                Optional: load python2.7 for $ lsusb.py
                load bash for $ usb-devices

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

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

