#!/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
###Get the localized application name of a certain .desktop file (indicated in the "file" variable, in the next line)
file="/usr/share/applications/trash.desktop"
#Get system language (to allow localization):
lang=$(locale | grep LANG | cut -d= -f2 | cut -d. -f1)
#hack to fix languages that are identified in .desktop files by only 2 characters, and not 4 (5 counting the _)
#comparing text that's before the "_" to the text that after that, converted to lower case, if it matches, use only the leters before the "_"
l1=$(echo $lang |cut -d_ -f1)
l2=$(echo $lang |cut -d_ -f2)
l2_converted=$(echo "${l2,,}")
if [ $l1 = $l2_converted ]; then lang=$l1; fi
 name2=$name1
 translated_name1=$(grep -o -m 1 "^Name\[$lang\]=.*" $file)
 [ -z "$translated_name1" ] && name1=$(grep -o -m 1 '^Name=.*' $file) || name1=$translated_name1
#if the desktop file has the string "Desktop Action" simply use the original untranslated name, to avoid risking using a translation that's not the name of the app
	grep -q "Desktop Action" $file && name1=$(grep -o -m 1 '^Name=.*' $file)
localized_trash=$(echo $name1|sed 's/.*\=//') 
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

#Localize "New Command" toolbar menu entry (that has a localized entry, but always keeps the original menu entry, that's only replaced by the localized version if that entry is deleted by the user
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
new_command=$"New _Command"
targetfile=~/.config/zzzfm/session
sed -i "s/cstm_6bf2815e-label=.*/cstm_6bf2815e-label=$new_command/g"  $targetfile

#Localize "File Search" toolbar icon
TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=zzzfm
file_search_toolbar_entry=$"_File Search"
targetfile=~/.config/zzzfm/session
#remove _
fixed_file_search_toolbar_entry=${file_search_toolbar_entry//_/}
sed -i "s/cstm_12757b81-label=.*/cstm_12757b81-label=$fixed_file_search_toolbar_entry/g"  $targetfile

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


