#!/bin/sh
# test we are root
if [ "$USER" != "root" ] ; then
   echo "Run as root please, exiting." 
   exit 1                                                      
fi

# modinfo cfg80211 | grep firmware
# firmware:       regulatory.db
# no flags we grab files

P=wireless-regdb
V=2023-05-03
SRC=wireless-regdb-master-$V

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

cd /tmp
URL=https://git.kernel.org/pub/scm/linux/kernel/git/sforshee/wireless-regdb.git/snapshot
su -c "/usr/local/bin/wget \
$URL/$P-master-$V.tar.gz " $USER 
tar xvf *$V*gz

# using u local lib fw gives dmesg..
# platform regulatory.0: Direct firmware load for regulatory.db failed with error -2
mkdir -p $P/lib/firmware
cp $SRC/regulatory.db $P/lib/firmware
cp $SRC/regulatory.db.p7s $P/lib/firmware

mkdir -p $P/usr/local/share/$P
echo 'options cfg80211 ieee80211_regdom="AU"' > $P/usr/local/share/$P/regdom.conf

# tce.install
###########
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
P=wireless-regdb
LOOP=/tmp/tcloop/$P/usr/local/share/$P
FILE=/etc/modprobe.d/regdom.conf
[ -d /etc/modprobe.d ] || mkdir /etc/modprobe.d 
[ -f $FILE ] || cp $LOOP/regdom.conf /etc/modprobe.d 
chown root:root $FILE
chmod 664 $FILE
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
cp $SRC/LICENSE $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
    sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

cat >> $P.tcz.info <<'EOF'
Title:          wireless-regdb.tcz  
Description:    regulatory.db firmware
Version:        2023-05-03
Author:         Luis R. Rodriguez, Johannes Berg & Michael Gree
Original-site:  https://git.kernel.org/pub/scm/linux/kernel/git/sforshee/wireless-regdb.git
Copying-policy: accompanied
Size:           8.0K
Extension_by:   aus9 
Tags:           cfg80211 regulatory.db firmware
Comments:       wireless firmware. Read writeable config is
                /etc/modprobe.d/regdom.conf 
                change "AU" to your country code if needed

                This TCE must load before wireless-KERNEL TCE
                as module cfg80211 depends on this TCE firmware 
                
Change-log:     2023/05/06 any arch v 2023-05-03
Current:        2023/05/06 
EOF

# no dep

submitqc --libs
