#!/bin/bash

        TEXTDOMAINDIR=/usr/share/locale
        TEXTDOMAIN=icewm-manager-gui

if [ "$(wmctrl -l | sed -n 's/^\(..*\)  ..* '$"Basic IceWM Settings Manager"'/\1/p')" != "" ]; then
  wmctrl -F -a $"Basic IceWM Settings Manager"
  exit 0
fi

###Text shown in the script (for easy localization/edition, it's placed in variables):
main_window_title=$"Basic IceWM Settings Manager"
main_window_text=$"Select what you want to change from the options below. If an option fails to work, please try clicking it again."
main_window_text=$"If an option fails to work, please try clicking it again."
clock_field=$"Clock"
clock_on_off=$"Show clock on/off"
clock_12_24h=$"12h/24h time"
clock_seconds=$"Show seconds on clock on/off"
clock_LED=$"Show clock in LED on/off"
sys_info_field=$"System information shown in System Tray"
sys_net=$"Show Network information on System Tray on/off"
sys_ram=$"Show RAM information on System Tray on/off"
sys_cpu=$"Show CPU, RAM and SWAP  information on System Tray on/off"
window_managment_field=$"Window Management"
title_icon=$"Show Application icon on window title bar on/off"
center_win=$"Centre new large windows on/off"
moving_osd=$"Show indication of position when moving windows  on/off"
remember_win=$"Click a Window to memorize its position"
startup_field=$"Auto Start"
add_remove_main_button=$"Add or Remove an application from IceWM start file"
toolbar_settings_field=$"Toolbar generic settings"
auto_hide=$"Auto-hide toolbar on/off"
double_hight=$"Double height toolbar on/off"
top_bottom=$"Toolbar on top/bottom of screen"
names_on_toolbar=$"Show window names on toolbar on/off"
tim=$"Manage quick launch toolbar icons"
show_desktop=$"'Show Desktop' icon on/off"
workspace_toggle=$"Workspace indicator on/off"
workspace_manager=$"Workspace Manager"
menu_field=$"Menu"
personal_menu_manager=$"Manage Personal Menu entries"
look_feel=$"Look and feel"
wallpaper=$"Wallpaper"
appearance=$"Appearance"
select_theme=$"Select IceWM Theme"
add_to_desktop=$"Selected app icon to place on the (zzz) Desktop"
add_remove_text=$"Do you want to add or to remove an entry to IceWm's startup? Current startup file"
add_startup_confirmation=$"The line was added to the $HOME/.icewm/startup file. It will start automatically the next time you start  antiX using the IceWM desktop"
add_startup_command_title=$"Enter command to be added to IceWM's startup file"
add_startup_command_text=$"Enter command you want to run at IceWM's startup. Note: an ampersand/& will automatically be appended to the end of the command"
remove_startup_text="Select command to be removed from the startup file. Note: all entries with the selected command will be deleted!"
remove_button=$"Remove"
remove_confirmation=$"The line was removed from $HOME/.icewm/startup file. The related application will no longer start automatically the next time you start  antiX using the IceWM desktop"
themes_title=$"IceWM Themes"
select_theme_text=$"Double click its name to select a new Theme:"
current_theme_text=$"The current IceWM Theme is "
keyboard_shortcuts=$"Manage keyboard shortcuts"
warning=$"Warning"
icewm_only_text=$"This script is meant to be run only in an IceWM desktop"
reset_text=$"Reset to default contents"
show_only_used_network_devices=$"Show only used network devices on/off"
collapse=$"Collapse Toolbar button on/off"
weather=$"Show Weather icon on/off"
manualedit=$"Manually edit config files"
personal_menu_toggle=$"Personal sub-menu on/off"
change_time_date=$"Set time and date"
personal_entries_on_the_first_layer=$"Personal entries on the first layer of menu on/off"
run_menu_entry=$"Run menu entry on/off"
conky=$"Show/hide Conky resource indicator on the desktop"
show_icewm_preferences_menu=$"Icewm Settings in menu on/off"
show_xkill=$"Xkill menu entry on/off"
toolbar_width_and_horizontal_position_window_title=$"IceWM Toolbar width and horizontal position"
toolbar_field=$"Toolbar"
system_tray_field=$"System tray"
compositor=$"Toggle Compositor on/off"
weather_tooltip=$"This button adds/removes an icon to the toolbar that displays the weather on a text window, using the wttr.in web service, that tries to auto-detect your current location"
doubleheight_toolbar_tooltip=$"You can't directly manage IceWM's toolbar height. You can choose double height/normal height, but, other than that, the height of the toolbar is set by the Theme that is being used (that you can change by clicking the theme button on the '$look_feel' tab)"
show_only_used_network_devices_tooltip=$"By default, 2 network information squares are shown in the system tray, one for ethernet connections and another for wi-fi connections. This option allows to save toolbar space and display just the Internet connection that is being used (making the information easier to read), or restore the default setting, showing both squares. When no internet connection is detected, no network information square will be displayed in the toolbar, if this option is selected (but when network is available, the square will appear again)"
collapse_tooltip=$"This feature, when enabled, allows the toolbar to shrink to the size of a small arrow, on the corner of the screen. When you click that arrow, or press the 'Windows'/Super key, the toolbar is restored back to it's normal size. This allows better use of screen space, particularly in very small screens"
workspace_manager_tooltip=$"This allows users to manage virtual workspaces and change the name of existing workspaces permanently and/or change the number of workspaces available for the current session. If you don't use virtual desktops, you can disable this indicator and save a bit of toolbar space"
workspace_toggle_tooltip=$"This allows users to hide/show the virtual desktop indicator in the toolbar. Even if the virtual desktop indicator is not shown, you can still switch between virtual desktops, using keyboard shortcuts or any other way"
show_desktop_tooltip=$"This allows user to hide/show the toolbar icon (by default it's right next to the menu button, on the top left). When clicked this icon toggles between showing the currently non minimized windows or showing the desktop background" 
tim_tooltip=$"This launches the window that allows users to add, remove or move quick-launch icons to the toolbar. Please note that you can't drag and drop toolbar quick-launch icons (nor move them around). You can also add/remove quick-launch icons using app-select (the app launcher/app finder available in the menu)"
names_on_toolbar_tooltip=$"This allows users to toggle having both the icons and names of currently running windows on the taskbar section of the the toolbar, or having just their icons. Displaying only icons does save toolbar space, and is used by default in many Operating Systems... but it can lead to confusion if you have multiple windows of the same app, like a word processor, open"
toolbar_width_and_horizontal_position_window_title_tooltip=$"This opens a window that allows you to select the percentage of horizontal space the toolbar will use and also it's horizontal position (note that it is always shown by default on the window as being in the Center, but it can also be on the Left or on the Right). Changes only produce effects after you click the 'Ok' button"
top_bottom_tooltip=$"This allows you to have the toolbar on the bottom of the screen (the default place) or on the top of the screen. After moving the toolbar to the top, you should restart your applications, so their top section is not hidden by the toolbar. Please note that IceWM does not allow the toolbar to be placed vertically on the sides of the screen"
auto_hide_tooltip=$"This toggles between always showing the toolbar (the default setting) or hiding it when it's not being used. To see the toolbar again simply move the mouse pointer to the place where it's hiding (by the default, the bottom of the screen) and it will appear, or press the Windows/Super key. When not being used, the toolbar will then automatically hide. Please note that after you toggle this selection you should resize or restart your apps, so they recognize the available screen space was changed"
sys_net_tooltip=$"This allows to display (the default) or hide the square(s) that show network information on the system tray. Note: if you double left click this information squares, by default, you summon the Conman network manager's window."
sys_ram_tooltip=$"This allows to display (the default) or hide the square that displays detailed information about the use of your RAM on the system tray. (Note: that general information about your RAM use is already displayed in another information square by default)"
sys_cpu_tooltip=$"This allows to display on the system tray (the default) or hide the square that displays detailed information about the use of CPU, RAM, Swap, CPU Temperature and frequency. Note: every time you double left click this information square, by default, you summon the Task Manager's window"
clock_on_off_tooltip=$"This allows to show (the default) or hide the clock shown on the toolbar. Note: when you double left click the clock, by default, you summon the antiX Calendar's window. When you right click the clock, you can select the way the time is shown, including if the date is to be displayed, for example"
clock_12_24h_tooltip=$"This toggles the toolbar clock mode between displaying time in 12h or 24h. Note that this does not change the way time is displayed system wide, only on the toolbar clock"
clock_seconds_tooltips=$"Show or hide seconds on the toolbar clock. Having seconds always displayed can be a bit distracting, but, on the other hand it can also be helpful as an easy way to measure small amounts of time. Note that this does not change the way time is displayed system wide, only on the toolbar clock"
reset_text_tooltip=$"This allows to reset clock to the original settings, related to showing seconds, date, 12/24h format. Note: this does not affect using or not the LCD type of clock display"
clock_LED_tooltip=$"This enables or disables (the default) using special numbers, created to usually mimic old LCD clock displays, to show the time. Note: the aspect of the LCD's can vary according to the IceWM theme being used. Also, LCD can't display some data, like, for example, the full date"
change_time_date_tooltip=$"This allows you to run the same window that antiX Control Centre uses to set time and date or select your time-zone. If asked to, you'll have to enter your password. IMPORTANT NOTE: Changing time and date, of course, affects the way all window managers, not just IceWM, show the time"
title_icon_tooltip=$"This allows to show or hide applications icons on the windows on the left side of their title bars. Some IceWM themes enable or disable this setting by default. Here you can force the icon to always be displayed or always be hidden"
center_win_tooltip=$"In IceWM, by default windows open on random places, in order not to cover completly previous windows. The option allows users to select to always display large windows on the center of the screen or to use the default behaviour"
moving_osd_tooltip=$"By default, when the user moves a window, IceWM displays it's exact coordinates on the bottom center of the screen (to help fine tune the position and use it later). Using this option the user can disable or enable that behaviour"
remember_win_tooltip=$"Clicking this option will instantly start the GUI program that allows IceWM to memorise the selected window's position. The mouse pointer will change to a '+'. Click anywhere on the window you want to 'memorize' and the GUI's main window will appear, so you can confirm the selection (by clicking 'Ok'), cancel the action or select a different window to 'memorize'."
personal_menu_manager_tooltip=$"The Personal Menu is where you can Pin your favourite or most used applications, for quicker access. This option opens the window for the GUI program that allows you to add, remove or move application from that list. Note: app-select (the app launcher/app finder available in the menu) also allows users to add or remove applications to/from the Personal Menu."
personal_menu_toggle_tooltip=$"The Personal Menu is, by default, showed as a sub-menu of the antiX Menu. This option allows you to completely disable that sub-menu, either because you don't use it and don't want to waste menu space on it or because you want to directly display your 'personal' applications on the first layer of the antiX menu (you can manage that with the button below this one)"
personal_entries_on_the_first_layer_tooltip=$"The Personal Menu is, by default, showed as a sub-menu of the antiX Menu. This option allows you to display your 'Personal' apps directly on the first layer of the menu, for quicker access. Note that, to save menu space if you enable this feature, it's advisable to use the option above this one to make sure the 'Personal sub-menu' is not shown. Even if your 'Personal' apps are shown on the first layer of the menu, editing your Personal menu list can never damage your antiX menu.  WARNING: please take care not to display your 'Personal' entries on the first layer of the menu if that makes the menu taller than the available screen space, because that makes the menu a bit harder to use, since it's top will be off screen"
show_xkill_tooltip=$"By default, the antiX Menu always shows, near the bottom, an entry that allows users to force close windows (handy if the application is frozen). If you have no need for this option, you can disable it, or enable it again any time you want. When you click this antiX Menu entry, the mouse pointer will turn into an 'x'. Any window you click next will be automatically closed. If you don't want to close any window, press the 'Esc' key and the mouse pointer will be back to normal"
run_menu_entry_tooltip=$"By default, the antiX Menu always shows, near the bottom, an entry that shows a window that allows users to run any command, or start any application by typing it's executable's name. If you have no need for this option, you can disable it, or enable it again any time you want"
show_icewm_preferences_menu_tooltip=$"By default, the antix Menu always shows, near the bottom, a sub-menu that allows access to the manual edition of several IceWM settings. If you have no need for this option (since the most used options are already managed using this window), you can disable it (to make the menu more streamlined and simpler to use), and enable it again any time you want. Note: by default, this menu entry allows access to the IceWM 'Themes' menu, that allows to change Themes instantly. When you select to change any setting in the 'Preferences' sub-menu, you have to always click the last entry, that saves the changes, and usually, you'll also have to restart IceWM, so the changes produce effects"
wallpaper_tooltip=$"This opens the window that allows you to select the background of the desktop. It can be any image or even a simple colour."
appearance_tooltip=$"This opens LXappearance's window, that allows you to select the GTK theme (that manages the interior contents of some windows), the icon set, the way the mouse pointer looks, etc. Note: selecting a light or dark GTK theme  can be used in conjunction with a IceWM Theme (that you can select by clicking the button below this one), in order to have a 'light/dark mode'"
conky_tooltip=$"This enables you to show or hide the conky (the system information that is, by default displayed on the top right corner of your desktop's background. Note that conky can be (manually) edited to look almost any way you want, and to display several things, including the Weather, Rss News feeds, etc- acting almost like a group of widgets"
keyboard_shortcuts_tooltip=$"This opens a window that allows you to add, remove or edit keyboard shortcuts used in IceWM"
add_to_desktop_tooltip=$"This opens an app-select window (that can take a few moments to load). When you select any application on that window it's icon will instantly be added to the desktop (if the currently used desktop allows using desktop icons) Please note that this also works on rox... desktops, but in a quirky way"
select_theme_tooltip=$"This opens a window that allows you to select a IceWM theme. Themes manage the way the toolbar, the menu and the window decorations (this means the window title bar and it's buttons and window frame look). The selection window remains open until you close it, allowing you to select the theme you like best. Note: selecting a light or dark IceWM theme can be used in conjunction with the Appearance window (available by clicking the button above this one), in order to have a 'light/dark mode'"
compositor_tooltip=$"Enable or disables using the video compositor. Enabling video composition allows having visual effects, like fading in/fading out windows, shadows, etc. It can also solve some video problems. Using a compositor, however does use a bit of system resources. If you have a weak computer, not using a compositor can help it run a bit faster"
add_remove_main_button_tooltip=$"This opens a window that allows you to add an application (selected using app-select, that can that a few moments to load), add a command (typed by you), or remove any line, to/from IceWM's 'startup' configuration file. Any application or command that's listed on this file is run every time the Window Manager starts. NOTE: the Window Manager configuration file is not the same as antiX's desktop-session startup file. Both of these files manage the commands that are run when antiX starts. The desktop-session startup file affects all window managers, not just IceWM, and it has it's own GUI, available in antiX Menu > Applications > antiX"
keyboard_layout_button=$"Toggle indicator of keyboard layout on/off"
keyboard_layout_tooltip=$"Permanently enable (default setting) or disable the keyboard language indicator flag (it's called 'fbxkb'), that is displayed in the system tray. If you use only one keyboard layout, you probably have really no need for it, and it just stays there, using system resources. WARNING: changing this setting produces effects on all Window Managers, not just IceWM"
recent_files=$"Recent Files"
manage_recent_files_list=$"Select number of recent files (displayed in the menu)"
clear_recent_files_list=$"Clear antiX's recent files history"
manage_recent_files_list_tooltip=$"This displays a window that allows you to select exactly how many recent files should be displayed in the antiX IceWM Menu- In that window, select exactly the number of files you want to see and click the 'Ok' button. If you cancel or close that window, the number of recent files to display will be '0' (but the list of recent files won't be deleted, you can later reset it to any number you want)"
clear_recent_files_list_tooltip=$"This displays a window that allows you to completely delete the list of recent files that is displayed by antiX. A confirmation window will appear. If you click the 'Delete' button, a notification window will inform that the procedure was finished. WARNING: This is done system wide, not just in IceWM's Recent files menu"
applications_menu_label=$"Show 'Applications' sub-menu OR 'Categories' on the first layer of the menu"
applications_menu_tooltip=$"This toggles between showing the default 'Applications' sub-menu OR directly showing all aplication 'Categories' on the first layer of the antiX menu. The 'Categories' option does make the menu much taller, but allows faster access to almost all applications."
display_time_date_label=$"Display Date and time"
display_time_date_tooltip=$"This allows users to select exactly what will be displayed in the 'Clock' section of the toolbar: day of the week, day of the month, month, year and 12 or 24h clocks, with or without seconds. Note that the toolbar displays time and date in a single line, so the more elements it has, the more horizontal space that section of the toolbar will use."
edit_menu_label=$"Show/hide menu entries"
edit_menu_tooltip=$"This allows users to show or hide the applications that are shown in the Applications or Personal menus, via a GUI that asks consecutive questions and allows users to select from an alphabetical list of all applications  that have valid a .desktop file. You can use this option to hide from  apps you don't want to see, but that should not be uninstalled, like system applications, etc. NOTE: this option does not manage the list of apps pinned directly on the first layer of the menu (ex: Terminal, File Manager)- the only way to do that is manually editing IceWM's 'menu' configuration file, by clicking the '$manualedit' button - please use that option only if you know exactly what you are doing, and always back up the original configuration file before editing it"

###How to add new features to 'IceWM CC':
#1- create translatable string  variables in the section above, 1 for the button, 1 for the tooltip (section ###Text shown in the script (for easy localization/edition, it's placed in variables):)
#2- if not calling an external script, create a bash function in the section directly below this one, in the /tmp/sourcefunctions file (section #######CREATE FILE WITH FUNCTIONS TO BE CALLED FROM MAIN WINDOW:)
#3- create an entry in the section "##### Create IceWM CC buttons: "
#4- add the entry created in the previous step in the relevant section of "###Contents of all categories: " 

#######CREATE FILE WITH FUNCTIONS TO BE CALLED FROM MAIN WINDOW:

cat > "/tmp/sourcefunctions" << 'FILE_TEMPLATE_A'
#!/bin/bash

        TEXTDOMAINDIR=/usr/share/locale
        TEXTDOMAIN=icewm-manager-gui

toogle_system_info_square_on_system_tray()
{
###Manage the System Info toolbar square that shows information about CPU/RAM/SWAP usage:
###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarShowCPUStatus="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return
}

toogle_RAM_info_square_on_system_tray()
{
###Manage the System Info toolbar square that shows information about CPU/RAM/SWAP usage:
###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarShowMEMStatus="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowMEMStatus:
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return
}

toogle_Network_info_square_on_system_tray()
{
###Manage the System Info toolbar square that shows information about NETWORK usage:
###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarShowNetStatus="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowMEMStatus:
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return
}

toggle_seconds()
{
Show/hide seconds on clock
file=$HOME/.icewm/prefoverride
text="TimeFormat="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found, use default values
    echo ${text}%H:%M:%S  >> $file
fi

#Toggle value:
if grep -F  ":%S" "$file"; then
    sed -i "s/\:%S.*/ /" $file
else
    sed -i "s/\:%M.*/:%M:%S/" $file
 fi
 
#restart icewm, to see the change:
 icewm -r

}

toggle12h24h()
{
Show/hide seconds on clock
file=$HOME/.icewm/prefoverride
text="TimeFormat="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found, use default values
    echo ${text}%H:%M:%S  >> $file
fi

#Toggle value:
if grep -F  "%H" "$file"; then
	sed -i 's/%H/%l/' $file
   else
    sed -i 's/%l/%H/' $file
 fi
 
#restart icewm, to see the change:
 icewm -r

}

toggle_clock_LED()
{
###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarClockLeds="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return	
}

display_time_date()
{
#Localizable Variables:
none=$"None"
short_weekday=$"Short weekday"
long_weekday=$"Long weekday"
day=$"Day"
short_month=$"Short month"
long_month=$"Long month"
short_year=$"Short year"
long_year=$"Long year"
twofourh=$"24H clock without seconds"
onetwoh=$"12H clock without seconds"
twofourhsec=$"24H clock with seconds"
onetwohsec=$"12H clock with seconds"
title=$"IceWM Toolbar's clock & date settings"
Week_day_label=$"Day of the week"
time_label=$"Time"
date1_label=$"Date1"
date2_label=$"Date2"
date3_label=$"Date3"
apply_button=$"Apply and exit"
note=$"Note: this window always starts by displaying the default selections- they do not reflect the current configuration!"
note2=$"Items marked as '$none' will be ignored and won't take any space on the toolbar."
note3=$"The $date1_label, $date2_label and $date3_label dropboxes allow you to select what date elements will be shown, and in which order."
note4=$"'Short' and 'Long' options mean the shorter or longer version of the date element- ex: 'Mon' instead of 'Monday', etc."

#Contents of dropboxes, that users can select from:
weekday_options="$none!$short_weekday !$long_weekday "
date_options="$none!$day !$short_month !$long_month !$short_year !$long_year "
clock_options="$twofourh !$onetwoh !$twofourhsec !$onetwohsec !$none"

#Get user selection:
date_time_selection=$(yad --center --fixed --borders=10 --window-icon=/usr/share/icons/papirus-antix/48x48/categories/clock.png --title="$title" --text="<span foreground='red'> $note </span>\n $note2 \n $note3 \n $note4 \n" --form --columns=5 \
--field="$Week_day_label::"CB "$weekday_options" \
--field="$date1_label::"CB "$date_options" \
--field="$date2_label::"CB "$date_options" \
--field="$date3_label::"CB "$date_options" \
--field="$time_label::"CB "$clock_options" --button="$apply_button!gtk-apply!")

#if no selection is made, exit the script:
if [ -z "$date_time_selection" ]; then
    exit 1
fi

#Process user selection:
time_date=$(echo $date_time_selection| sed "s/$none//g"  | sed "s/$short_weekday/%a/g" | sed "s/$long_weekday/%A/g" | sed "s/$day/%d/g" | sed "s/$short_month/%b/g"| sed "s/$long_month/%B/g" | sed "s/$short_year/%y/g" | sed "s/$long_year/%Y/g"| sed "s/$twofourh/%H:%M/g" | sed "s/$onetwoh/%I:%M %p/g"| sed "s/$twofourhsec/%H:%M:%S/g"| sed "s/$onetwohsec/%I:%M:%S %p/g" |  tr -d '\|')

#Remove any previous value from the config file:
sed -i '/^TimeFormat=/d' $HOME/.icewm/prefoverride
#Use selected values in the config file:
echo TimeFormat=\"$time_date\"  >> $HOME/.icewm/prefoverride
 
#restart icewm, to see the change:
icewm -r
}

toggle_clock()
{
###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarShowClock="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return	
}

icon_on_title_bar()
{
file=$HOME/.icewm/prefoverride
text="ShowMenuButtonIcon="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
	
autohide_toolbar()
{
file=$HOME/.icewm/prefoverride
text="TaskBarAutoHide="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}

doubleheight_toolbar()
{
file=$HOME/.icewm/prefoverride
text="TaskBarDoubleHeight="
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
	
top_toolbar()
{
text="TaskBarAtTop="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
	
center_large_windows()
{
text="CenterLarge="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}1 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
	
ShowMoveSizeStatus()
{
text="ShowMoveSizeStatus="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
	
TaskBarShowWorkspaces()
{
text="TaskBarShowWorkspaces="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}
		
TaskBarShowShowDesktopButton()
{
text="TaskBarShowShowDesktopButton="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}0 >> $file
fi
#Toggle TaskBarShowCPUStatus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}	
	
TaskbarButtonWidthDivisor()
{
text="TaskBarShowWindowTitles"
file=$HOME/.icewm/prefoverride
#Toggle
if grep -F  "${text}" "$file"; then
    sed -i /$text/d $file
else
	echo TaskBarShowWindowTitles=0 >> $HOME/.icewm/prefoverride
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}	
	
Select_Theme()
{
current_theme=$(cat ~/.icewm/theme| head -n 1| cut -d '=' -f 2  | cut -d "\"" -f 2)

#list available icewm themes and save the result to a text file called icewm_themes.list, in tht /tmp/ folder:
icewm -l > /tmp/icewm_themes.list
sed -i "s/\/home\/$USER\/.icewm\/themes//g" /tmp/icewm_themes.list
sed -i 's/\usr\/share\/icewm\/themes\///g' /tmp/icewm_themes.list
sed -i 's/\/default.theme//g' /tmp/icewm_themes.list
sed -i 's/^\///g' /tmp/icewm_themes.list

#Select theme from list
selected_theme=$(yad --no-buttons --window-icon=/usr/share/pixmaps/numix-square/icewm.png --text="${current_theme_text}: $current_theme" --title="$themes_title" --width=600 --height=550 --center --separator=" " --list  --column="$select_theme_text"  < /tmp/icewm_themes.list)

#If a selection was not done, exit
if [ -z "$selected_theme" ]; then exit
fi
#remove (trailing) space from selection
trimmed_selected_theme=${selected_theme::-1}
	echo 1 DID YOU SELECT ${trimmed_selected_theme}???
#if selection does not include .theme, append default.theme to it's end
if [[ ${trimmed_selected_theme} != *".theme"* ]];then
	# selection does not contain a slash in the name, appending default name
    trimmed_selected_theme="${trimmed_selected_theme}/default.theme"
fi
	echo DID YOU SELECT \"${trimmed_selected_theme}\"??? 

# Parse result from list of available themes, select the first match
result=$(icewm -l | grep -m 1 "$trimmed_selected_theme")
	echo FOUND: $result
#make edition to the config file
echo Theme=\"$result\" > $HOME/.icewm/theme
#restart IceWM
icewm -r
Select_Theme
	}	
	
ice_startup()
{
add_startup_appselect=$"Add application from a list"
add_startup_command=$"Add a (manual) command"
remove_startup=$"Remove a line"
startup_add_remove_title=$"Edit IceWM startup file"
startup_add_command=$"Command to be added to IceWM startup file"

	local add_remove_main_button=$add_remove_main_button
####GUI script to manage IceWM startup (add/remove applications from the start of an OS that uses IceWM as Window Manager)
yad --title="$startup_add_remove_title" --window-icon=/usr/share/pixmaps/numix-square/icewm.png --height=400 --width=550 --center --text=$"\n $add_remove_text $HOME/.icewm/startup):" --button="$add_startup_appselect":1 --button="$add_startup_command":2 --button="$remove_startup":3 --text-info <   ~/.icewm/startup --show-uri --fontname="ubuntu 12"
### wait for a button to be pressed then perform the selected function
main_selection=$?

if [[ $main_selection -eq 1 ]]; then
 selected=$(app-select --select| cut -d '|' -f 5| tail -1)
 #If a selection was not done, exit
	if [ -z "$selected" ]; then exit
	fi
	
	if echo "$selected" | grep 'Overall:'; then  exit
	fi
	
  echo "$selected  &" >> ~/.icewm/startup
  # yad --title="${add_remove_main_button}"  --window-icon=/usr/share/pixmaps/numix-square/icewm.png --fixed --center --text="$add_startup_confirmation" --button=" x "
ice_startup
fi

if [[ $main_selection -eq 2 ]]; then
 selected=$(yad --window-icon=/usr/share/pixmaps/numix-square/icewm.png --title="$startup_add_command" --center --width=500 --text="$add_startup_command_text" --form --field ':CE') 
 
# Remove trailing | from the end of the selection:
 selected=${selected::-1}
 
#If a selection was not done, exit
	if [ -z "$selected" ]; then exit
	fi
 
#Check if command already includes "&" in the end, if not, add it
 end_of_content=$(echo $selected| awk 'NF>1{print $NF}')
  if [ $end_of_content = "&" ]; then
   result="command already ends in an &, doing nothing"
   else
   #command did not end in an &, adding it
   selected=$(echo $selected "&")
  fi
 
 echo "$selected" >> ~/.icewm/startup
# yad --title="${add_remove_main_button}"  --window-icon=/usr/share/pixmaps/numix-square/icewm.png --center --text="$add_startup_confirmation" --button=" x "
ice_startup
fi


if [[ $main_selection -eq 3 ]]; then
  #### remove an  entry to startup file:
 #List contents of startup file, removing first line (with /bin/bash) and also any commented line
 cat ~/.icewm/startup| grep -v "#\!/bin/bash" | grep -o '^[^#]*' > /tmp/startuplist
 #clean up temporary file
 echo "" > /tmp/good
 #allow file to be truncated (i.e. append to the end of the line, instead of into a new line)
 truncate -s-1 /tmp/good
 #cicle trough the temporary file's contents and create /temp/good with everything into the same line, separed by "!", so it ca be used by yad drop box
 while read -r line; do
    echo -n ${line}\!  >> /tmp/good
 done < /tmp/startuplist
 #Show yad selection window, so user select what entry will be removed
 selection=$(yad --center --title="$remove_startup"  --window-icon=/usr/share/pixmaps/numix-square/icewm.png --text="${remove_startup_text}\n" --form --field="${remove_button}:CB" < /tmp/good)

 # Remove trailing | from the end of the selection:
 selection=${selection::-1}
 
 #process only untiul the first "&"
 b=${selection%\&*}
 line_number=$(cat ~/.icewm/startup | fgrep -n "$b"| cut -d ':' -f 1 | head -n 1)

#If a selection was not done, exit
	if [ -z "$line_number" ]; then exit
	fi

 #Remove all entries with this contents from the startup file (it's missing the -i flag)
 sed -i "${line_number}d" ~/.icewm/startup
 #yad --title="$remove_startup"  --window-icon=/usr/share/pixmaps/numix-square/icewm.png --fixed --center --text="${remove_confirmation}" --button=" x "
 ice_startup
fi
	}	
	
send_to_desktop()
{
selected=$(app-select --select| cut -d '|' -f 1| tail -1)
 #If a selection was not done, exit
	if [ -z "$selected" ]; then exit
	fi
 cp "$selected"  /"$XDG_DESKTOP_DIR"/
	}		
		
TaskBarShowCollapseButton()
{
text="TaskBarShowCollapseButton="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo TaskBarShowCollapseButton=0 >> $file
fi
#Toggle TaskBarShowCPUStaus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}

NetStatusShowOnlyRunning()
{
text="NetStatusShowOnlyRunning="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo NetStatusShowOnlyRunning=0 >> $file
fi
#Toggle TaskBarShowCPUStaus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}

wttr()
{
	
cat > "$HOME/.icewm/antix-weather.sh" << 'FILE_TEMPLATE'
#!/bin/bash
geoip="$(wget -O- -q http://geoip.ubuntu.com/lookup)"
area="$(sed -r 's/.*<RegionName>(.*?)<\/RegionName>.*/\1/g' <<< $geoip)"
corrected_area=$(echo "${area// /_}")
 langu=$(locale | grep LANG | cut -d= -f2 | cut -d. -f1)
l1=$(echo $langu |cut -d_ -f1)
WINID=$(wmctrl -lx | grep 'Wtrr.in' | awk 'NR==1{print $1}')
if [ $WINID ]; then  wmctrl -ia $WINID & else roxterm --hide-menubar --title "Wtrr.in" --zoom=0,85 --geometry 130x40 -e bash -c "curl 'http://wttr.in/$corrected_area?lang=$l1'; read -N1;" & fi

FILE_TEMPLATE

file=$HOME/.icewm/toolbar
if cat $file | grep antix-weather.sh; then
   # echo found, remove line
   sed -i "/antix-weather.sh/d" $file
   else
   # echo not found, add line
   echo "prog \"Wttr.in\" /usr/share/icons/papirus-antix/24x24/apps/org.gnome.Weather.png bash $HOME/.icewm/antix-weather.sh"  >> $file
fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		

}

show_personal_submenu()
{
#Script to Toggle on/off the "personal menu" submenu on icewm's main menu
file=$HOME/.icewm/menu
line_content=$(grep -Fiw menufile <$file | grep -Fiw personal | grep -Fiw preferences-desktop-user.png)
line_number=$(grep -Fiwn menufile <$file | grep -Fiw personal | grep -Fiw preferences-desktop-user.png|cut -d: -f1)
first_character_in_line=${line_content:0:1}
	if [ "$first_character_in_line" == "#" ]; then #line is commented out already. Uncommententing it
		sed -i '/preferences-desktop-user.png personal/ s/^#//' $file
			else # line exists  and it is not commented, commenting it
			sed -i  "$line_number"' s/^/#/' "$file"
	fi
 #Exit function:
return			
}

show_personal_menu_entries_on_first_layer_of_menu()
{
#Script to Toggle on/off the "personal menu" submenu on icewm's main menu
file=$HOME/.icewm/menu
string="include personal"
line=2
if grep -Fiw "include personal" <$file ; then
  echo found entry, removing it...
  sed -i "/$string/d" $file
else
echo not entry found, adding it...
sed -i "$line i include personal" $file
fi
 #Exit function:
return			
}

Show_run_menu_entry()
{
text="ShowRun="
file=$HOME/.icewm/prefoverride
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ShowRun=0 >> $file
fi
#Toggle TaskBarShowCPUStaus (if it exists in prefoverride):
if grep -F  "${text}"1 "$file"; then
    sed -i "s/${text}.*/${text}0/" $file
elif grep -F  "${text}"0 "$file"; then
    sed -i "s/${text}.*/${text}1/" $file
 fi
#restart icewm, to see the change:
 icewm -r
#Exit function:
return		
	}

Show_preferences_submenu()
{
fulltexton="ShowSettingsMenu=1"
text="ShowSettingsMenu="
file=$HOME/.icewm/preferences
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep ^$fulltexton; then
    #echo text found, that means the preferences submenu is on, turning it off:
	sed -i "s/ShowSettingsMenu=1/ShowSettingsMenu=0/g" $file
	sed -i "s/ShowFocusModeMenu=1/ShowFocusModeMenu=0/g" $file
	sed -i "s/ShowThemesMenu=1/ShowThemesMenu=0/g" $file
     #restart icewm, to see the change:
	icewm -r
  else
    #echo not found, that means the preferences submenu is off, turning it on:
	sed -i "s/ShowSettingsMenu=0/ShowSettingsMenu=1/g" $file
	sed -i "s/ShowFocusModeMenu=0/ShowFocusModeMenu=1/g" $file
	sed -i "s/ShowThemesMenu=0/ShowThemesMenu=1/g" $file
	#restart icewm, to see the change:
	icewm -r
fi
#Exit function:
return		
	}
		
show_xkill()
{
#Script to Toggle on/off the "personal menu" submenu on icewm's main menu
file=$HOME/.icewm/menu
string="xkill"
#Check if string exists in  file, if it does not, add it and make sure it's set to 0 (its 1 be default):
if cat $file | grep xkill | grep \#; then
    echo found and it is commented, making sure its not commented out
    line_number=$(cat $file | grep -n prog | grep -n xkill | cut -d ':' -f 1 | head -n 1)
    #uncomment all lines with the text xkill (that can be dangerous, if xkill is contained on a comment, for example)
    sed -i "/xkill/ s/^#//" $file
else
   echo not commented, commenting it
   #comment out line with string- it can be superflous if xkill is contained on a comment, for example:
   sed -i '/xkill/s/^/#/g' $file
  # file=$HOME/.icewm/menu
  # line_number=$(cat $file | grep -n prog | grep -n xkill | cut -d ':' -f 1 | head -n 1)
  # sed -i "${line_number}s/./#&/" $file
fi
 #Note: there's no need to restart icewm to see the change. The menu is updated by icewm automatically, on the fly
 #Exit function:
return			
}		

applications_menu_toggle()
{
#Script to switch between displaying the Applications sub-menu OR the Application Categories directly on the first layer of the IceWM menu
#search if the icewm menu file has a line that starts with menufile and includes also the word menu-applications:
grep -P '(?=^menufile)(?=.*menu-applications)' ~/.icewm/menu
#If it does:
if [ $? -eq 0 ]; then
    # If grep found a match, display a warning
    echo " Match found - The Applications sub-menu IS currently being show on the menu"
    line_number=$(grep -n -P '(?=^menufile)(?=.*menu-applications)' ~/.icewm/menu| cut -d ':' -f 1)
    #comment out the line
    sed -i "${line_number}s/^/#/" ~/.icewm/menu
    #add the new line
    sed -i "${line_number}ainclude menu-applications" ~/.icewm/menu
    echo " Now the IceWM menu should show all the aplication categories directly on the first layer"
  #if it dies not:    
  else
	echo "No match- The Applications sub-menu IS NOT currently being show on the menu"
	#making sure that the categories are not displayed in the first layer of the menu:
	sed -i '/include menu-applications/d' ~/.icewm/menu
	#uncomment the line that shows the Applications sub-menu:
	line_number=$(grep -n -P '(?=^#menufile)(?=.*menu-applications)' ~/.icewm/menu| cut -d ':' -f 1) 
	sed -i "${line_number}s/^.//" ~/.icewm/menu
	echo Now you should have the Applications sub-menu available in the IceWM menu, and not the application categories
fi	
}

toolbar_width()
{
###strings to localize:
toolbar_width_window_title=$"IceWM Toolbar Width"
position_description=$"Position"
center=$"center"
left=$"left"
right=$"right"

###first make sure that it exists in prefoverride, then make sure it's set to 0
file=$HOME/.icewm/prefoverride
text="TaskBarWidthPercentage="
#Check if string exists in  file, if it does not, add it and make sure it's set to 100 (its the default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}100 >> $file
fi

text="TaskBarJustify="
#Check if string exists in  file, if it does not, add it and make sure it's set to 100 (its the default):
if cat $file | grep ^$text; then
   # echo found, making sure its not commented out
    sed -i "/$text/s/^#//g" $file
else
   # echo not found
    echo ${text}"left" >> $file
fi

current_size=$(sed -n '/TaskBarWidthPercentage/p' $HOME/.icewm/prefoverride| cut -f2- -d=| head -n1 | cut -d " " -f1)
yad --button="OK" --window-icon=/usr/share/pixmaps/numix-square/icewm.png --width=350 --center --title="$toolbar_width_window_title" --form --separator="," \
--field="% ::NUM" $current_size!25..100!1!0 \
--field="$position_description::CB" "$center!$left!$right" \
"" "" ""| while read line; do
POSITION=`echo $line | awk -F',' '{print $2}'`
if [ "$POSITION" = $center ]; then POSITION="center"; fi
if [ "$POSITION" = $left ]; then POSITION="left"; fi
if [ "$POSITION" = $right ]; then POSITION="right"; fi

SIZE=`echo $line | awk -F',' '{print $1}'`
#Toggle TaskBarWidthPercentage:
sed -i "s/TaskBarWidthPercentage=.*/TaskBarWidthPercentage=${SIZE}/" $HOME/.icewm/prefoverride
sed -i "s/TaskBarJustify=.*/TaskBarJustify=${POSITION}/" $HOME/.icewm/prefoverride
#restart icewm, to see the change:
icewm -r
toolbar_width
done
}	

keyboard_indicator()
{
	#script to enable/disable antiX Linux's "system tray's keyboard language icon", also enabling/disabling it from ~/.desktop-session/startup, by making sure  it's entry is uncommentend/commented), by PPC, GPL license, 2023

file=$HOME/.desktop-session/startup
line_content=$(grep -Fiw fbxkb-start <$file)
line_number=$(grep -Fiwn fbxkb-start <$file |cut -d: -f1)
first_character_in_line=${line_content:0:1}

if [ "$(pidof fbxkb)" ]; then
#	echo "fbxkb is running, turning it off"
	pkill fbxkb 
 # make sure fbxkb is comented on the startup file
sed -i  "$line_number"' s/^/#/' "$file"
        
	else

# echo "fbxkb is not running, starting it"
	fbxkb &
 
#and also make sure fbxkb is not commented on  the startup file
 
	if [ "$first_character_in_line" == "#" ]; then #line is commented out already. Uncommententing it
		sed -i "$line_number"' s/^#//' $file
        fi
	
fi
}

please_restart()
{
	##restart IceWM
icewm -r
    ##Display warning
	yad --center --text=$"Please restart any open window so the GTK theme change can be applied to it \n This window is already displayed in the currently selected GTK theme. \n Certain apps (like Firefox, and Geany) have to be configured to use it's own Dark/Light Modes or Themes." --button=" x " --fixed --title=$"Global Theme change"  --window-icon=/usr/share/icons/papirus-antix/48x48/categories/icewmcc.png
}

dark_mode()
{
	####Dark Mode
file=$HOME/.config/gtk-3.0/settings.ini
text="gtk-theme-name="
sed -i "s/${text}.*/${text}Arc-Dark/" $file
sed -i "s/${text}.*/${text}Arc-Dark/" $HOME/.gtkrc-2.0

gconftool-2 --type=string --set /desktop/gnome/interface/gtk_theme "Arc-Dark"
#~/.config/gtk-3.0/settings.ini
##make edition to the config file
echo Theme=\"/usr/share/icewm/themes/FX-dark/default.theme\" > $HOME/.icewm/theme
please_restart
}

light_mode()
{
	####Light Mode
file=$HOME/.config/gtk-3.0/settings.ini
text="gtk-theme-name="
sed -i "s/${text}.*/${text}Arc-EvoPro2/" $file
sed -i "s/${text}.*/${text}Arc-EvoPro2/" $HOME/.gtkrc-2.0
gconftool-2 --type=string --set /desktop/gnome/interface/gtk_theme "Arc-EvoPro2"
#~/.config/gtk-3.0/settings.ini
##make edition to the config file
echo Theme=\"/usr/share/icewm/themes/FX-light/default.theme\" > $HOME/.icewm/theme
please_restart
}

default_mode()
{
	####default Mode
file=$HOME/.config/gtk-3.0/settings.ini
text="gtk-theme-name="
sed -i "s/${text}.*/${text}Arc-EvoPro2/" $file
sed -i "s/${text}.*/${text}Arc-EvoPro2/" $HOME/.gtkrc-2.0
gconftool-2 --type=string --set /desktop/gnome/interface/gtk_theme "Arc-EvoPro2"
#~/.config/gtk-3.0/settings.ini
##make edition to the config file
echo Theme=\"/usr/share/icewm/themes/FX-dark/default.theme\" > $HOME/.icewm/theme
please_restart
}


FILE_TEMPLATE_A


######MAIN SCRIPT:
ICONS=/usr/share/icons/papirus-antix/48x48/categories/
ICONS2=/usr/share/pixmaps


its_alive() {
    # return 0
    local root_fstype=$(df -PT / | tail -n1 | awk '{print $2}')
    case $root_fstype in
        aufs|overlay) return 0 ;;
                   *) return 1 ;;
    esac
}

its_alive && ITS_ALIVE=true

##Draw main window:
vbox() {
    local text="$*"
    local len=${#text}

    if [ $len -lt 20 ]; then
        echo '<vbox><hseparator></hseparator></vbox>'
    else
    echo "  <vbox>"
    local item
    for item; do
        echo "$item"
    done
    echo "  </vbox>"
    fi
}

hbox() {
    local text="$*"
    local len=${#text}

    [ $len -lt 30 ] && return
    echo "<hbox>"
    local item
    for item; do
        echo "$item"
    done
    echo "</hbox>"
}

vbox_frame_hbox() {
    local text="$*"
    local len=${#text}

    [ $len -lt 20 ] && return
    echo "<vbox><frame><hbox>"
    local item
    for item; do
        echo "$item"
    done
    echo "</hbox></frame></vbox>"
}

entry() {
    local image="$1" action="$2" text="$3" tooltip="$4"
    [ -n "$tooltip" ] || tooltip=$"(No tooltip for now, sorry.)"
    cat<<Entry
    <hbox>
      <button tooltip-text="$tooltip">
        <input file>$image</input>
        <height>48></height>
        <action>$action</action>
      </button>
      <text use-markup="true" width-chars="45">
        <label>$text</label>
      </text>
    </hbox>
Entry
}

######(Start of IceWM functions)


#Script variables
file=$HOME/.icewm/prefoverride
#Make sure the prefoverride file exists:
touch $file

###Check if the current desktop is IceWM, if not, exit
desktop=$(wmctrl -m)
if [[ $desktop == *"icewm"* ]]; then
  result="You are running an IceWM desktop"
    else 
   yad --title="$warning" --text="$icewm_only_text" --timeout=10 --no-buttons --center
 exit
fi

###### (end of IceWM functions)

##### Create IceWM CC buttons: icon; command/function ; text; tooltip

#TOOLBAR Tab options:
toolbar_auto_hide_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/stock_bottom.png" \
    "bash -c 'source /tmp/sourcefunctions && autohide_toolbar'" \
    "$auto_hide" \
    "$auto_hide_tooltip")
    
double_hight_toolbar_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/layer-duplicate.png" \
    "bash -c 'source /tmp/sourcefunctions && doubleheight_toolbar'" \
    "$double_hight" \
    "$doubleheight_toolbar_tooltip")

top_bottom_toolbar_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/applications-interfacedesign.png" \
    "bash -c 'source /tmp/sourcefunctions && top_toolbar'" \
    "$top_bottom" \
    "$top_bottom_tooltip")

toolbar_width_and_horizontal_position_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/fitwidth.png" \
    "bash -c 'source /tmp/sourcefunctions && toolbar_width'" \
    "$toolbar_width_and_horizontal_position_window_title" \
    "$toolbar_width_and_horizontal_position_window_title_tooltip")
    
names_on_toolbar_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/cs-windows.png" \
    "bash -c 'source /tmp/sourcefunctions && TaskbarButtonWidthDivisor'" \
    "$names_on_toolbar" \
    "$names_on_toolbar_tooltip")

tim_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/icewm_editor.png" \
    "bash -c 'icewm-toolbar-icon-manager.sh || icewm-toolbar-icon-manager'" \
    "$tim" \
    "$tim_tooltip")
    
show_desktop_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/dashboard-show.png" \
    "bash -c 'source /tmp/sourcefunctions && TaskBarShowShowDesktopButton'" \
    "$show_desktop" \
    "$show_desktop_tooltip")
    
workspace_toggle_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/view-dual.png" \
    "bash -c 'source /tmp/sourcefunctions && TaskBarShowWorkspaces'" \
    "$workspace_toggle" \
    "$workspace_toggle_tooltip")

workspace_manager_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/cs-workspaces.png" \
    "bash -c 'antiX-workspace-manager'" \
    "$workspace_manager" \
    "$workspace_manager_tooltip")

collapse_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/kt-start.png" \
    "bash -c 'source /tmp/sourcefunctions && TaskBarShowCollapseButton'" \
    "$collapse" \
    "$collapse_tooltip")

weather_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/org.gnome.Weather.png" \
    "bash -c 'source /tmp/sourcefunctions &&  wttr'" \
    "$weather" \
    "$weather_tooltip")

#System tray Tab options:
keyboard_layout_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-desktop-locale.png" \
    "bash -c 'source /tmp/sourcefunctions && keyboard_indicator'" \
    "$keyboard_layout_button" \
    "$keyboard_layout_tooltip")		
    
sys_net_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-system-network.png" \
    "bash -c 'source /tmp/sourcefunctions && toogle_Network_info_square_on_system_tray'" \
    "$sys_net" \
    "$sys_net_tooltip")	

show_only_used_network_devices_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/transform-move-horizontal.png" \
    "bash -c 'source /tmp/sourcefunctions && NetStatusShowOnlyRunning'" \
    "$show_only_used_network_devices" \
    "$show_only_used_network_devices_tooltip")	
		
toogle_RAM_info_square_on_system_tray_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/devices/media-memory.png" \
    "bash -c 'source /tmp/sourcefunctions && toogle_RAM_info_square_on_system_tray'" \
    "$sys_ram" \
    "$sys_ram_tooltip")	
		
toogle_CPU_info_square_on_system_tray_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/devices/cpu.png" \
    "bash -c 'source /tmp/sourcefunctions && toogle_system_info_square_on_system_tray'" \
    "$sys_cpu" \
    "$sys_cpu_tooltip")	
    
# Clock settings Tab options:
clock_on_off_entry=$(entry \
    "/usr/share/icons/Adwaita/48x48/status/appointment-missed-symbolic.symbolic.png" \
    "bash -c 'source /tmp/sourcefunctions && toggle_clock'" \
    "$clock_on_off" \
    "$clock_on_off_tooltip")	        
		
clock_12_24h_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/stock_new-24h-appointment.png" \
    "bash -c 'source /tmp/sourcefunctions && toggle12h24h'" \
    "$clock_12_24h" \
    "$clock_12_24h_tooltip")
    		
clock_seconds_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/sysprof.png" \
    "bash -c 'source /tmp/sourcefunctions && toggle_seconds'" \
    "$clock_seconds" \
    "$clock_seconds_tooltip")
    		
clock_reset_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/object-rotate-left.png" \
    "bash -c 'sed -i '/^TimeFormat=/d' ~/.icewm/prefoverride &&  icewm -r'" \
    "$reset_text" \
    "$reset_text_tooltip")		
		
clock_LED_entry=$(entry \
    "/usr/share/icons/papirus-antix/24x24/panel/indicator-workspaces-8.png" \
    "bash -c 'source /tmp/sourcefunctions && toggle_clock_LED'" \
    "$clock_LED" \
    "$clock_LED_tooltip")
    		   
change_time_date_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/clock.png" \
    "bash -c 'set_time-and_date.sh'" \
    "$change_time_date" \
    "$change_time_date_tooltip")
    
display_time_date_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/org.kde.contrib.simpledateandtime.png" \
    "bash -c 'source /tmp/sourcefunctions && display_time_date'" \
    "$display_time_date_label" \
    "$display_time_date_tooltip")    
    
# Window management Tab options:
title_icon_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/insert-image.png" \
    "bash -c 'source /tmp/sourcefunctions && icon_on_title_bar'" \
    "$title_icon" \
    "$title_icon_tooltip")
	
center_win_entry=$(entry \
    "/usr/share/icons/Adwaita/48x48/actions/zoom-fit-best-symbolic.symbolic.png" \
    "bash -c 'source /tmp/sourcefunctions && center_large_windows'" \
    "$center_win" \
    "$center_win_tooltip")	
    
moving_osd_entry=$(entry \
    "/usr/share/icons/Adwaita/48x48/actions/media-view-subtitles-symbolic.symbolic.png" \
    "bash -c 'source /tmp/sourcefunctions && ShowMoveSizeStatus'" \
    "$moving_osd" \
    "$moving_osd_tooltip")		
	
remember_win_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-system-windows-move.png" \
    "bash -c 'icewm-remember-settings'" \
    "$remember_win" \
    "$remember_win_tooltip")	

# Menu settings Tab options:
personal_menu_manager_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/icewm_peditor.png" \
    "bash -c 'icewm-quick-personal-menu-manager.sh || icewm-quick-personal-menu-manager'" \
    "$personal_menu_manager" \
    "$personal_menu_manager_tooltip")	
    
personal_menu_toggle_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/emblems/emblem-rabbitvcs-unversioned.png" \
    "bash -c 'source /tmp/sourcefunctions && show_personal_submenu'" \
    "$personal_menu_toggle" \
    "$personal_menu_toggle_tooltip")		

personal_entries_on_the_first_layer_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/emblems/emblem-favorite.png" \
    "bash -c 'source /tmp/sourcefunctions && show_personal_menu_entries_on_first_layer_of_menu'" \
    "$personal_entries_on_the_first_layer" \
    "$personal_entries_on_the_first_layer_tooltip")		

show_xkill_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/emblems/emblem-rabbitvcs-conflicted.png" \
    "bash -c 'source /tmp/sourcefunctions && show_xkill'" \
    "$show_xkill" \
    "$show_xkill_tooltip")		

run_menu_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/debug-run-cursor.png" \
    "bash -c 'source /tmp/sourcefunctions && Show_run_menu_entry'" \
    "$run_menu_entry" \
    "$run_menu_entry_tooltip")	

show_icewm_preferences_menu_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-desktop-tweaks.png" \
    "bash -c 'source /tmp/sourcefunctions && Show_preferences_submenu'" \
    "$show_icewm_preferences_menu" \
    "$show_icewm_preferences_menu_tooltip")	
    
applications_menu_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/applications.png" \
    "bash -c 'source /tmp/sourcefunctions && applications_menu_toggle'" \
    "$applications_menu_label" \
    "$applications_menu_tooltip")	

edit_menu_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/xfce4-panel-menu.png" \
    "bash -c 'sudo menu_manager.sh'" \
    "$edit_menu_label" \
    "$edit_menu_tooltip")	    
         
# Look and Feel Tab options    
wallpaper_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-desktop-wallpaper.png" \
    "bash -c 'wallpaper'" \
    "$wallpaper" \
    "$wallpaper_tooltip")	
    	
appearance_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-desktop-theme.png" \
    "bash -c  'lxappearance'"\
    "$appearance" \
    "$appearance_tooltip")		
	
select_theme_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/categories/cs-cat-appearance.png" \
    "bash -c 'source /tmp/sourcefunctions && Select_Theme'" \
    "$select_theme" \
    "$select_theme_tooltip")	
	
compositor_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/desktop-effects.png" \
    "bash -c  'simple_compositor_toggle'" \
    "$compositor" \
    "$compositor_tooltip")
    	
conky_toogle_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/conky-on-off.png" \
    "bash -c  conkytoggle.sh" \
    "$conky" \
    "$conky_tooltip")
    	
keyboard_shortcuts_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/devices/keyboard.png" \
    "bash -c  add-key" \
    "$keyboard_shortcuts" \
    "$keyboard_shortcuts_tooltip")	
	
add_to_desktop_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/preferences-desktop-icons.png" \
    "bash -c 'source /tmp/sourcefunctions && send_to_desktop'" \
    "$add_to_desktop" \
    "$add_to_desktop_tooltip")	
    
# Auto Start Tab options:
add_remove_main_button_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/actions/focus-legacy-systray.png" \
    "bash -c 'source /tmp/sourcefunctions && ice_startup'" \
    "$add_remove_main_button" \
    "$add_remove_main_button_tooltip")	

# Recent files tab options
manage_recent_files_list_entry=$(entry \
    "/usr/share/pixmaps/papirus/file_32x32.png" \
    "bash -c  /usr/local/bin/yad-recently-used-settings.sh" \
    "$manage_recent_files_list" \
    "$manage_recent_files_list_tooltip")	
    
clear_recent_files_list_entry=$(entry \
    "/usr/share/icons/papirus-antix/48x48/apps/trashindicator.png" \
    "bash -c  /usr/local/bin/yad-recently-used-delete.sh" \
    "$clear_recent_files_list" \
    "$clear_recent_files_list_tooltip")	

###Tabs to be shown on the leff side of the MAIN WINDOW:
    tab_labels="$toolbar_field|$system_tray_field|$clock_field|$window_managment_field|$menu_field|$look_feel|$startup_field|$recent_files"
    
# Dirty workaround to make gettext translation work within the heredoc:
###Title of the main window:
windowheader=$"Basic IceWM Settings Manager" 

###Contents of all categories: "tab-pos" sets the tab position (0 is on the left side); window-position="1" means "centered window"; 
#Each "$(vbox_frame_hbox" is one Tab and it's entry- if there are more tabs than indicated on the field with tab names, their name will simply be their number, continuing fom the previous entry with a name
#"/usr/share/icons/papirus-antix/48x48/categories/icewmcc.png"
export IceWMControlCenter=$(cat<<Control_Center 
<window title="$windowheader"  window-position="1" icon-name="icewmcc">
  <vbox>
  <text label="$main_window_text"></text>
<notebook tab-pos="0" labels="$tab_labels">
$(vbox_frame_hbox \
"$(vbox "$toolbar_auto_hide_entry" "$double_hight_toolbar_entry" "$top_bottom_toolbar_entry" "$toolbar_width_and_horizontal_position_entry" "$names_on_toolbar_entry" "$tim_entry")" \
"$(vbox "$show_desktop_entry" "$workspace_toggle_entry" "$workspace_manager_entry" "$collapse_entry" "$weather_entry")" )

$(vbox_frame_hbox \
"$(vbox "$keyboard_layout_entry" "$sys_net_entry" "$show_only_used_network_devices_entry" )" \
"$(vbox "$toogle_RAM_info_square_on_system_tray_entry" "$toogle_CPU_info_square_on_system_tray_entry")")

$(vbox_frame_hbox \
"$(vbox "$clock_on_off_entry" "$clock_12_24h_entry"  "$clock_seconds_entry" "$clock_reset_entry" )" \
"$(vbox "$clock_LED_entry" "$display_time_date_entry" "$change_time_date_entry")" )

$(vbox_frame_hbox \
"$(vbox "$title_icon_entry" "$center_win_entry" "$moving_osd_entry" )" \
"$(vbox "$remember_win_entry")" )

$(vbox_frame_hbox \
"$(vbox "$personal_menu_manager_entry" "$personal_menu_toggle_entry" "$personal_entries_on_the_first_layer_entry" "$applications_menu_entry" "$edit_menu_entry" "$show_xkill_entry")" "$(vbox "$run_menu_entry" "$show_icewm_preferences_menu_entry")" )

$(vbox_frame_hbox \
"$(vbox "$wallpaper_entry" "$appearance_entry" "$select_theme_entry" "$compositor_entry")" \
"$(vbox "$conky_toogle_entry" "$keyboard_shortcuts_entry" "$add_to_desktop_entry")")

$(vbox_frame_hbox \
"$(vbox "$add_remove_main_button_entry")")

$(vbox_frame_hbox \
"$(vbox "$manage_recent_files_list_entry")" \
"$(vbox "$clear_recent_files_list_entry")")
</notebook>
            <hbox> 
		<button>
		 <label> "$manualedit" </label>
		 <input file icon="gtk-edit"></input>
		 <action>"bash -c  'desktop-defaults-run -te $HOME/.icewm/winoptions $HOME/.icewm/preferences $HOME/.icewm/prefoverride $HOME/.icewm/keys $HOME/.icewm/startup $HOME/.icewm/toolbar $HOME/.icewm/menu $HOME/.icewm/personal $HOME/.icewm/menu-applications $HOME/.icewm/personal'"</action>

		</button> 
	</hbox> 
</vbox>

</window>
Control_Center
)

gtkdialog --program="IceWMControlCenter" 
