#!/bin/bash
#Script to localize antiX Linux File Manager (zzzFM's) "add-ons": Common Bookmarks, Home Bookmark, Recent Files and Trash related entries, by PPC, 7/2/2023, GPL
        TEXTDOMAINDIR=/usr/share/locale
        TEXTDOMAIN=zzzfmlocalize  ###NOTE: this is just an example, the dev team is free to choose
        
trash_label=$"Trash"
trash_menu_entry=$"Send to Trash" 
restore_menu_entry=$"Restore" 
recents_label=$"Recent Files"

#make sure that zzzfm is not running (yes, this makes desktop icons disapear, if using a zzz desktop
#pkill zzzfm && sleep 0.1

#Generic variable(s)
targetfile=~/.config/zzzfm/session

#Localize Downloads Bookmark and path
label=$(echo $XDG_DOWNLOAD_DIR| cut -d/ -f4)
sed -i "s/cstm_6c99bdd2-label=.*/cstm_6c99bdd2-label=$label/g"  $targetfile
sed -i "s|cstm_6c99bdd2-z=.*|cstm_6c99bdd2-z=$HOME/$label|g"  $targetfile

#Localize Documents Bookmark and path
label=$(echo $XDG_DOCUMENTS_DIR| cut -d/ -f4)
sed -i "s/cstm_1f966a72-label=.*/cstm_1f966a72-label=$label/g"  $targetfile
sed -i "s|cstm_1f966a72-z=.*|cstm_1f966a72-z=$HOME/$label|g"  $targetfile

#Localize Pictures Bookmark and path
label=$(echo $XDG_PICTURES_DIR| cut -d/ -f4)
sed -i "s/cstm_23f8698b-label=.*/cstm_23f8698b-label=$label/g"  $targetfile
sed -i "s|cstm_23f8698b-z=.*|cstm_23f8698b-z=$HOME/$label|g"  $targetfile

#Localize Videos Bookmark and path
label=$(echo $XDG_VIDEOS_DIR| cut -d/ -f4)
sed -i "s/cstm_3be1d265-label=.*/cstm_3be1d265-label=$label/g"  $targetfile
sed -i "s|cstm_3be1d265-z=.*|cstm_3be1d265-z=$HOME/$label|g"  $targetfile

#Localize Music Bookmark and path
label=$(echo $XDG_MUSIC_DIR| cut -d/ -f4)
sed -i "s/cstm_1bd6d9d6-label=.*/cstm_1bd6d9d6-label=$label/g"  $targetfile
sed -i "s|cstm_1bd6d9d6-z=.*|cstm_1bd6d9d6-z=$HOME/$label|g"  $targetfile

#Localize Trash related entries 
#Trash bookmark
sed -i "s/cstm_3ca29516-label=.*/cstm_3ca29516-label=$trash_label/g"  $targetfile
#Send to Trash contextual menu entry
sed -i "s/cstm_25a291d5-label=.*/cstm_25a291d5-label=$trash_menu_entry/g"  $targetfile
#Restore from Trash contextual menu entry
sed -i "s/cstm_2b015761-label=.*/cstm_2b015761-label=$restore_menu_entry/g"  $targetfile

#localized Recent Files bookmark:
#use geany's localization string, for now (our own localization can be added later, when creating the localization fils for the "addons" o zzzfm...
export TEXTDOMAIN=geany;  recents_label=$(echo "$(gettext "Recent _Files")")
#remove any underscore
recents_label=${recents_label//_/}
#localize bookmark
sed -i "s/cstm_0f7a0cea-label=.*/cstm_0f7a0cea-label=$recents_label/g"  $targetfile

#Localize Home bookmark:
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
homie=$"Home"
sed -i "s/cstm_2b7f3cb1-label=.*/cstm_2b7f3cb1-label=$homie/g"  $targetfile

#Localize Details/Compact view toolbar buttons:
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
label=$"_Detailed"
sed -i "s/cstm_248656b5-label=.*/cstm_248656b5-label=$label/g"  $targetfile
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
label=$"_Compact"
sed -i "s/cstm_6c5d99cd-label=.*/cstm_6c5d99cd-label=$label/g"  $targetfile

#Localize (" > Wallpaper" that stands for "use currently selected image as Wallpaper" contextual menu entry):
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
label=$"Wallpaper:"
#remove :
fixed_label=${label//:/}
sed -i "s/cstm_6eabb9d5-label=.*/cstm_6eabb9d5-label= -> $fixed_label/g"  $targetfile

#Localize "Trash" bookmark using the localization from the trash.desktop
trash="Trash"
targetfile=~/.config/zzzfm/session
###Create need python script for python v3
cat << EOF > /tmp/pythonscript-v3.py
#!/usr/bin/python
import subprocess
from gi.repository import Gio

all_apps = Gio.AppInfo.get_all()  # Returns a list of DesktopAppInfo objects (see docs)

# For example, print display name of all apps and description, using "|" to devide the fields
for app in all_apps:
    print(app.get_display_name(), app.get_filename(), sep='|')
    ### did not implement  app.get_description()
EOF

chmod 755 /tmp/pythonscript-v3.py
localized_trash=$(/tmp/pythonscript-v3.py |grep trash.desktop|cut -d "|" -f 1)
sed -i "s/cstm_3ca29516-label=.*/cstm_3ca29516-label=$localized_trash/g"  $targetfile

#Localize the "Send to Trash" contextual menu entry, using zzzfm localization of "send to" and the previous localization of "Trash"
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
move_to=$"_Move To"
#remove _
fixed_move_to=${move_to//_/}
move_to_trash=$(echo $fixed_move_to $localized_trash)
sed -i "s/cstm_25a291d5-label=.*/cstm_25a291d5-label=$move_to_trash/g"  $targetfile

#Save flag, letting the system know that zzzfm is already localized
echo "localized" > ~/.config/zzzfm/zzzfm_already_localized


