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

# no flags needed so any arch

P=wireless-regdb
V=2025.10.07
SRC=$P-$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://www.kernel.org/pub/software/network/wireless-regdb
su -c "/usr/local/bin/wget -nc --no-check-certificate \
$URL/$P-$V.tar.xz " $USER 
tar xvf $P*xz

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

# tce.install..... earlier install script was testing for read-write dir. this time keep it simple
###########
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
P=wireless-regdb
SRC=/usr/local/share/$P/files

# regulatory domain config
##############################
FILE=/etc/modprobe.d/regdom.conf
[ -d /etc/modprobe.d ] || mkdir /etc/modprobe.d 
[ -f $FILE ] || cp $SRC/regdom.conf /etc/modprobe.d 
chown root:root $FILE
chmod 644       $FILE

# firmware setup for any arch- may result is duplicate file
########################################################
SRC=/usr/local/share/$P/files 
L=/lib/firmware
U=/usr/local/lib/firmware
# do not test for -w on dir just try both pathways
# may result in Rpi have 2 files
[ -d $L ] || mkdir -p $L
[ -d $U ] || mkdir -p $U
[ -f $L/regulatory.db ] || cp $SRC/regulatory.db $L
[ -f $U/regulatory.db ] || cp $SRC/regulatory.db $U
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 . -not -type d | cut -c 2- | sort > /tmp/$Z.tcz.list
	cd /tmp
done

ls -hal

cat >> $P.tcz.info <<'EOF'
Title:          wireless-regdb.tcz  
Description:    wireless regulatory domain firmware and country code config
Version:        2025/10/07
Author:         Luis R. Rodriguez, Johannes Berg & Michael Green
Original-site:  https://www.kernel.org/pub/software/network/wireless-regdb/
Copying-policy: accompanied
Size:           4.0K
Extension_by:   aus9 @linuxquestions.org
Tags:           cfg80211 regulatory.db firmware domain
Comments:       Read-writeable config is /etc/modprobe.d/regdom.conf 
                change "AU" to your country code. Your country laws
                may require you to use correct country. Persist any changes by 
                $ sudo echo 'etc/modprobe.d/regdom.conf' >> /opt/.filetool.lst 
                full reboot WITH backup. On reboot you can test by loading iw + 
                wireless-KERNEL TCEs then 
                $ iw reg get # you should see correct country details
                Other wireless TCEs not discussed here

                module cfg80211 depends on this TCE 
                                
Change-log:     2023/05/06 any arch v 2023-05-03 source was
                https://git.kernel.org/pub/scm/linux/kernel/git/sforshee/wireless-regdb.git
Current:        2025/10/11 any arch v 2025/10/07 source now
                https://www.kernel.org/pub/software/network/wireless-regdb
                install script changed + regulatory.db.p7s not needed
EOF

# no dep

submitqc --libs
rm -rf *.zsync
# reboot and test using iw TCE  $ iw reg get  -> country code should be what you set



