#!/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="-mtune=generic -Os -pipe -fno-exceptions -fno-rtti"

P=8188eu
SRC=rtl8188eu
USER=`cat /etc/sysconfig/tcuser`
WL=/lib/modules/6.1.2-tinycore64/kernel/drivers/net/wireless
MOD=$P.ko

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

# kernel preparations
######################
#  kernel environment script no longer works as per Juanito post
# http://forum.tinycorelinux.net/index.php/topic,24946.msg158828.html#msg158828
# after putting back my saved compiled /usr/src/linux*
# ln -s /usr/src/linux-6.1.2 /lib/modules/6.1.2-tinycore64/build

cd /tmp
su -c "git clone  \
https://github.com/ivanovborislav/rtl8188eu.git  " $USER

URL=https://raw.githubusercontent.com/ivanovborislav/document/main/patch
su -c "/usr/local/bin/wget --no-check-certificate  \
$URL/20230314-k-v6.3-rc2.patch " $USER

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-tinycore64/kernel/drivers/net/wireless/
# make install
cd /tmp

# TCZ
#####
mkdir -p $P/$WL
cp $SRC/$MOD $P/$WL
gzip $P/$WL/$MOD

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
udevadm trigger
depmod -a 6.1.2-tinycore64
modprobe 8188eu
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $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

# TCZ them
#######  submitqc rebuilds list
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
	cd /tmp
done

ls -hal

echo 'Title:          8188eu.tcz
Description:    wifi kernel module for below devices
Version:        git-20230309
Author:         Ivan Ovborislav
Original-site:  https://github.com/ivanovborislav/rtl8188eu
Copying-policy: GPL v2
Size:           764K
Extension_by:   aus9
Tags:           wifi wlan wireless
Comments:       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 
                Optional dependency- wireless-db read its info please

                $ sudo ifconfig eth0 down # cable can remain connected
                $ tce-load -i wifi 8188eu
                $ sudo wifi.sh # and follow prompts

                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
                                
                dmesg now outputs a lot more info.

Change-log:    2018/05/17 first version, 8188, used a restart daemon script
                          source was https://github.com/lwfinger/rtl8188eu
               2018/05/29 daemons were started by install script 
               2018/07/04 start daemons removed from install script 
               2018/07/08 TCE name 8188eu  
               2019/01/29 10x kernel and modify build as no kernel headers 
               2020/05/26 11x kernel with git-2020may25, last lwfinger source
Current:       2023/05/04 14x kernel, new upstream src, make FW a dep      ' > $P.tcz.info

echo 'wireless-6.1.2-tinycore64.tcz
firmware-rtlwifi.tcz' > $P.tcz.dep

submitqc --libs 
