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

# due to my lack of RAM addresses kernel module built on qemu for TC32
# only the packaging done on real TC32

FW=rtl8188eufw.bin
P=rtl8188eu-6.1.2-tinycore
SRC=rtl8188eu-gitMay4
USER=`cat /etc/sysconfig/tcuser`
WL=/usr/local/lib/modules/6.1.2-tinycore/kernel/drivers/net/wireless
MOD=8188eu.ko.gz

USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc git bc wireless-6.1.2-tinycore firmware-rtlwifi"
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

# no need to check for la files grab the 64 bit source, it compiles on TC32

cd /tmp
URL=http://repo.tinycorelinux.net/14.x/x86_64/tcz/src/rtl8188eu
su -c "wget $URL/rtl8188eu.tar.gz \
$URL/20230314-k-v6.3-rc2.patch " $USER
tar xvf $P.tar.gz
mv $P $SRC

# save src and inject into qemu TC32 and continue with a
# kernel prepped /usr/src/* dir and sym link
ln -s /usr/src/linux-6.1.2 /lib/modules/6.1.2-tinycore64/build

# module fails to build correctly until I do this
modprobe cfg80211 # its a dep of 8188eu

cd $SRC
patch -p1 < ../20230314-k-v6.3-rc2.patch
make all # 3 minutes....not stripped to reduce failures
# mkdir -p /lib/modules/6.1.2-tinycore/kernel/drivers/net/wireless
# make install
cd /tmp

# save 8188eu.ko and copy back into real TC32 for below under /tmp

# TCE
#####
# no instructions for unpack copy of firmware-rlwifi 
# no instructions for unpack of  wireless-6.1.2-tinycore

chown root:root 8188eu.ko LICENCE.rtlwifi_firmware.txt rtl8188eufw.bin
chmod 644 rtl8188eufw.bin

mkdir -p $P/$WL
mkdir -p $P/usr/local/lib/firmware/rtlwifi
gzip /tmp/8188eu.ko
cp /tmp/$MOD $P/$WL
cp /tmp/$FW $P/usr/local/lib/firmware/rtlwifi

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
/sbin/udevadm trigger
depmod -a 6.1.2-tinycore
modprobe 8188eu
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

mkdir -p $P/usr/local/share/doc/$P
echo 'GPL v2' > $P/usr/local/share/doc/$P/COPYING	
cp $SRC/README.md $P/usr/local/share/doc/$P/README.txt
cp LICENCE.rtl* $P/usr/local/share/doc/$P/

# 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
        find lib -not -type d >> /tmp/$Z.tcz.list
        sed 's|usr|/usr|g'    -i /tmp/$Z.tcz.list
        sed 's|lib|/lib|g'    -i /tmp/$Z.tcz.list
        sed 's|//|/|g'        -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

echo 'Title:          rtl8188eu-6.1.2-tinycore.tcz
Description:    wifi kernel module & fw for below devices
Version:        module: git-May4
                fw: Jan17
Author:         module: Ivan Ovborislav 
                fw: Realtek Semiconductor Corporation
Original-site:  module: https://github.com/ivanovborislav/rtl8188eu
                fw: https://git.kernel.org/pub/scm/linux/\
                kernel/git/firmware/linux-firmware.git
Copying-policy: module: GPL v2
                fw: accompanied
Size:           724K
Extension_by:   aus9
Tags:           wifi wlan wireless
Comments:       fw=firmware       
                Supported class N USB wireless devices
                TP-Link TL-WN722N V2/V3 High Gain 
                TP-Link TL-WN727N V5.20 
                TP-Link TL-WN725N V3 Nano 
                EDIMAX EW-7811Un V2 4 Nano 
                ASUS USB-N10 Nano B1 
                D-Link DWA-125 (rev.D)
                D-Link DWA-123 (rev.D)
                D-Link GO-USB-N150 Easy (rev.B)
                D-Link DWA-121 (rev.B)
                Realtek RTL8188EU 
                Realtek RTL8188ETV 
                802.11bgn Mini 
                ELECOM WDC-150SU2M 
                Sitecom WLA-1100 V2  
                MERCUSYS MW150US V2 Nano 
                Rosewill RNX-N150NUB Nano 

                simple instructions - insert dongle first pls
                $ sudo ifconfig eth0 down # cable can remain connected
                $ tce-load -i wifi rtl8188eu
                $ sudo wifi.sh # and follow prompts

                For module parameters, ids etc $ modinfo 8188eu 

                If you need power, managed mode etc
                see README.txt in /usr/local/share/doc/8188eu dir
                Those commands will need more TCEs downloaded and installed

                Optional dependency- wireless-db.tcz -read its info please
                                
                dmesg outputs a lot of info.

Change-log:    2023/09/28 2reduce deps-fw added on 14.x
Current:       2023/09/28     ' > $P.tcz.info

echo 'wireless-6.1.2-tinycore.tcz' > $P.tcz.dep

