#!/bin/sh
#250822 sfs
# Инициализация gettext
export TEXTDOMAIN="ptheme-labwc"
export TEXTDOMAINDIR="/usr/share/locale"

# Функции для перевода
gettext() {
    /usr/bin/gettext "$1"
}

eval_gettext() {
    /usr/bin/gettext -e "$1"
}

while :; do
    THEMES=
    for THEME in /usr/local/share/themes/* /usr/share/themes/*; do
        [ -f ${THEME}/openbox-3/themerc ] && THEMES="$THEMES ${THEME##*/}"
    done

    PLACEMENTS="center!automatic!cursor!cascade"

    # Получаем текущие значения из конфига
    CURRENT_THEME=$(grep -oP '<theme><name>\K[^<]+' ~/.config/labwc/rc.xml 2>/dev/null || echo "")
    CURRENT_PLACEMENT=$(grep -oP '<policy>\K[^<]+' ~/.config/labwc/rc.xml 2>/dev/null || echo "center")
    
    labwc_icon="gtk-preferences"
    #labwc_icon="labwc"
    
    # Переведенные строки
    t=$(gettext "LabWC Configuration")
    theme_label=$(gettext "Appearance theme")
    policy_label=$(gettext "Windows placement")
    
    p=$(eval_gettext 'Specify a placement policy for new windows.
The "center" policy will always place windows at the center of the active output.
The "automatic" policy will try to place new windows in such a way that they will have minimal overlap with existing windows.
The "cursor" policy will center new windows under the cursor.
The "cascade" policy will try to place new windows at the center of the active output, but possibly shifts its position to bottom-right not to cover existing windows.
Default is "cascade".')

    CHOICE=$(yad --fixed --name="$labwc_icon" --title="$t" --text="$t" --window-icon="$labwc_icon" \
         --image-on-top --image="labwc" --Xheight=300 --form \
        --field="$theme_label:CB" "$CURRENT_THEME!$(echo $THEMES | tr ' ' '!' | sed 's/!$//')" \
        --field="$policy_label!$p:CB" "$CURRENT_PLACEMENT!$PLACEMENTS")
    
    [ -z "$CHOICE" ] && break
    
    THEME=$(echo "$CHOICE" | cut -d'|' -f1)
    PLACEMENT=$(echo "$CHOICE" | cut -d'|' -f2)
    echo "$THEME $PLACEMENT"
    
    # Обновляем конфигурацию
    sed -i "s~<theme><name>.*</name>~<theme><name>${THEME}</name>~" ~/.config/labwc/rc.xml
    sed -i "s~<policy>.*</policy>~<policy>${PLACEMENT}</policy>~" ~/.config/labwc/rc.xml
    
    labwc -r
done
