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

BIN=rtl8188eufw.bin
BUILD=/lib/modules/4.19.10-tinycore64/build
CONF=config-4.19.10-tinycore64
K=/usr/src/linux-4.19.10
LOOP=/tmp/tcloop/firmware-rtlwifi/usr/local/share/doc/License
MOD=8188eu.ko
P=8188eu
SRC=rtl8188eu-master
SYM=Module.symvers-4.19.10-tinycore64
USER=`cat /etc/sysconfig/tcuser`
WL=/lib/modules/4.19.10-tinycore64/kernel/drivers/net/wireless

LIST="compiletc submitqc firmware-rtlwifi  xz elfutils-dev bc \
bash perl5 advcomp  "
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

# lwfinger download and unpack
###############################
cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://codeload.github.com/lwfinger/rtl8188eu/zip/master " $USER
unzip master

# until further notice cheat a little off wl build
##############################
su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://repo.tinycorelinux.net/10.x/x86_64/release/src/kernel/linux-4.19.10-patched.txz " $USER
su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://repo.tinycorelinux.net/10.x/x86_64/release/src/kernel/config-4.19.10-tinycore64 " $USER
su -c "/usr/local/bin/wget -nc --no-check-certificate \
http://repo.tinycorelinux.net/10.x/x86_64/release/src/kernel/Module.symvers-4.19.10-tinycore64.gz " $USER

xz -d linux-4.19.10-patched.txz && tar xvf linux-4.19.10-patched.tar
rm -rf *tar
mkdir /usr/src
mv linux-4.19.10  /usr/src
gunzip $SYM.gz
cd $K
make mrproper
cp /tmp/$CONF  .config
make oldconfig
make prepare
make modules_prepare
cp /tmp/$SYM Module.symvers
make SUBDIRS=scripts/mod
ln -s /usr/src/linux-4.19.10/ $BUILD

# module stuff
##############

# additional steps to leave 8188 Makefile unaltered
mkdir -p $WL
mkdir -p /etc/modprobe.d
mkdir -p /lib/firmware/rtlwifi

cd /tmp/$SRC
make all
make strip
cd /tmp

# TCZ
#####
mkdir -p $P/$WL
mv $SRC/$MOD $P/$WL
gzip $P/$WL/$MOD
mkdir -p $P/usr/local/lib/firmware/rtlwifi
mv $SRC/*bin $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
depmod -a
udevadm trigger
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
cp $SRC/COPYING $P/usr/local/share/doc/$P/
# no license showing in zip file for fw bin file
cp $LOOP/Realtek*txt $P/usr/local/share/doc/$P/
chmod 644 $P/usr/local/share/doc/$P/*

# trying u loc for fw bin file amended list

# 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 'wicd.tcz'  > $P.tcz.dep

echo 'Title:          8188eu.tcz
Description:    wifi module and one firmware ...as below
Version:        Kernel=4.19.10-tinycore64 - module=git-zip-20190117
Author:         lwfinger for kernel module and see below
Original-site:  https://github.com/lwfinger/rtl8188eu
Copying-policy: see below
Size:          344K   		
Extension_by:   aus9 at gmx dot com
Tags:           8188eu rtl8188eufw.bin wifi network adapter usb dongle
Comments:       The following devices are supported but try at own risk please
                usb 2357:010C TL-WN722N v2 (tested)....class N
                usb 056E:4008 from Elecom Coy Ltd
                usb 2001:331B from D-Link
                usb 2001:3311 from D-Link
                usb 2001:3310 from D-Link
                usb 2001:330F from D-Link
                usb 0DF6:0076 from Sitecom  Europe
                usb 07B8:8179 from AboCom Systems
                usb 0BDA:0179 RTL8188ETV Wireless LAN 802.11n Network Adapter
                usb 0BDA:8179 RTL8188EUS 802.11n Wireless Network Adapter
                (Load usb-utils if you wish to run $lsusb to get usb ids)

                I prefer wicd to obtain wifi.
                                
                After usb dongle is inserted I run
                $ tce-load -i 8188eu
                $ sudo /usr/local/etc/init.d/dbus start
                $ sudo /usr/local/etc/init.d/wicd start

                Click on the wicd icon, and assuming you see your router 
                name, click on properties and pulldown and choose WPA 1/2 
                (PASSPHRASE) assuming you use wpa2, input your passphrase 
                then click Ok. Next click connect.

                Optional--Top right corner is a triangle pointing down symbol
                - Preferences -> advanced settings you may like to click 
                dBm to get dBm instead of % connection strength?
                I prefer to un-tick show never connections. YMMV 

                If your device works for you, consider reading wicd info 
                on adding some stuff to your backup, and adding this TCZ
                to your current boot list.

                Good Luck Testing

                Info
                ##### 
                COPYING file is for module license = GPL v2
                Real*txt is the license for the firmware file and names the author
                Credits to coreplayer2 and bmarkus for supplying the fw
                license that I have copied over. Pathways explained in build script

                Example of correct hardware detection...from $ dmesg
                ####################################
                8188eu: loading out-of-tree module taints kernel.
                Chip Version Info: CHIP_8188E_Normal_Chip_TSMC_D_CUT_1T1R_RomVer(0)
                EEPROM ID = 0x8129
                usbcore: registered new interface driver r8188eu
      
                compiled for 10x
Change-log:     2018/05/17 first version used a restart daemon script
                2018/05/29 daemons were started by install script 
                2018/07/04 start daemons removed from install script 
                2018/07/08 8188 renamed to 8188eu  
Current:        2019/01/296  for 10x and follow new Makefile guide ' > $P.tcz.info

submitqc --libs
