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

P=update-desktops
V=1.0
USER=`cat /etc/sysconfig/tcuser`

LIST="submitqc adwaita-icon-theme "
for Z in $LIST
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp

mkdir -p $P/usr/local/bin
echo '#!/bin/sh
sudo update-desktop-database /usr/local/share/applications/
sudo update-mime-database /usr/local/share/mime
' > $P/usr/local/bin/$P
chmod 755 $P/usr/local/bin/$P

mkdir -p $P/usr/local/share/pixmaps
LOOP=/tmp/tcloop/adwaita-icon-theme/usr/local/share/icons/Adwaita/48x48/actions/folder-new-symbolic.symbolic.png 
cp $LOOP $P/usr/local/share/pixmaps/$P.png

APP=$P/usr/local/share/applications
mkdir -p $APP
echo '[Desktop Entry]
Type=Application
Icon=update-desktops
Name=update-desktops
Categories=System;Utility;
Exec=update-desktops
StartupNotify=false
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/update-desktops.png' > $APP/$P.desktop

# tce.install
#############
mkdir -p $P/usr/local/tce.installed
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
# assists pcmanfm but may work for any file manager 
# where you load a TCE after the file manager
update-desktop-database /usr/local/share/applications/
update-mime-database /usr/local/share/mime
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# TCZ them
###########
LIST2="$P  "
for Z in $LIST2
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 'Title:          update-desktops.tcz
Description:    tool to refresh mime and desktops
Version:        1.0
Author:         aus9
Original-site:  TinyCore
Copying-policy: opensource 
Size:           4.0K         		
Extension_by:   aus9  
Tags:          mime desktops
Comments:   Some of us load TCEs after booting. File managers like
            pcmanfm and spacefm do not auto detect freshly loaded TCEs.
            If any new loaded TCE offer mime handling, consider 
            running this app.
            
            If you see the next line, it means no new desktops
            were detected.
            "The databases in /usr/local/share/applications/ 
            could not be updated."  ^ = (above-dir used below)
            
            Be aware that if you attempt to open something in a 
            file manager, and you have no option to open it with a 
            TCE.....check that your preferred desktop file mentions 
            your target mime type.
            
            mime types include txt mp3 mp4 pdf html and so on.
            Look under /above-dir/something.desktop for the line 
            that starts with MimeType.
            If not found, consider reporting by pm or forum post to 
            the downstream maintainer. 
            
            In pcmanfm and spacefm, it is not necessary to close and 
            restart it, to see new mime type TCE options after running 
            this exe
            
            You can copy from /tmp/tcloop/etc the bin file to 
            ~./local/bin/ if you prefer not to have this TCE in 
            your boot list       
                          
Change-log:     2020/07/31 Original 11x  v 1.00
Current:        2020/07/31 
' > $P.tcz.info

echo 'shared-mime-info.tcz
desktop-file-utils.tcz' > $P.tcz.dep

submitqc --libs
