#!/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=rtl8188eu
SRC=rtl8188eu-src
USER=`cat /etc/sysconfig/tcuser`
WL=/lib/modules/6.6.8-tinycore64/kernel/drivers/net/wireless
MOD=8188eu.ko

LIST="compiletc submitqc bc elfutils-dev gettext-dev glibc_apps git perl5  "
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.6.8 /lib/modules/6.6.8-tinycore64/build

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

mv $P $SRC

cd $SRC
# make error .../tmp/rtl8188eu/os_dep/linux/wifi_regd.c: In function 'rtw_regd_init':
#           /tmp/rtl8188eu/os_dep/linux/wifi_regd.c:409:36: error: 'REGULATORY_IGNORE_STALE_KICKOFF' undeclared (first use in this function)
#           409 |         wiphy->regulatory_flags |= REGULATORY_IGNORE_STALE_KICKOFF
# net says edit line 408 for that file ref https://github.com/morrownr/88x2bu-20210702/issues/165#issuecomment-1730133899

FILE=/tmp/$SRC/os_dep/linux/wifi_regd.c
# sed line too ugly so manually adjusted and replaced
rm -rf $FILE  
# inject manually correctly c file as per src submission
make all # 3M 15s....not stripped to reduce failures
mkdir -p /lib/modules/6.6.8-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.6.8-tinycore64
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

# 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:          rtl8188eu.tcz
Description:    wifi kernel module (8188eu) for below devices
Version:        git-20240305
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 rtl8188eu
                $ 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, TCE name=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
               2023/05/04 14x kernel, new upstream src, make FW a dep 
Current:       2024/03/06 15x kernel & TCE name rtl8188eu           ' > $P.tcz.info

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

submitqc --libs 
