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

# This is a fix to existing TCE not a compile

P=usbutils
USER=`cat /etc/sysconfig/tcuser`
LIST="squashfs-tools wget "
for Z in $LIST    
    do 
    su -c "tce-load -i $Z" $USER
done


su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://tinycorelinux.net/12.x/aarch64/tcz/usbutils.tcz \
http://www.linux-usb.org/usb.ids " $USER

unsquashfs $P.tcz
mv squash* $P

# compress the ids
gzip usb.ids
rm -rf $P/usr/local/share/$P/usb.ids*
mv usb.ids.gz $P/usr/local/share/$P

# fix install script
#############
rm -rf $P/usr/local/tce.installed/$P

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 ] || cp $LOOP/usb.ids.gz $HW/ 
gunzip $HW/usb.ids.gz                  ' > $P/usr/local/tce.installed/$P
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# add licence
mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2 ' > $P/usr/local/share/doc/$P/COPYING

# fix python script
PY=$P/usr/local/bin/lsusb.py
sed 's|python|python3|' -i $PY
sed 's|/usr/share/usb.ids|/usr/local/share/hwdata/usb.ids|' -i $PY

# 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
done

ls -hal

echo 'Title:          usbutils.tcz
Description:    usb tools 
Version:        usbutils 007
                usbids 20230824
Author:         usbutils Thomas Sailer,Johannes Erdfelt,David Brownell & Aurelien Jarno
                usbids Stephen J. Gowdy
Original-site:  https://github.com/gregkh/usbutils
                http://www.linux-usb.org/usb.ids
Copying-policy: usbutils:GPL v 3 usbids: none
Size:           304K
Extension_by:   aus9
Tags:           usb usb.ids lsusb
Comments:       Run on a USB host not on a USB device
                See FAQ at http://www.linux-usb.org/
                
                usb.ids updated to 20230824  
                If you wish to update run
                $ sudo update-usbids.sh
                To persist add to backup
                usr/local/share/hwdata/usb.ids

                Optional: load python3.11 for $ lsusb.py
                load bash for $ usb-devices

Change-log:     2020/09/12 usbutils v 007 usbids v 20200820  on 12.x
Current:        2020/09/12 updated usbids v 20230824 fix install script on 14.x  ' > $P.tcz.info

readelf -d $P/usr/local/bin/* | grep 'NEEDED'
# [libusb-1.0.so.0] libusb
# [libudev.so.0]  udev-lib dep of libusb 
readelf -d $P/usr/local/sbin/* | grep 'NEEDED' # Not an ELF file

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

# reboot and test

sudo find / -name usb.ids
# /mnt/sda1/buildsarm8/usbutils/src/usbutils/usb.ids
# /usr/local/share/hwdata/usb.ids
ls -al /usr/local/share/hwdata/usb.ids
# -rw-r--r--    1 root     root        724018 Oct  3 08:13 /usr/local/share/hwdata/usb.ids
sudo update-usbids.sh
# Connecting to www.linux-usb.org (204.68.111.100:80)
# saving to '/usr/local/share/hwdata/usb.ids.new'
# usb.ids.new          100% |****************************************************************************************************************************|  707k  0:00:00 ETA
# '/usr/local/share/hwdata/usb.ids.new' saved
# Done.
# ls -al /usr/local/share/hwdata
#-rw-r--r--    1 root     root        724018 Oct  3 08:16 usb.ids
# -rw-r--r--    1 root     root        724018 Oct  3 08:16 usb.ids.old









