#!/bin/bash
#
# conkyctl - script to handle various Conky operations in Mabox

CONKYDIR="$HOME"/.config/conky
BASECONKY="$CONKYDIR/sysinfo_mbcolor.conkyrc"
SESSIONFILE="$CONKYDIR/conky-sessionfile"
COLORIZERDIR="$HOME/.config/colorizer"
CONKYTHEMEDIR="$COLORIZERDIR/conky/themes"

if [ ! -d "$CONKYTHEMEDIR" ];then
mkdir -p "$CONKYTHEMEDIR"
rsync -a /usr/share/mabox-colorizer/themes/conky/* ${CONKYTHEMEDIR}/
fi

update_session () {
    sleep 1
    :> "${SESSIONFILE}"
    while read -r pid b c confpath 
    do
    echo "$b $c ${confpath} & sleep .5" >> ${SESSIONFILE}
    done < <(pgrep -u $USER -af "conky -c ")
    # Remove duplicates
    awk -i inplace '!seen[$0]++' ${SESSIONFILE}
}

transparent_all () {
    sd "own_window_transparent.*$" "own_window_transparent = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
    fix_transparent_conkies
}
transparent () {
    sd "own_window_transparent.*$" "own_window_transparent = ${1}," ${2}
}
win_bgcolor_all () {
    sd "own_window_colour .*=.*$" "own_window_colour = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
    sd "own_window_transparent.*$" "own_window_transparent = false," ${CONKYDIR}/*mbcolor.conkyrc
    sd "own_window_transparent.*$" "own_window_transparent = true," ${CONKYDIR}/logo_mbcolor.conkyrc
    fix_transparent_conkies
}
win_bgcolor () {
    sd "own_window_colour .*=.*$" "own_window_colour = '${1}'," ${2}
    sd "own_window_transparent.*$" "own_window_transparent = false," ${2}
    #notify-send.sh "${2}" aaa

}
draw_borders_all () {
    sd "draw_borders.*$" "draw_borders = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
    sd "draw_borders.*$" "draw_borders = false," ${CONKYDIR}/logo_mbcolor.conkyrc
}
draw_borders () {
    sd "draw_borders.*$" "draw_borders = ${1}," ${2}
}
border_width () {
	sd "border_width.*$" "border_width = ${1}," ${2}
	#calculate border_inner_margin = 15,	border_outer_margin = 1,
	#outer width /2
	#inner 16 - outer
	if [[ "${1}" = "1" ]];then
	omargin=7
	imargin=8
	else
	omargin=$((${1} / 2 ))
	imargin=$((10 - $omargin))
	fi
	#notify-send.sh "Margins" "outer = ${omargin}, inner = ${imargin}"
	sd "border_outer_margin.*$" "border_outer_margin = ${omargin}," ${2}
	sd "border_inner_margin.*$" "border_inner_margin = ${imargin}," ${2}
}
border_width_all () {
	sd "border_width.*$" "border_width = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
	if [[ "${1}" = "1" ]];then
	omargin=7
	imargin=8
	else
	omargin=$((${1} / 2 ))
	imargin=$((10 - $omargin))
	fi
	sd "border_outer_margin.*$" "border_outer_margin = ${omargin}," ${CONKYDIR}/*mbcolor.conkyrc
	sd "border_inner_margin.*$" "border_inner_margin = ${imargin}," ${CONKYDIR}/*mbcolor.conkyrc
    draw_borders_all true
}
stippled_borders_all () {
    sd "stippled_borders.*$" "stippled_borders = ${1}," ${CONKYDIR}/*mbcolor.conkyrc
    sd "draw_borders.*$" "draw_borders = true," ${CONKYDIR}/*mbcolor.conkyrc
    sd "draw_borders.*$" "draw_borders = false," ${CONKYDIR}/logo_mbcolor.conkyrc
}
stippled_borders () {
    sd "stippled_borders.*$" "stippled_borders = ${1}," ${2}
    sd "draw_borders.*$" "draw_borders = true," ${2}
}
default_color_all () {
    sd "default_color .*=.*$" "default_color = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color0_all () {
    sd "color0 .*=.*$" "color0 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color1_all () {
    sd "color1 .*=.*$" "color1 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color2_all () {
    sd "color2 .*=.*$" "color2 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
color3_all () {
    sd "color3 .*=.*$" "color3 = '${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
default_color () {
    sd "default_color .*=.*$" "default_color = '${1}'," ${2}
}
color0 () {
    sd "color0 .*=.*$" "color0 = '${1}'," ${2}
}
color1 () {
    sd "color1 .*=.*$" "color1 = '${1}'," ${2}
}
color2 () {
    sd "color2 .*=.*$" "color2 = '${1}'," ${2}
}
color3 () {
    sd "color3 .*=.*$" "color3 = '${1}'," ${2}
}
color8 () {
    sd "color8 .*=.*$" "color8 = '${1}'," ${2}
}
color9 () {
    sd "color9 .*=.*$" "color9 = '${1}'," ${2}
}

basefont_inc_all () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE++))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
basefont_dec_all () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE--))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
basefont_inc () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE++))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${1}
}
basefont_dec () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    ((FONTSIZE--))
    sd "font .*=.*,$" "font = '${FONT}:size=${FONTSIZE}'," ${1}
}
basefont_family () {
	read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    sd "font .*=.*,$" "font = '${2}:size=${FONTSIZE}'," ${1}
}
basefont_family_all () {
	read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    sd "font .*=.*,$" "font = '${1}:size=${FONTSIZE}'," ${CONKYDIR}/*mbcolor.conkyrc
}
basefont_size () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    sd "font .*=.*,$" "font = '${FONT}:size=${2}'," ${1}
}
basefont_size_all () {
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    sd "font .*=.*,$" "font = '${FONT}:size=${1}'," ${CONKYDIR}/*mbcolor.conkyrc
}
shades_all () {
    case "$1" in
        shadow)
        sd "draw_shades .*=.*,$" "draw_shades = true," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = false," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
        outline)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = true," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
        *)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${CONKYDIR}/*mbcolor.conkyrc
        sd "draw_outline .*=.*,$" "draw_outline = false," ${CONKYDIR}/*mbcolor.conkyrc
        ;;
    esac
}
shades () {
    case "$1" in
        shadow)
        sd "draw_shades .*=.*,$" "draw_shades = true," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = false," ${2}
        ;;
        outline)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = true," ${2}
        ;;
        *)
        sd "draw_shades .*=.*,$" "draw_shades = false," ${2}
        sd "draw_outline .*=.*,$" "draw_outline = false," ${2}
        ;;
    esac
}
logosize () {
    sd "minimum_height.*$" "minimum_height = ${1}," ${CONKYDIR}/logo_mbcolor.conkyrc
    sd "minimum_width.*$" "minimum_width = ${1}," ${CONKYDIR}/logo_mbcolor.conkyrc
    sd "\{image ~.*$" "{image ~/.config/conky/images/mabox_3d.png -s ${1}x${1}}" ${CONKYDIR}/logo_mbcolor.conkyrc
}

pos () {
    def_x=${conky_gap_x:-30}
    def_y=${conky_gap_y:-60}
    
    sd "alignment .*=.*,$" "alignment = '${1}'," ${2}
    ### jak po środku to wyzerować gaps
    case "$1" in
        top_left|top_right|bottom_left|bottom_right)
        sd "gap_x .*=.*," "gap_x = ${def_x}," ${2}
        sd "gap_y .*=.*," "gap_y = ${def_y}," ${2}
        ;;
        top_middle|bottom_middle)
        sd "gap_x .*=.*," "gap_x = 0," ${2}
        sd "gap_y .*=.*," "gap_y = ${def_y}," ${2}
        ;;
        middle_left|middle_right)
        sd "gap_x .*=.*," "gap_x = ${def_x}," ${2}
        sd "gap_y .*=.*," "gap_y = 0," ${2}
        ;;
        middle_middle)
        sd "gap_x .*=.*," "gap_x = 0," ${2}
        sd "gap_y .*=.*," "gap_y = 0," ${2}
        ;;
    esac
}
gap_x () {
    sd "gap_x .*=.*," "gap_x = ${1}," ${2}
}
gap_y () {
    sd "gap_y .*=.*," "gap_y = ${1}," ${2}
}
setpos () {
    filename=${1##*/}
    name=${filename%%_mb*}
    title=${name//_/ }
    case "$LANG" in
    pl*)
    SUMM="Nowa pozycja dla Conky: ${title^}"
    BODY="\nNarysuj mały prostokąt na ekranie aby wyznaczyć nową pozycję...\n <i>(ESC lub prawy klik anuluje</i>"
    ABORTED_SUMM="Anulowano!"
    ABORTED_BODY="\n...operacja anulowana"
    ;;
    es*)
    SUMM="Mark new position for ${title^} Conky"
    BODY="\nDraw small rectangle to mark new position...\n <i>(ESC or right click to abort)</i>"
    ABORTED_SUMM="Aborted!"
    ABORTED_BODY="\n... operation aborted!"
    ;;
    *)
    SUMM="Mark new position for ${title^} Conky"
    BODY="\nDraw small rectangle to mark new position...\n <i>(ESC or right click to abort)</i>"
    ABORTED_SUMM="Aborted!"
    ABORTED_BODY="\n... operation aborted!"
        ;;
    esac
    
    wmctrl -k on
    notify-send.sh -i mbcc --replace-file=/tmp/conkyctlfile "${SUMM}" "${BODY}"
    read -r X Y < <(slop -t 0 -l -b 3 --color=0.3,0.4,0.6,0.4 -f "%x %y")
    if [ -z "$X" ]
then
    notify-send.sh -i mbcc --replace-file=/tmp/conkyctlfile "${ABORTED_SUMM}" "${ABORTED_BODY}"
else
    pos top_left ${1}
    # improve conky margin calculation here: gap = border_width + border_inner_margin + border_outer_margin
    ((X=X+17))
    ((Y=Y+17))
    gap_x ${X} ${1}
    gap_y ${Y} ${1}
    case "$LANG" in
        pl*)
        THNX_SUMM="Dziękuję!"
        THNX_BODY="Zapisano nową pozycję ${title^} \n\tX: <b>$X</b>\n\tY: <b>$Y</b>"
        ;;
        es*)
        THNX_SUMM="Thank you!"
        THNX_BODY="New position of ${title^} saved! \n\tX: <b>$X</b>\n\tY: <b>$Y</b>"
        ;;
        *)
        THNX_SUMM="Thank you!"
        THNX_BODY="New position of ${title^} saved! \n\tX: <b>$X</b>\n\tY: <b>$Y</b>"
        ;;
    esac
    
    notify-send.sh  -i mbcc --replace-file=/tmp/conkyctlfile "${THNX_SUMM}" "${THNX_BODY}"
    #wmctrl -k off
fi
}

startone () {
    conky -c "${1}" &
    update_session
}
startall () {
    for i in "$CONKYDIR"/*_mbcolor*; do
    if pgrep -u $USER -af "conky .*${i}" >/dev/null; then
    :
    else
    conky -c "${i}" &
    fi
    done
    update_session
}
kill () {
    pkill -f "conky .*${1}"
    update_session
}
restartone () {
    kill ${1}
    startone ${1} 
}
reload() {
kill ${1}
conky -c ${1}
}
toggleone () {
	if pgrep -u $USER -af "conky .*${1}" >/dev/null; then
    kill "${1}"
    else
    CCFG=$(ls ~/.config/conky/${1}*.conkyrc)
    #notify-send.sh "Conky" "$CCFG"
		if test -f "$CCFG";
		then
			startone "$CCFG"
		fi
    #conky -c "${i}" &
    fi
}
makemoveableall () {
    wmctrl -k on
    sd "own_window_type.*$" "own_window_type = 'normal'," ${CONKYDIR}/*mbcolor.conkyrc
    case "$LANG" in
        pl*)
        TITLE="Przenieś Conky dokąd chcesz"
        DESC="Możesz teraz przesuwać wszystkie Conky.\nPrzytrzymaj wciśnięty klawisz <b>Alt</b> i przesuń każde Conky gdzie chcesz\n "
        VB="<span foreground='red'>To prawdopodobnie nie zadziała w środowiskach wirtualnych</span>"
        CANCEL="Anuluj"
        SAVE="Zapisz nowe pozycje"
        ;;
        es*)
        TITLE="Move Conkies where you want"
        DESC="Move Conkies to desired locations now...\n Hold <b>Alt</b> and drag Conkies one by one.\n"
        VB="<span foreground='red'>It will probably not work in Virtual environments</span>"
        CANCEL="Cancel"
        SAVE="Save new positions"
        ;;
        *)
        TITLE="Move Conkies where you want"
        DESC="Move Conkies to desired locations now...\n Hold <b>Alt</b> and drag Conkies one by one.\n"
        VB="<span foreground='red'>It will probably not work in Virtual environments</span>"
        CANCEL="Cancel"
        SAVE="Save new positions"
        ;;
    esac
    TYPE=$(cat /sys/class/dmi/id/chassis_type)
    if [ "$TYPE" != "1" ];then
    VB=""
    fi
    cmd=(
    yad --center --borders=20
    --title="$TITLE"
    --text="$DESC\n\n${VB}"
    --button="$CANCEL":1
    --button="$SAVE":0
    )
    "${cmd[@]}"
    exval=$?
    #notify-send.sh -t 10000  "Exval" "${exval}"
    case $exval in
        1) makedesktoptypeall;;
        0) saveposall;;
        252) makedesktoptypeall;;
    esac
    
}
saveposall () {
    # save new positions here
    while read -r WIN_ID b c X Y rest 
    do
    conffile=$(xprop -id $WIN_ID |grep WM_COMMAND |cut -d'"' -f6)
    if [[ "$conffile" = *"mbcolor"* ]];then
        pos top_left ${conffile}
        #notify-send.sh -t 10000 "${X} ${Y}" "${conffile}"
        # improve conky margin calculation here: gap = border_width + border_inner_margin + border_outer_margin
        ((X=X+17))
        ((Y=Y+17))
        gap_x ${X} ${conffile}
        gap_y ${Y} ${conffile}
    fi
    done < <(wmctrl -lpG | grep '1 0 ')
    makedesktoptypeall
}
makedesktoptypeall () {
    sd "own_window_type.*$" "own_window_type = 'desktop'," ${CONKYDIR}/*mbcolor.conkyrc
}
resetcolorsall () {
    transparent_all false
    win_bgcolor_all '#142c2c'
    draw_borders_all true
    stippled_borders_all 0
    default_color_all '#d3d4bf'
    color0_all '#accdc7'
    color1_all '#309552'
    color2_all '#E8CB2D'
    color3_all '#2bb0a5'
    fix_transparent_conkies
}
resetcolorsone () {
    transparent false "${1}"
    win_bgcolor '#142c2c' "${1}"
    default_color '#d3d4bf' "${1}"
    color0 '#accdc7' "${1}"
    color1 '#309552' "${1}"
    color2 '#E8CB2D' "${1}"
    color3 '#2bb0a5' "${1}"
}

languages () {
     case "$LANG" in
    pl*)
    UNSUPPORTED="częściowo wspierane!!!"
    OLD_SYNTAX_DETECTED="<b><span bgcolor='yellow' fgcolor='black'> stara składnia! </span></b> wykryta..."
    CONVERT_OLD="Czy spróbować skonwertować?"
    CONVERT_OLD_TRY="Tak, spróbuj konwertować"
    POSITION="Pozycja"
    MAKEMOVEABLE="Ustaw wszystkie (<i>poprzez przeciąganie</i>)..."
    MARKNEWPOS="Nowa pozycja (<i>zaznacz na ekranie</i>)..."
    ALIGNMENT="Wyrównanie"
    TOPLEFT="Góra po lewej"
    TOP="Góra"
    TOPRIGHT="Góra po prawej"
    LEFT="Lewa"
    CENTER="Po środku"
    RIGHT="Prawa"
    BOTTOMLEFT="Dół po lewej"
    BOTTOM="Dół"
    BOTTOMRIGHT="Dół po prawej"
    HORIZONTAL_GAP="Odstęp poziomy"
    VERTICAL_GAP="Odstęp pionowy"
    COLORS="Kolory"
    DEFCOLOR="Kolor obramowania"
    COLOR_0="Kolor 0 <small>(tekst)</small>"
    COLOR_1="Kolor 1 <small>(tekst alt)</small>"
    COLOR_2="Kolor 2 <small>(pasek/graf)</small>"
    COLOR_3="Kolor 3 <small>(większy tekst)</small>"
    BACKGROUND="Kolor tła"
    COLORSCHEME="Motywy kolorów"
    SYSTEM_SCHEMES="Systemowe"
    YOUR_SCHEMES="Moje motywy"
    OPENTHEMEDIR="Otwórz katalog motywów"
    APPLY_THEME="Zastosuj motyw"
    APPLY_TO_ONE="Zastosuj do "
    APPLY_TO_ALL="Zastosuj do wszystkich"
    NEWTHEME="Nowy motyw kolorów..."
    SAVENEWTHEME="utwórz nowy motyw..."
    SAVE_CURRSCHEME="Zapisz aktualną kolorystykę jako..."
    MONOCHROMATIC="Monochromatyczny"
    CHOOSE_AND_PICK="Wybierz wariant i pobierz kolor..."
    LIGHTBG="Jasne tło"
    DARKBG="Ciemne tło"
    RESETCOLORS="Przywróć domyślne kolory"
    GENFROMWP="Generuj z kolorów tapety"
    TRANSPARENT="Przezroczyste tło?"
    YES="Tak"
    NO="Nie"
    FONTS="Czcionka"
    SELECTFONT="Wybierz czcionkę..."
    FONTFAMILY_LBL="czcionka"
    _EDIT_FAVFONTS="Edytuj ulubione czcionki"
    FONTSIZE_LBL="rozmiar czcionki"
    INCREASE="Powiększ do"
    DECREASE="Pomniejsz do"
    OLINE="Obrys"
    SHADOW="Cień"
    DRAW="Rysuj"
    BORDERS="Obramowanie"
    EDIT="Edytuj plik Conky"
    RELOAD="Przeładuj"
    KILL="Wyłącz"
    LOGOSIZE="Rozmiar logo"
    SMALL="Małe"
    NORMAL="Normalne"
    BIG="Duże"
    _BORDER_WIDTH="Border width"
    _OLD_STYLE="(old style)"
    STIPPLED_BORDERS="Nakrapiane"
    SOLID_LINE="linia ciągła"
    NOT_WORKING="Nie działa? Naprawmy to :)"
    MOUNTPOINTS="Dyski (punkty montowania)"
    _ACTIONS="Zaawansowane"
    ;;
    *)
    UNSUPPORTED="partially supported!!!"
    OLD_SYNTAX_DETECTED="<b><span bgcolor='yellow' fgcolor='black'> old syntax!!! </span></b> detected..."
    CONVERT_OLD="Try to convert to new syntax?"
    CONVERT_OLD_TRY="Yes, try to convert"
    POSITION="Position"
    MAKEMOVEABLE="Rearrange widget positions"
    MARKNEWPOS="New position (<i>mark on the screen</i>)..."
    ALIGNMENT="Alignment"
    TOPLEFT="TopLeft"
    TOP="Top"
    TOPRIGHT="TopRight"
    LEFT="Left"
    CENTER="Center"
    RIGHT="Right"
    BOTTOMLEFT="BottomLeft"
    BOTTOM="Bottom"
    BOTTOMRIGHT="BottomRight"
    HORIZONTAL_GAP="Horizontal Gap"
    VERTICAL_GAP="Vertical Gap"
    COLORS="Colors"
    DEFCOLOR="Border color"
    COLOR_0="<small>(color0)</small> Text"
    COLOR_1="<small>(color1)</small> Text alt"
    COLOR_2="<small>(color2)</small> Bars/graphs"
    COLOR_3="<small>(color3)</small> <big>Bigger text</big>"
    BACKGROUND="Background color"
    COLORSCHEME="Color schemes"
    SYSTEM_SCHEMES="System colorschemes"
    YOUR_SCHEMES="My colorschemes"
    OPENTHEMEDIR="Open schemes dir"
    APPLY_THEME="Apply scheme..."
    APPLY_TO_ONE="Apply to "
    APPLY_TO_ALL="Apply to All"
    NEWTHEME="New Color scheme..."
    SAVENEWTHEME="from current colors..."
    SAVE_CURRSCHEME="Save current colors as..."
    MONOCHROMATIC="Monochromatic"
    CHOOSE_AND_PICK="Choose variant and pick color..."
    LIGHTBG="Light Background"
    DARKBG="Dark Background"
    RESETCOLORS="Restore default colors"
    GENFROMWP="Generate from wallpaper"
    TRANSPARENT="Transparent background?"
    YES="Yes"
    NO="No"
    FONTS="Font"
    SELECTFONT="Select Font..."
    FONTFAMILY_LBL="font family"
    _EDIT_FAVFONTS="Edit favorite fonts list"
    FONTSIZE_LBL="font size"
    INCREASE="Increase to"
    DECREASE="Decrease to"
    OLINE="Outline"
    SHADOW="Shadow"
    DRAW="Draw"
    BORDERS="Borders"
    EDIT="Edit Conky file"
    RELOAD="Reload"
    KILL="Quit"
    LOGOSIZE="Logo size"
    SMALL="Small"
    NORMAL="Normal"
    BIG="Big"
    _BORDER_WIDTH="Border width"
    _OLD_STYLE="(old style)"
    STIPPLED_BORDERS="Stippled borders"
    SOLID_LINE="solid line"
    NOT_WORKING="Not working? Try to fix"
    MOUNTPOINTS="Discs (mount points)"
    _ACTIONS="Advanced"
    ;;
    esac   
}

single () {
    . /usr/share/mb-jgtools/glyphicons.inc
    unset NOT_SUPPORTED OLD_SYNTAX
    
    SYSSCHEMES="/usr/share/mabox-colorizer/colorschemes/conky.csv"
	#USERSCHEMES="$HOME/.config/colorizer/conky/c_userschemes.csv"
	USERSCHEMES="$HOME/.config/colorizer/conky/colorschemes.csv"
	if [ ! -f "$USERSCHEMES" ]; then
		mkdir -p "$HOME/.config/colorizer/conky/"
		echo "WINCLR |  CLR  | CLR0  | CLR1  | CLR2  | CLR3  |WINTRANS|OUTLINE|SHADES | BORD |BWIDTH| STIP |scheme_name" > "$USERSCHEMES"
	fi
	
    ### Wallpaper colors
WALLPALDIR="$HOME/.cache/colorizer/palettes"
mkdir -p "$WALLPALDIR"
read WALLPATH<<< $(grep file "$HOME"/.config/nitrogen/bg-saved.cfg | tail -1 | cut -d'=' -f2)
NAME=${WALLPATH////_}
if [[ "${NAME}" =~ ^_home_.* ]]; then
n=${#HOME}
((n++))
NAME=${NAME:${n}}
fi
if [ ! -f "$WALLPALDIR/$NAME.clr" ]; then
	if [ "$ext" == "avif" ];then
	magick ${WALLPATH} -resize 25% /tmp/out.jpg
	magick /tmp/out.jpg  -colors 12 -unique-colors txt:- |grep -v '^#'| awk '{print substr($3,1,7)}' |pastel sort-by brightness |pastel format hex > "$WALLPALDIR/$NAME.clr"
	else
	magick ${WALLPATH} -resize 25% -colors 12 -unique-colors txt:- |grep -v '^#'| awk '{print substr($3,1,7)}' |pastel sort-by brightness |pastel format hex > "$WALLPALDIR/$NAME.clr"
	fi
fi
mapfile -t w < "$WALLPALDIR/$NAME.clr"
    
    
    languages
    filename=${1##*/}
    name=${filename%%_mb*}
    title=${name//_/ }
    
    grep -q "conky.text" "$1" && : || OLD_SYNTAX="true"
    
    # breaks mbclr color picking from contextmenu, leave it as "colorizer-conky -s" for now
    if [[ "$2" == "s" ]];then
    me="none none"
    else
    me="colorizer-conky -s"
    fi
    read POS <<< "$(grep alignment ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    read GAPX <<< "$(grep gap_x ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read GAPY <<< "$(grep gap_y ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    
    read WINCLASS <<< "$(grep own_window_class ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    #notify-send.sh "Class" "${WINCLASS}"
    
    read WINTRANS <<< "$(grep own_window_transparent ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read FONTDEF <<< "$(grep "font .*=.*,$" ${1} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    read WINCLR <<< "$(grep own_window_colour ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $WINCLR =~ ^#.* ]] && : || WINCLR="#${WINCLR}"
    
    read CLR <<< "$(grep default_color ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR =~ ^#.* ]] && : || CLR="#${CLR}"
    read CLR0 <<< "$(grep color0 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR0 =~ ^#.* ]] && : || CLR0="#${CLR0}"
    read CLR1 <<< "$(grep color1 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR1 =~ ^#.* ]] && : || CLR1="#${CLR1}"
    read CLR2 <<< "$(grep color2 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR2 =~ ^#.* ]] && : || CLR2="#${CLR2}"
    read CLR3 <<< "$(grep color3 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR3 =~ ^#.* ]] && : || CLR3="#${CLR3}"
    read OUTLINE <<< "$(grep draw_outline ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read SHADES <<< "$(grep draw_shades ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    SHADOWS="no"
    [[ "$OUTLINE" == "true" ]] && SHADOWS="outline"
    [[ "$SHADES" == "true" ]] && SHADOWS="shadow"
    
    
    read BORD <<< "$(grep draw_borders ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read BWIDTH <<< "$(grep border_width ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    read STIP <<< "$(grep stippled_borders ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    
    [[ "$WINTRANS" == false ]] && BG="bgcolor='$WINCLR'" || BG=""
    
    if [[ "$filename" == *"mbcolor"* ]];then
    out+=("^sep(${title^})")
    else
    NOT_SUPPORTED="true"
    out+=("^sep(Conky: [ ${title^} ]")
    out+=("^sep(<span bgcolor='red' fgcolor='white'> ${UNSUPPORTED} </span>)")
    fi
    ### TUKILL
    out+=("   $KILL ${title^},conkyctl kill ${1##*/};${me}")
    out+=("^sep()")
if [[ "$OLD_SYNTAX" = "true" ]];then    
    out+=("$OLD_SYNTAX_DETECTED,^checkout(oldsyntax)")
    out2+=("^tag(oldsyntax)")
    out2+=("^sep($CONVERT_OLD)")
    out2+=("\"\"\"$CONVERT_OLD_TRY\"\"\",conkyctl convert_from_old ${1}")
    #notify-send.sh "Plik" "${1}"
    else
    out+=("  $POSITION,^checkout(position)")
    if [[ "$1" =~ "etwork_mbcolor" ]]; then
    out+=("$NOT_WORKING,conkyctl netdevice")
    fi
    #out+=("^sep($POSITION)")
    out2+=("^tag(position)")
    out2+=("^sep($POSITION)")
    out2+=("<big>󰆞</big>  $MARKNEWPOS,conkyctl setpos ${1};$me")
    #out2+=("^sep()")
    out2+=("^sep($ALIGNMENT)")
    out2+=("<big></big>  $TOPLEFT,conkyctl pos top_left ${1};$me")
    out2+=("<big></big>  $TOP,conkyctl pos top_middle ${1};$me")
    out2+=("<big></big>  $TOPRIGHT,conkyctl pos top_right ${1};$me")
    out2+=("<big></big>  $LEFT,conkyctl pos middle_left ${1};$me")
    out2+=("$CENTER,conkyctl pos middle_middle ${1};$me")
    out2+=("<big></big>  $RIGHT,conkyctl pos middle_right ${1};$me")
    out2+=("<big></big>  $BOTTOMLEFT,conkyctl pos bottom_left ${1};$me")
    out2+=("<big></big>  $BOTTOM,conkyctl pos bottom_middle ${1};$me")
    out2+=("<big></big>  $BOTTOMRIGHT,conkyctl pos bottom_right ${1};$me")
    
    
        if [[ "$NOT_SUPPORTED" != "true" ]];then
    out2+=("^sep()")
    out2+=("  $MAKEMOVEABLE,conkyctl makemoveableall")
    fi
       
    
    if [[ "$1" =~ "logo_mbcolor" ]]; then
    read LSIZE <<< "$(grep minimum_height ${1} | cut -d'=' -f2 | cut -d"," -f1)"
    out+=("^sep($LOGOSIZE)")
    out+=("$LOGOSIZE [ <b>$LSIZE</b> ],^checkout(logosize)")
    out2+=("^tag(logosize)")
    out2+=("^sep($LOGOSIZE)")
    out2+=("$SMALL 64px,conkyctl logosize 64;$me")
    out2+=("$NORMAL 128px,conkyctl logosize 128;$me")
    out2+=("$BIG 256px,conkyctl logosize 256;$me")
    fi
    
    if [[ "$1" =~ "quoter" ]]; then
    case "$LANG" in
        pl*)
        FONTSIZE="Czcionka i rozmiar"
        QUOTE_FSIZE="Cytat"
        AUTHOR_FSIZE="Autor"
        EDIT_CONF="Edytuj plik konfiguracyjny"
        RELOAD="Przeładuj"
        ;;
        es*)
        FONTSIZE="Font size &amp; family"
        QUOTE_FSIZE="Quote"
        AUTHOR_FSIZE="Author"
        EDIT_CONF="Edit conf file"
        RELOAD="Reload"
        ;;
        *)
        FONTSIZE="Font size &amp; family"
        QUOTE_FSIZE="Quote"
        AUTHOR_FSIZE="Author"
        EDIT_CONF="Edit conf file"
        RELOAD="Reload"
        ;;
    esac
    Q_CFG="$HOME/.config/quoter/quoter.conf"
    source "$Q_CFG"
    out+=("^sep($FONTSIZE)")
    out+=("$QUOTE_FSIZE [ <b>$quote_fontsize</b> ],^checkout(qsize)")
    out+=("$AUTHOR_FSIZE [ <b>$author_fontsize</b> ],^checkout(asize)")
    out2+=("^tag(qsize)")
    out2+=("^sep($QUOTE_FSIZE)")
    for i in 10 11 12 13 14 15 16 18 20; do
    out2+=("${i},mb-setvar quote_fontsize=$i $Q_CFG;conkyctl restartone ${1};$me")
    done
    out2+=("^tag(asize)")
    out2+=("^sep($AUTHOR_FSIZE)")
    for i in 10 11 12 13 14 15 16 18 20; do
    out2+=("${i},mb-setvar author_fontsize=$i $Q_CFG;conkyctl restartone ${1};$me")
    done
    #FONT FAMILY FOR QUOTER
    out+=("$FONT_NAME [ <b>${font_name}</b> ],^checkout(qfontfamily)")
    out2+=("^tag(qfontfamily)")
    if [ -f "$HOME/.config/mabox/fonts.list" ];then
		mapfile -t favfonts < <( grep -vE "^($|#)" ~/.config/mabox/fonts.list)
	else
		mapfile -t favfonts < <( grep -vE "^($|#)" /usr/share/mabox-colorizer/fonts.list)
		cp /usr/share/mabox-colorizer/fonts.list ~/.config/mabox/
	fi
	#notify-send.sh "Ile" "${#favfonts[@]}"
	if [ "${#favfonts[@]}" -gt 0 ];then
		out2+=("^sep($FONTFAMILY_LBL)")
		for fon in "${favfonts[@]}";do
			[[ "$fon" == "$font_name" ]] && out2+=("<big>${ron}</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>${roff}</big>  <span font_family=\"${fon}\">${fon}</span>,mb-setvar 'font_name=\"${fon}\"' $Q_CFG;conkyctl restartone ${1}")
		done
	fi
    fi
if [[ "${WINCLASS}" != *"nobg"* ]];then    
    out+=("^sep($COLORS)")
    # COLOR SCHEMES
    #if [ "$WINTRANS" == false ];then
        out+=("<span bgcolor='#282828'> <span fgcolor='#ebdbb2'></span> <span fgcolor='#98971a'></span> <span fgcolor='#d79921'></span> <span fgcolor='#cc241d'></span> </span> $COLORSCHEME,^checkout(regensingle)")
        out2+=("^tag(regensingle)")
        out2+=("^sep($MONOCHROMATIC)")
        out2+=("^sep(<i>$CHOOSE_AND_PICK</i>)")
		out2+=("<big></big>   $DARKBG...,conkyctl monoone pick dark ${1}")
		out2+=("<big></big>   $LIGHTBG...,conkyctl monoone pick light ${1}")
        out2+=("^sep($GENFROMWP)")
        out2+=("<big><span bgcolor='${w[-1]}' fgcolor='${w[0]}'>  $LIGHTBG  <span fgcolor='${w[4]}'></span>  <span fgcolor='${w[5]}'></span>  </span></big>,w2theme conkyone light ${1}")
        out2+=("<big><span bgcolor='${w[0]}' fgcolor='${w[-1]}'>  $DARKBG  <span fgcolor='${w[-4]}'></span>  <span fgcolor='${w[-5]}'></span>  </span></big>,w2theme conkyone dark ${1}")
        out2+=("^sep($APPLY_THEME)")
        out2+=("^sep($SYSTEM_SCHEMES)")
        n=1
		while IFS="|" read -r winclr clr clr0 clr1 clr2 clr3 wintrans outline shades bord bwidth stip scheme_name
		do
		((n=n+1))
		[[ "$wintrans" = true ]] && bga=1 || bga=100
            out2+=("<tt>$(printf '%-20s' "${scheme_name}")</tt><big><span bgcolor='$winclr' bgalpha='${bga}%'><span bgcolor='${clr}'>  </span>   <span fgcolor='${clr0}'></span> <span fgcolor='${clr1}'></span> <span fgcolor='${clr2}'></span> <span fgcolor='${clr3}'></span>   <span bgcolor='${clr}'>  </span></span></big>,conkyctl applyscheme sys $n ${1}")
        done  < <(tail -n +2 "$SYSSCHEMES")
        out2+=("^sep($YOUR_SCHEMES)")
        if [[ "$1" =~ "sysinfo_" ]]; then
        #check for CLR3
				if [[ "${CLR3}" == "#" ]];then
				case "$LANG" in
					pl*)_UPD_NEEDED="Dostępna aktualizacja konfigów Conky!!!"
					_UPD_NOW="Aktualizuj teraz!"
					;;
					*)_UPD_NEEDED="Conky configs update needed !!!"
					_UPD_NOW="Update now!"
					;;
				esac
				out+=("<span fgcolor='white' bgcolor='red'> $_UPD_NEEDED </span>,^checkout(conkyupd)")
				out3+=("^tag(conkyupd)")
				out3+=("^sep($_UPD_NEEDED)")
				out3+=("$_UPD_NOW <small>(mb-reset conky)</small>,mb-reset conky")
				CLR3=${CLR}
				recolor3=1
				fi
			#currcolors="${WINCLR}|${CLR}|${CLR0}|${CLR1}|${CLR2}|"
			cursettings="${WINCLR}|${CLR}|${CLR0}|${CLR1}|${CLR2}|${CLR3}|${WINTRANS}|${OUTLINE}|${SHADES}|${BORD}|${BWIDTH}|${STIP}|"
			out2+=("<big></big>   $SAVE_CURRSCHEME,conkyctl savescheme '${cursettings}'" "^sep()")
			if [[ "${recolor3}" == "1" ]];then
			CLR3="#"
			recolor3=0
			fi
        fi
        n=1
		while IFS="|" read -r winclr clr clr0 clr1 clr2 clr3 wintrans outline shades bord bwidth stip scheme_name
		do
		((n=n+1))
		[[ "$wintrans" = true ]] && bga=1 || bga=100
            out2+=("<tt>$(printf '%-20s' "${scheme_name}")</tt><big><span bgcolor='$winclr' bgalpha='${bga}%'><span bgcolor='${clr}'>  </span>   <span fgcolor='${clr0}'></span> <span fgcolor='${clr1}'></span> <span fgcolor='${clr2}'></span> <span fgcolor='${clr3}'></span>   <span bgcolor='${clr}'>  </span></span></big>,conkyctl applyscheme usr $n ${1}")
            #out2+=("<span bgcolor='$own_window_colour'>   <span fgcolor='$default_color'></span> <span fgcolor='$color0'></span> <span fgcolor='$color1'></span> <span fgcolor='$color2'></span>   </span>  $schemetitle,conkyctl settheme $themefilename $1")
                        
        done  < <(tail -n +2 "$USERSCHEMES")
        
       
      
        out+=("^sep()")
    #fi
     
    if [[ "$WINCLR" != "#" ]];then
        if [ "$WINTRANS" == false ];then
            out+=("<small><span bgcolor='$WINCLR'>   </span><span ${BG} fgcolor='$WINCLR'>  AbCd  </span></small>   $BACKGROUND,^pipe(mbclr '$WINCLR' conkyctl win_bgcolor '${BACKGROUND}' '$me' ${1})")
        else
            out+=("<small><span bgcolor='$WINCLR'> transparent </span></small>   $BACKGROUND,^pipe(mbclr '$WINCLR' conkyctl win_bgcolor '${BACKGROUND}' '$me' ${1})")
        fi
    fi
    [[ "$WINTRANS" == false ]] && out+=("<big>${cboff}</big>  $TRANSPARENT,conkyctl transparent true ${1}") || out+=("<big>${cbon}</big>  $TRANSPARENT,conkyctl transparent false ${1}")
    out+=("^sep()")
    
    if [[ "$1" != *"logo_mbcolor"* ]]; then
    
    [[ "$CLR" != "#" ]] && out+=("<span bgcolor='$CLR'> </span><span ${BG} fgcolor='$CLR'>   border  </span><span bgcolor='$CLR'> </span>   $DEFCOLOR,^pipe(mbclr '$CLR' conkyctl default_color '${DEFCOLOR}' '$me' ${1})")
    [[ "$CLR0" != "#" ]] && out+=("<span ${BG} fgcolor='$CLR0'>   AbCd   <span bgcolor='$CLR0'>   </span> </span>   $COLOR_0,^pipe(mbclr '$CLR0' conkyctl color0 '$COLOR_0' '$me' ${1})")
    [[ "$CLR1" != "#" ]] && out+=("<span ${BG} fgcolor='$CLR1'>   AbCd   <span bgcolor='$CLR1'>   </span> </span>   $COLOR_1,^pipe(mbclr '$CLR1' conkyctl color1 '$COLOR_1' '$me' ${1})")
    [[ "$CLR2" != "#" ]] && out+=("<span ${BG} fgcolor='$CLR2'>   󰍴󰍴󰍴󰍴   <span bgcolor='$CLR2'>   </span> </span>   $COLOR_2,^pipe(mbclr '$CLR2' conkyctl color2 '$COLOR_2' '$me' ${1})")
    [[ "$CLR3" != "#" ]] && out+=("<span ${BG} fgcolor='$CLR3'>  <b>AbCd</b>   <span bgcolor='$CLR3'>   </span> </span>   $COLOR_3,^pipe(mbclr '$CLR3' conkyctl color3 '$COLOR_3' '$me' ${1})")
    out+=("^sep()")
    fi
fi #Conky-nobg END

if [[ "${1}" == *"SVG_mbcolor"* ]];then
case "$LANG" in
	pl*)
	_VARIANT_AND_SIZE="Wariant i rozmiar"
	_3D="sześcian 3D"
	_SQUARE="kwadratowe"
	_CIRCLE="okrągłe"
	_LOGOSIZE="Rozmiar logo"
	_COLORS="Kolory"
	_INNER_COLOR="Wewnętrzny kolor (M)"
	_OUTER_COLOR="Zewnętrzny kolor"
	_REVERSE_COLORS="Oderóć kolory"
	_RESTORE_COLORS="Przywróć domyślne kolory"
	_OPACITY_BOTH="Nieprzejrzystość"
	_INNER_OPACITY="Nieprzejrzystość (M)"
	_OUTER_OPACITY="Nieprzejrzystość"
	;;
	*)
	_VARIANT_AND_SIZE="Variant and size"
	_3D="cube 3D"
	_SQUARE="square"
	_CIRCLE="circle"
	_LOGOSIZE="Logo size"
	_COLORS="Colors"
	_INNER_COLOR="Inner color (M)"
	_OUTER_COLOR="Outer color"
	_REVERSE_COLORS="Reverse colors"
	_RESTORE_COLORS="Restore default colors"
	_OPACITY_BOTH="Opacity (both)"
	_INNER_OPACITY="Inner opacity (M)"
	_OUTER_OPACITY="Outer opacity"
	;;
esac

CONKYLOGO="$HOME/.config/conky/Mabox_logo_SVG_mbcolor.conkyrc"
read TYP <<< "$(grep template0 ${1} | cut -d'=' -f2)"
read SIZE <<< "$(grep minimum_height ${1} | cut -d'=' -f2 | cut -d"," -f1)"
. ~/.config/mabox/mabox.conf
#notify-send.sh "Colors" "${logo_in_color} ${logo_in_opacity}\n${logo_out_color} ${logo_out_opacity}"
	out+=("^sep($_VARIANT_AND_SIZE)")
	[[ "${TYP}" == *"3d"* ]] && out+=("<big>${ron}</big>   <b>$_3D</b>,mabox-logo type 3d") || out+=("<big>${roff}</big>   $_3D,mabox-logo type 3d")
	[[ "${TYP}" == *"square"* ]] && out+=("<big>${ron}</big>   <b>$_SQUARE</b>,mabox-logo type square") || out+=("<big>${roff}</big>   $_SQUARE,mabox-logo type square")
	[[ "${TYP}" == *"circle"* ]] && out+=("<big>${ron}</big>   <b>$_CIRCLE</b>,mabox-logo type circle") || out+=("<big>${roff}</big>   $_CIRCLE,mabox-logo type circle")
	out+=("^sep()")
	out+=("$_LOGOSIZE	[ <b>${SIZE}</b>x<b>${SIZE}</b> ],^checkout(logosize)")
	out2+=("^tag(logosize)")
	out2+=("^sep($_LOGOSIZE (px))")
	for s in 32 48 64 96 128 256 512;do
	[[ "${SIZE}" == "${s}" ]] && out2+=("<big>${ron}</big>   <b>${s}</b>,mabox-logo size ${s}") || out2+=("<big>${roff}</big>   ${s},mabox-logo size ${s}")
	done
	out+=("^sep($_COLORS)")
	out+=("<span bgcolor='#32B557'>   </span><span bgcolor='${logo_in_color}'>         </span>  $_INNER_COLOR,^pipe(mbclr '${logo_in_color}' mabox-logo ic '$_INNER_COLOR')")
	out+=("<span bgcolor='#F8F8FF'>   </span><span bgcolor='${logo_out_color}'>         </span>  $_OUTER_COLOR,^pipe(mbclr '${logo_out_color}' mabox-logo oc '$_OUTER_COLOR')")
	out+=("<big>易</big>   $_REVERSE_COLORS,mabox-logo revcolors")
	out+=("<span bgcolor='#32B557'>   </span><span bgcolor='#F8F8FF'>   </span>   $_RESTORE_COLORS,mabox-logo default")
	out+=("^sep()")
	out+=("$_OPACITY_BOTH	[ <b>${logo_in_opacity}  ${logo_out_opacity}</b> ],^checkout(oboth)")
	out2+=("^tag(oboth)")
	out2+=("^sep($_OPACITY_BOTH)")
	for o in 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3;do
	out2+=("${o},mabox-logo io ${o};mabox-logo oo ${o}")
	done
	out+=("^sep()")
	out+=("<span bgcolor='${logo_in_color}'>   </span>  $_INNER_OPACITY	[ <b>${logo_in_opacity}</b> ],^checkout(iopa)")
	out2+=("^tag(iopa)")
	out2+=("^sep($_INNER_OPACITY)")
	for o in 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0;do
	[[ "${logo_in_opacity}" == "$o" ]] && out2+=("<big>${ron}</big>   <b>${o}</b>,mabox-logo io ${o}") || out2+=("<big>${roff}</big>   ${o},mabox-logo io ${o}")
	done
	out+=("<span bgcolor='${logo_out_color}'>   </span>  $_OUTER_OPACITY	[ <b>${logo_out_opacity}</b> ],^checkout(oopa)")
	out2+=("^tag(oopa)")
	out2+=("^sep($_OUTER_OPACITY)")
	for o in 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0;do
	[[ "${logo_out_opacity}" == "$o" ]] && out2+=("<big>${ron}</big>   <b>${o}</b>,mabox-logo oo ${o}") || out2+=("<big>${roff}</big>   ${o},mabox-logo oo ${o}")
	done
fi # END LOGO SVG

if [[ "${1}" == *"etter_Clock_mbcolor"* ]];then
case "$LANG" in
	pl*)
	_TIME="Godzina"
	_DATE="Data"
	_SHOW_SECONDS="Pokaż sekundy (mniejsze)"
	_SHOW_DATE="Pokaż datę"
	_TIMECOLOR="Kolor Godzina"
	_DATECOLOR="Kolor Data"
	_FONT_TIME="Czcionka Godzina"
	_FONT_DATE="Czcionka Data"
	;;
	*)
	_TIME="Time"
	_DATE="Date"
	_SHOW_SECONDS="Show seconds (smaller)"
	_SHOW_DATE="Show date"
	_TIMECOLOR="Color Time"
	_DATECOLOR="Color Date"
	_FONT_TIME="Font Time"
	_FONT_DATE="Font Date"
	;;
esac

out+=("^sep($_TIME)")
#getvalues
read TIMEFORMAT <<< "$(grep template0 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read DATEFORMAT <<< "$(grep template1 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read FONTTIME <<< "$(grep template2 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read FONTDATE <<< "$(grep template3 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read FSTIME <<< "$(grep template4 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read FSDATE <<< "$(grep template6 ${1} | cut -d'[' -f3 | cut -d']' -f1)"
read DCOLOR <<< "$(grep color9 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"
read TCOLOR <<< "$(grep color8 ${1} | cut -d'=' -f2 | cut -d"'" -f2)"

#out+=("\"\"\"<big>󱑀</big>   _TIMEFORMAT	[ <b>${TIMEFORMAT}</b> ]\"\"\"")
[[ "${TIMEFORMAT}" == "%H:%M" ]] && out+=("<big>${ron}</big>   <b>$(date +%H:%M)</b>	<small>( %H:%M )</small>") || out+=("<big>${roff}</big>   <b>$(date +%H:%M)</b>	<small>( %H:%M )</small>,conkyctl bc_tformat \"%H:%M\"")
[[ "${TIMEFORMAT}" == "%H:%M:%S" ]] && out+=("<big>${ron}</big>   <b>$(date +%H:%M:%S)</b>	<small>( %H:%M:%S )</small>") || out+=("<big>${roff}</big>   <b>$(date +%H:%M:%S)</b>	<small>( %H:%M:%S )</small>,conkyctl bc_tformat \"%H:%M:%S\"")
[[ "${TIMEFORMAT}" == "%I:%M %p" ]] && out+=("<big>${ron}</big>   <b>$(date +"%I:%M %p")</b>	<small>( %I:%M %p )</small>") || out+=("<big>${roff}</big>   <b>$(date +"%I:%M %p")</b>	<small>( %I:%M %p )</small>,conkyctl bc_tformat \"%I:%M %p\"")
[[ "${TIMEFORMAT}" == "%-I:%M %p" ]] && out+=("<big>${ron}</big>   <b>$(date +"%-I:%M %p")</b>	<small>( %-I:%M %p )</small>") || out+=("<big>${roff}</big>   <b>$(date +"%-I:%M %p")</b>	<small>( %-I:%M %p )</small>,conkyctl bc_tformat \"%-I:%M %p\"")
[[ "${TIMEFORMAT}" == "%H:%M (%a)" ]] && out+=("<big>${ron}</big>   <b>$(date +"%H:%M (%a)")</b>	<small>( %H:%M (%a) )</small>") || out+=("<big>${roff}</big>   <b>$(date +"%H:%M (%a)")</b>	<small>( %H:%M (%a) )</small>,conkyctl bc_tformat \"%H:%M (%a)\"")
out+=("^sep()")
[[ -f "${CONKYDIR}/menuscripts/.seconds" ]] && out+=("<big>${cbon}</big>   $_SHOW_SECONDS,rm ${CONKYDIR}/menuscripts/.seconds") || out+=("<big>${cboff}</big>   $_SHOW_SECONDS,touch ${CONKYDIR}/menuscripts/.seconds")
out+=("^sep()")
out+=("<big></big>  <span font='${FONTTIME}'>${FONTTIME}   ${FSTIME}</span>,^checkout(ftime)")
out2+=("^tag(ftime)")
out2+=("^sep($_FONT_TIME)")
for si in 128 96 64 48 32;do
[[ "${FSTIME}" == "$si" ]] && out2+=("<big>${ron}</big>   <b>${si} px</b>") || out2+=("<big>${roff}</big>   ${si} px,conkyctl bc_fstime ${si}")
done
out2+=("^sep()")

if [ -f "$HOME/.config/mabox/fonts.list" ];then
	mapfile -t favfonts < <( grep -vE "^($|#)" ~/.config/mabox/fonts.list)
else
	mapfile -t favfonts < <( grep -vE "^($|#)" /usr/share/mabox-colorizer/fonts.list)
	cp /usr/share/mabox-colorizer/fonts.list ~/.config/mabox/
fi
if [ "${#favfonts[@]}" -gt 0 ];then
	for fon in "${favfonts[@]}";do
		[[ "$fon" == "${FONTTIME}" ]] && out2+=("<big>${ron}</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>${roff}</big>  <span font_family=\"${fon}\">${fon}</span>,conkyctl bc_ftime \"${fon}\"")
	done
fi

out+=("^sep()")
out+=("<span bgcolor='$TCOLOR'>                </span>   $_TIMECOLOR,^pipe(mbclr '$TCOLOR' conkyctl color8 '$_TIMECOLOR' '$me' ${1})")
out+=("^sep($_DATE)")
[[ -f "${CONKYDIR}/menuscripts/.date" ]] && out+=("<big>${cbon}</big>   $_SHOW_DATE,rm ${CONKYDIR}/menuscripts/.date") || out+=("<big>${cboff}</big>   $_SHOW_DATE,touch ${CONKYDIR}/menuscripts/.date")
if [[ -f "${CONKYDIR}/menuscripts/.date" ]];then
out+=("^sep()")
#out+=("\"\"\"_DATEFORMAT	[ <b>${DATEFORMAT}</b> ]\"\"\"")
[[ "${DATEFORMAT}" == "%A, %B %e" ]] && out+=("\"\"\"<big>${ron}</big>   <b>$(date "+%A, %B %e")</b> <small>( %A, %B %e )</small>\"\"\"") || out+=("\"\"\"<big>${roff}</big>   <b>$(date "+%A, %B %e")</b> <small>( %A, %B %e )</small>\"\"\",\"\"\"conkyctl bc_dformat '%A, %B %e'\"\"\"")
[[ "${DATEFORMAT}" == "%A %e %B" ]] && out+=("<big>${ron}</big>   <b>$(date "+%A %e %B")</b> <small>( %A %e %B )</small>") || out+=("<big>${roff}</big>   <b>$(date "+%A %e %B")</b> <small>( %A %e %B )</small>,conkyctl bc_dformat '%A %e %B'")
[[ "${DATEFORMAT}" == "%a %e %b" ]] && out+=("<big>${ron}</big>   <b>$(date "+%a %e %b")</b>	<small>( %a %e %b )</small>") || out+=("<big>${roff}</big>   <b>$(date "+%a %e %b")</b>	<small>( %a %e %b )</small>,conkyctl bc_dformat '%a %e %b'")
[[ "${DATEFORMAT}" == "%F" ]] && out+=("\"\"\"<big>${ron}</big>   <b>$(date "+%F")</b>	<small>( %F )</small>\"\"\"") || out+=("<big>${roff}</big>   <b>$(date "+%F")</b>	<small>( %F )</small>,conkyctl bc_dformat \"%F\"")
[[ "${DATEFORMAT}" == "%x" ]] && out+=("\"\"\"<big>${ron}</big>   <b>$(date "+%x")</b>	<small>( %x )</small>\"\"\"") || out+=("<big>${roff}</big>   <b>$(date "+%x")</b>	<small>( %x )</small>,conkyctl bc_dformat \"%x\"")
[[ "${DATEFORMAT}" == "%m-%d-%Y" ]] && out+=("\"\"\"<big>${ron}</big>   <b>$(date "+%m-%d-%Y")</b>	<small>( %m-%d-%Y )</small>\"\"\"") || out+=("<big>${roff}</big>   <b>$(date "+%m-%d-%Y")</b>	<small>( %m-%d-%Y )</small>,conkyctl bc_dformat \"%m-%d-%Y\"")

out+=("^sep()")
out+=("<big></big>  <span font_family='${FONTDATE}'>${FONTDATE}   ${FSDATE}</span>,^checkout(fdate)")
out2+=("^tag(fdate)")
out2+=("^sep($_FONT_DATE)")
for si in 32 24 20 16 12 10;do
[[ "${FSDATE}" == "$si" ]] && out2+=("<big>${ron}</big>   <b>${si} px</b>") || out2+=("<big>${roff}</big>   ${si} px,conkyctl bc_fsdate ${si}")
done
out2+=("^sep()")
if [ "${#favfonts[@]}" -gt 0 ];then
	for fon in "${favfonts[@]}";do
		[[ "$fon" == "${FONTDATE}" ]] && out2+=("<big>${ron}</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>${roff}</big>  <span font_family=\"${fon}\">${fon}</span>,conkyctl bc_fdate \"${fon}\"")
	done
fi
out+=("^sep()")
out+=("<span bgcolor='$DCOLOR'>                </span>   $_DATECOLOR,^pipe(mbclr '$DCOLOR' conkyctl color9 '$_DATECOLOR' '$me' ${1})")
fi
fi
# END Better Clock

if [[ "$FONT" != "" ]];then
    if [[ "$1" != *"logo_mbcolor"* ]]; then
        if [[ "$1" != *"quoter_mbcolor"* ]]; then
			
			out+=("<big>󰛖</big>   $FONTS   [ <b><span font_family=\"${FONT}\">$FONT</span> $FONTSIZE</b> ],^checkout(fontsingle)")
			out2+=("^tag(fontsingle)")
			out2+=("^sep($FONTS)")
			out2+=("<big>󰛖</big>   $SELECTFONT [ <b><span font_family=\"${FONT}\">$FONT</span> $FONTSIZE</b> ],mb-setfont conky_single ${1};$me")
			out2+=("^sep($FONTSIZE_LBL)")
			out2+=("<big>󰧴</big>   $INCREASE <b>$((FONTSIZE+1)) px</b>,conkyctl basefont_inc ${1};$me")
			out2+=("<big>󰧳</big>   $DECREASE <b>$((FONTSIZE-1)) px</b>,conkyctl basefont_dec ${1};$me")
			out2+=("^sep()")
				sizes=(12 11 10 9 8 )
				for i in "${sizes[@]}"
				do
				[[ "$FONTSIZE" == "$i" ]] && out2+=("<big>${ron}</big>  <b>$i px</b>,conkyctl basefont_size ${1} $i;$me") || out2+=("<big>${roff}</big>  $i px,conkyctl basefont_size ${1} $i;$me")
				done
        
				if [ -f "$HOME/.config/mabox/fonts.list" ];then
					mapfile -t favfonts < <( grep -vE "^($|#)" ~/.config/mabox/fonts.list)
				else
					mapfile -t favfonts < <( grep -vE "^($|#)" /usr/share/mabox-colorizer/fonts.list)
					cp /usr/share/mabox-colorizer/fonts.list ~/.config/mabox/
				fi
				#notify-send.sh "Ile" "${#favfonts[@]}"
				if [ "${#favfonts[@]}" -gt 0 ];then
				out2+=("^sep($FONTFAMILY_LBL)")
					for fon in "${favfonts[@]}";do
					[[ "$FONT" == "$fon" ]] && out2+=("<big>${ron}</big>  <span font_family=\"${fon}\">${fon}</span>,$me") || out2+=("<big>${roff}</big>  <span font_family=\"${fon}\">${fon}</span>,conkyctl basefont_family ${1} \"${fon}\";$me")
					done
				fi
			out2+=("^sep()")
			out2+=("<small>   $_EDIT_FAVFONTS</small>,geany ~/.config/mabox/fonts.list")
			
        fi
        [[ "$SHADOWS" == "shadow" ]] && out+=("<big>${cbon}</big>  $SHADOW,conkyctl shades no ${1};$me") || out+=("<big>${cboff}</big>  $SHADOW,conkyctl shades shadow ${1};$me")
        [[ "$SHADOWS" == "outline" ]] && out+=("<big>${cbon}</big>  $OLINE,conkyctl shades no ${1};$me") || out+=("<big>${cboff}</big>  $OLINE,conkyctl shades outline ${1};$me")
        #out+=("^sep()")
    fi
    
    [[ "$BORD" == false ]] && out+=("<big>${cboff}</big>  $BORDERS	[ <b>$BWIDTH px</b> ],^checkout(bordersingle)") || out+=("<big>${cbon}</big>  $BORDERS	[ <b>$BWIDTH px</b> ],^checkout(bordersingle)")
    out2+=("^tag(bordersingle)")
    out2+=("^sep($DRAW $BORDERS?)")
    [[ "$BORD" == false ]] && out2+=("<big>${roff}</big>  $YES,conkyctl draw_borders true ${1};$me") || out2+=("<big>${ron}</big>  $YES,conkyctl draw_borders false ${1};$me")
    [[ "$BORD" == false ]] && out2+=("<big>${ron}</big>  $NO,conkyctl draw_borders true ${1};$me") || out2+=("<big>${roff}</big>  $NO,conkyctl draw_borders false ${1};$me")
    out2+=("^sep($_BORDER_WIDTH)")
    [[ "${BWIDTH}" == "1" ]] && out2+=("<big>${ron}</big>  <b>1 px</b> ${_OLD_STYLE}") || out2+=("<big>${roff}</big>  1 px ${_OLD_STYLE},conkyctl border_width 1 ${1}")
    for i in 2 4 6 8 10;do
    [[ "${BWIDTH}" == "${i}" ]] && out2+=("<big>${ron}</big>  <b>${i} px</b>") || out2+=("<big>${roff}</big>  ${i} px,conkyctl border_width ${i} ${1}")
    done
    out2+=("^sep($STIPPLED_BORDERS ?)")
    [[ "${STIP}" == "0" ]] && out2+=("<big>${ron}</big>  <b>0 ($SOLID_LINE)</b>") || out2+=("<big>${roff}</big>  0 ($SOLID_LINE),conkyctl stippled_borders 0 ${1};$me")
    out2+=("^sep()")
    for st in 1 2 3 4;do
    [[ "${STIP}" == "${st}" ]] && out2+=("<big>${ron}</big>  <b>${st} px</b>") || out2+=("<big>${roff}</big>  ${st} px,conkyctl stippled_borders ${st} ${1};$me")
    done
fi
fi #OLD SYNTAX CHECK    
# BASICMODE
if [[ "${conky_basicmode}" != "on" ]];then
    out+=("^sep($_ACTIONS)")
    out+=("   $EDIT ${title^},xdg-open ${1}")
    out+=("󰑤   $RELOAD ${title^},conkyctl restartone ${1}")
fi
    
}
pipemenu () {
    single "$1"
    printf '%s\n' "${out[@]}"
    printf '%s\n' "${out2[@]}"
    printf '%s\n' "${out3[@]}"
    exit 0
}

contextmenu () {
    . /usr/share/mb-jgtools/jgtools.inc

    if [[ "$conky_rightclick" == "off" ]];then
        exit 0
    fi
    
    # get conky config path from clicked "conky window"
    # maybe split this to separate function?
    eval $(xdotool getmouselocation --shell)
    eval $(xdotool getwindowgeometry --shell ${WINDOW})
    hexid=$(wmctrl -lpG | grep ${X} |grep ${Y}|grep ${WIDTH}|grep ${HEIGHT} |cut -d' ' -f1)
    CONKYCFG=$(xprop -id ${hexid}|grep WM_COMMAND|cut -d'"' -f6)
    
    if [ ! -f "$CONKYCFG" ]; then
    exit 0
    fi
    
    languages
    filename=${CONKYCFG##*/}
    name=${filename%%_mb*}
    title=${name//_/ }
    #read POS <<< "$(grep alignment ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #read GAPX <<< "$(grep gap_x ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    #read GAPY <<< "$(grep gap_y ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    
    
    #read WINTRANS <<< "$(grep own_window_transparent ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    #read FONTDEF <<< "$(grep "font .*=.*,$" ${CONKYCFG} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    #FONT=${FONTDEF%:*}
    #FONTSIZE=${FONTDEF#*=}
    #read WINCLR <<< "$(grep own_window_colour ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $WINCLR =~ ^#.* ]] && : || WINCLR="#${WINCLR}"
    
    #read CLR <<< "$(grep default_color ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $CLR =~ ^#.* ]] && : || CLR="#${CLR}"
    #read CLR0 <<< "$(grep color0 ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $CLR0 =~ ^#.* ]] && : || CLR0="#${CLR0}"
    #read CLR1 <<< "$(grep color1 ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $CLR1 =~ ^#.* ]] && : || CLR1="#${CLR1}"
    #read CLR2 <<< "$(grep color2 ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $CLR2 =~ ^#.* ]] && : || CLR2="#${CLR2}"
    #read CLR3 <<< "$(grep color3 ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    #[[ $CLR3 =~ ^#.* ]] && : || CLR3="#${CLR3}"
    #read OUTLINE <<< "$(grep draw_outline ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    #read SHADES <<< "$(grep draw_shades ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    #SHADOWS="no"
    #[[ "$OUTLINE" == "true" ]] && SHADOWS="outline"
    #[[ "$SHADES" == "true" ]] && SHADOWS="shadow"
    
    
    #read BORD <<< "$(grep draw_borders ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    #read STIP <<< "$(grep stippled_borders ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    
    #[[ "$WINTRANS" == false ]] && BG="bgcolor='$WINCLR'" || BG=""
    
    
    #notify-send.sh -t 0 "Window" "z xdotool: ${WINDOW} \n X $X \n Y $Y \n hexid: $hexid \n $CONKYCFG \n ${WINCLR}\n ${name}\n $HOME/.config/mabox/${name}.csv"
    
    

    CONFIG_FILE=$(mktemp)
    MENU_ITEMS=$(mktemp)
    trap "rm -f ${CONFIG_FILE} ${MENU_ITEMS}" EXIT
    POSITION_MODE=${1:-pointer}
    HIDE_BACK_ITEMS=1
    menu_gradient_pos=none
    jgtools_padding=4
    MENU_PADDING_TOP=${jgtools_padding:-0}

    TABS=120
    jgmenu_icon_size=0

    
    single "${CONKYCFG}" "s"
    
    case "$LANG" in
        pl*)
        EDIT_CUSTOMCOMMANDS="   Edytuj menu komend (<i>lewy klik</i>) dla <b>${name^}</b>"
        CONKY_WIDGET_MNGR="<b>Menadżer</b> widżetów <b>Conky</b>";;
        *)
        EDIT_CUSTOMCOMMANDS="   Edit cmd menu (<i>left-clik</i>) for <b>${name^}</b>"
        CONKY_WIDGET_MNGR="<b>Conky</b> Widget <b>Manager</b>    "
        ;;
    esac
    ## BASICMODE
    if [[ "${conky_basicmode}" != "on" ]];then
    out+=("^sep()")
    out+=("$EDIT_CUSTOMCOMMANDS,conkyctl cmdmenu_edit ${name}")
    fi
    out+=("^sep()")
    out+=(" ${arrow_string_left} ${arrow_string_left} ${arrow_string_left}    $CONKY_WIDGET_MNGR,colorizer-conky -s")
    
    if [[ "$CONKYCFG" =~ "mount_points" ]]; then
        out+=("^sep($MOUNTPOINTS)")    
    while read DEVICE SIZE USED FREE PERCENT MOUNT
    do
    [[ "${#MOUNT}" -gt "10" ]] && MOUNTLBL=${MOUNT##*/} || MOUNTLBL=${MOUNT}
    [[ "${#MOUNTLBL}" -gt "10" ]] && MOUNTLBL=${MOUNTLBL:0:9}
    out+=("$MOUNTLBL,^pipe(jgbrowser ${MOUNT})")
    done < <(df -h | grep -v "boot" | grep '^/dev')
    fi
    

    
    mkconfigfile
 
cat <<EOF > ${MENU_ITEMS}
$(printf '%s\n' "${out[@]}")
$(printf '%s\n' "${out2[@]}")
$(printf '%s\n' "${out3[@]}")
EOF

jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
    
}

cmdmenu () {
    source "$HOME/.config/mabox/mabox.conf"
    
    #mkdir -p "${CONKYDIR}"/menuscripts
    # get conky config path from clicked "conky window"
    eval $(xdotool getmouselocation --shell)
    eval $(xdotool getwindowgeometry --shell ${WINDOW})
    hexid=$(wmctrl -lpG | grep ${X} |grep ${Y}|grep ${WIDTH}|grep ${HEIGHT} |cut -d' ' -f1)
    CONKYCFG=$(xprop -id ${hexid}|grep WM_COMMAND|cut -d'"' -f6)
    #notify-send.sh "${X} ${Y}" "${WIDTH} ${HEIGHT}"
    
    # Fix for nonsense Conky context menu when Left Click on Root is not binded to ToogleShowDesktop
    if [ ! -f "$CONKYCFG" ]; then
    exit 0
    fi

    filename=${CONKYCFG##*/}
    name=${filename%%_mb*}
    title=${name//_/ }
    # for quoter
    if [[ "${name}" == "quoter" && ("${1}" == "up" || "${1}" == "down") ]];then
    reload "${CONKYCFG}"
    exit 0
    fi
    
    # if command exists in system run it and exit
    if hash "${name}" 2>/dev/null ;then
    ${name} ${1}
    exit 0
    fi
    
    # if conky leftclick is disabled ... exit
    if [[ "$conky_leftclick" == "off" ]];then
        exit 0
    fi
    
    
    # if script exist in menuscripts run it and exit
    if [ "$1" != "" ];then
        if [ -x "${CONKYDIR}/menuscripts/${name}" ];then
        bash -c "${CONKYDIR}/menuscripts/${name} ${1}"
        exit 0
        fi
    fi
    
    # if CSV for menu exist show it
    if [ -f ${CONKYDIR}/menuscripts/${name}.csv ]; then
        LINES=$(grep  -Ev "^#|^$" ${CONKYDIR}/menuscripts/${name}.csv|wc -l)
        #notify-send.sh "$LINES"
        if [ ${LINES} == 0 ]; then
        : # do nothing, was contextmenu
        fi
        if [ ${LINES} == 1 ]; then
		# 1 line - execute command (one line menu make no sense)
			while IFS=$'\r\n' read -r line
				do
				cmd=${line#*,}
				label=${line%,*}
			done <<< $(grep -v '^\s*$\|^#\|^\s*#' ${CONKYDIR}/menuscripts/${name}.csv)
		#notify-send.sh "${label}" "${cmd[@]}"
		sleep .1
		${cmd[@]}
		exit 0
        else
        # more than 1 lines - show menu
		CUSTOMCOMMANDS=". ${CONKYDIR}/menuscripts/${name}.csv"
		fi
    else
        : # do nothing
    fi
    
   # otherwise build and show standalone menu with custom commands
   
    sleep .1
    read WINTRANS <<< "$(grep own_window_transparent ${CONKYCFG} | cut -d'=' -f2 | cut -d"," -f1)"
    read FONTDEF <<< "$(grep "font .*=.*,$" ${CONKYCFG} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    read WINCLR <<< "$(grep own_window_colour ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $WINCLR =~ ^#.* ]] && : || WINCLR="#${WINCLR}"
    read CLR <<< "$(grep default_color ${CONKYCFG} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR =~ ^#.* ]] && : || CLR="#${CLR}"
    
    
    
    . /usr/share/mb-jgtools/pipemenu-standalone.cfg
    . $HOME/.config/mabox/mabox.conf
    CONFIG_FILE=$(mktemp)
    MENU_ITEMS=$(mktemp)
    trap "rm -f ${CONFIG_FILE} ${MENU_ITEMS}" EXIT
    POSITION_MODE=pointer
    #POSITION_MODE=fixed
    HIDE_BACK_ITEMS=1
    menu_gradient_pos=none
    jgtools_padding=4
    MENU_PADDING_TOP=${jgtools_padding:-0}
    JGWIDTH=270
    TABS=140
    jgmenu_icon_size=0
    
     mkconfigfile
        
      case "$LANG" in
        pl*)
        CUSTOM_COMMANDS_SEP="Własne polecenia"
        EDIT_CUSTOMCOMMANDS="   Edytuj menu"
        ;;
        es*)
        CUSTOM_COMMANDS_SEP="Custom commands"
        EDIT_CUSTOMCOMMANDS="   Edit menu"
        ;;
        *)
        CUSTOM_COMMANDS_SEP="Custom commands"
        EDIT_CUSTOMCOMMANDS="   Edit menu"
        ;;
    esac
cat <<EOF > ${MENU_ITEMS}
${CUSTOMCOMMANDS}
EOF

jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
}
cmdmenu_edit() {
	if [[ -s "${CONKYDIR}/menuscripts/${1}.csv" ]];then
		geany "${CONKYDIR}/menuscripts/${1}.csv"
	else
	
cat <<EOF > "${CONKYDIR}/menuscripts/${1}.csv"
# Add your commands here
# Syntax is simple: Label,command
# examples:

#The Gimp,gimp
#Firefox,firefox --new-window https://maboxlinux.org
#Btop,terminator -T Btop -e btop

# If only one command is uncommented it will run, if more commands available menu will be shown
# to see how it works play with uncomenting examples above and clicking Conky
EOF
geany "${CONKYDIR}/menuscripts/${1}.csv"
	fi
}

netdevice () {
CFGFILE="$HOME/.config/conky/Network_mbcolor.conkyrc"
rm /tmp/xx*
cd /tmp

csplit -s ${CFGFILE} /conky.text/

#IFACE=$(ip route get 8.8.8.8 | awk -- '{printf $5}')
# thanks @sng :)
IFACE=$(ip route get 8.8.8.8 | awk '{for(i=1; i<NF; i++) {if($i=="dev") dev=i+1} print $dev}')
case "$LANG" in
    pl*)
    NETWORK="SIEĆ"
    DEVICE="Urządzenie"
    IP="Adres IP"
    DOWNLOAD="Pobieranie"
    ;;
    es*)
    NETWORK="NETWORK"
    DEVICE="Device"
    IP="IP"
    DOWNLOAD="Download"
    ;;
    *)
    NETWORK="NETWORK"
    DEVICE="Device"
    IP="IP"
    DOWNLOAD="Download"
    ;;
esac

cat <<EOF >> /tmp/xx00
conky.text = [[
\${color}$NETWORK
\${color}$DEVICE \${alignr}\${color0}$IFACE
\${color}$IP \${alignr}\${color0}\${addr $IFACE}
\${color}$DOWNLOAD \$color0\${downspeed $IFACE} \${alignr}\${color0}\${upspeed $IFACE}\${color} Upload
\${color2}\${downspeedgraph $IFACE 30,100 -t} \${alignr} \${upspeedgraph $IFACE 30,100 -t}
\${color}Total Down \${color0}\${totaldown $IFACE} \${alignr}\${color0}\${totalup $IFACE}\${color} Total Up
\${color0}\${hr}
\${execi 30 netstat -ept | grep ESTAB | awk '{print \$9}' | cut -d/ -f2 | sort | uniq -c | sort -nr}
]];
EOF

cat /tmp/xx00 > ${CFGFILE}
}
savescheme () {
	case "$LANG" in
        pl*)SAVE_AS="Nowy motyw Conky"
			DESC="Zapisz obecnie ustawione kolory Conky jako nowy motyw..."
			LABEL="Nazwa:"
			NAME="nazwa";;
        *) 	SAVE_AS="New Conky colorscheme"
			DESC="Save current Conky colors as colorscheme..."
			LABEL="Name:"
			NAME="colorscheme-name";;
    esac
	# ask for scheme name
	schemename=$(yad --center --width=300 --borders=20 --window-icon=mbcc --title "$SAVE_AS" --text="$DESC" --entry  --entry-label="$LABEL" --entry-text="$NAME") || exit 1
	echo "${1}${schemename}" >> "$HOME/.config/colorizer/conky/colorschemes.csv"
}
delscheme() {
	sed -i ${1}d "$HOME/.config/colorizer/conky/colorschemes.csv"
}

applyscheme () {
case "$1" in
	sys) SCHEMEFILE="/usr/share/mabox-colorizer/colorschemes/conky.csv";;
	usr) SCHEMEFILE="$HOME/.config/colorizer/conky/colorschemes.csv";;
esac
while IFS="|" read -r own_window_colour default_color color0 color1 color2 color3 wintrans outline shades bord bwidth stip scheme_name
do
	win_bgcolor ${own_window_colour} ${3}
    default_color ${default_color} ${3}
    color0 ${color0} ${3}
    color1 ${color1} ${3}
    color2 ${color2} ${3}
    color3 ${color3} ${3}
    transparent ${wintrans} ${3}
    shadow="no"
    [[ "$outline" == true ]] && shadow="outline"
    [[ "$shades" == true ]] && shadow="shadow"
    shades "$shadow" ${3}
    stippled_borders ${stip} ${3}
    draw_borders ${bord} ${3}
    border_width ${bwidth} ${3}
    
done < <(awk 'NR == n' n=$2 "$SCHEMEFILE")
}
fix_transparent_conkies(){
	transparent true ${CONKYDIR}/Polaroid_mbcolor.conkyrc
	transparent true ${CONKYDIR}/Better_Clock_mbcolor.conkyrc
	transparent true ${CONKYDIR}/Mabox_logo_SVG_mbcolor.conkyrc
}

applyscheme_all () {
case "$1" in
	sys) SCHEMEFILE="/usr/share/mabox-colorizer/colorschemes/conky.csv";;
	usr) SCHEMEFILE="$HOME/.config/colorizer/conky/colorschemes.csv";;
esac
while IFS="|" read -r own_window_colour default_color color0 color1 color2 color3 wintrans outline shades bord bwidth stip scheme_name
do
	win_bgcolor_all ${own_window_colour}
    default_color_all ${default_color}
    color0_all ${color0}
    color1_all ${color1}
    color2_all ${color2}
    color3_all ${color3}
    transparent_all ${wintrans}
    shadow="no"
    [[ "$outline" == true ]] && shadow="outline"
    [[ "$shades" == true ]] && shadow="shadow"
    shades_all "$shadow"
    stippled_borders_all ${stip}
    draw_borders_all ${bord}
    border_width_all ${bwidth}
done < <(awk 'NR == n' n=$2 "$SCHEMEFILE")

fix_transparent_conkies
}


convert_from_old () {
        # try to convert from old syntax to new by convert.lua script
        #notify-send.sh "Convert func" "${1}"
        kill ${1}
        convert.lua ${1}
        sleep 1
        startone ${1}
}
baseconky_info() {
	#notify-send.sh -i mbcc "Important" "Baseconky info"
	case "$LANG" in
		pl*)
	TITLE="Ważna informacja"
	MSG="Podczas pracy nad nowymi schematami kolorów lub dostosowywaniem kolorów warto uruchomić Conky: Sysinfo - który jest bazowym Conky.\nJego kolory i inne zmienne są brane pod uwagę.\n\nUruchomić Sysinfo teraz?\n\n"
	START="Tak, uruchom Sysinfo Conky"
	NO="Nie"
		;;
		*)
	TITLE="Important info"
	MSG="When working on new color schemes or customizing colors, it's worth running Conky: Sysinfo - which is the base Conky. Its colors and other variables are taken into account by Colorizer.\n\nStart Sysinfo Conky now?\n\n"
	START="Yes, start Sysinfo Conky"
	NO="No"
	;;
	esac
	cmd=(
	yad --center --width=360 --borders=20 --window-icon=mbcc --image=colorizer 
	--title "${TITLE}" 
	--text="${MSG}" 
	--button="${START}:1" 
	--button="${NO}:0"
	)
	"${cmd[@]}"
	exval=$?
    #notify-send.sh -t 10000  "Exval" "${exval}"
    case $exval in
        1) startone "$HOME/.config/conky/sysinfo_mbcolor.conkyrc";;
        0) : ;;
        252): ;;
    esac
    colorizer-conky -s
}
mono () {
lighten=(0.92 0.8 0.65 0.45 0.28)
darken=(0.9 0.74 0.54 0.38 0.18)

if [ "${1}" == pick ];then 
	color=$(gpick -pso 2>/dev/null)
else
	color="${1}"
fi

if [ "${#color}" == 7 ];then
	for i in "${lighten[@]}"
	do
	pal+=("$(pastel mix -f $i "#ffffff" "$color"|pastel format hex)")
	done
	pal+=("$color")

	for i in "${darken[@]}"
	do
	pal+=("$(pastel mix -f $i "$color" "#000000"|pastel format hex)")
	done
else
	exit 0
fi

#colorize Conky
if [ "$2" == "dark" ];then
	win_bgcolor_all "${pal[10]}"
	default_color_all "${pal[0]}"
	color0_all "${pal[3]}"
	color1_all "${pal[4]}"
	color2_all "${pal[5]}"
	color3_all "${pal[6]}"
else # light
	win_bgcolor_all "${pal[4]}"
	default_color_all "${pal[9]}"
	color0_all "${pal[7]}"
	color1_all "${pal[8]}"
	color2_all "${pal[1]}"
	color3_all "${pal[6]}"
fi

fix_transparent_conkies
colorizer-conky -s 2>/dev/null
}
monoone () {
lighten=(0.92 0.8 0.65 0.45 0.28)
darken=(0.9 0.74 0.54 0.38 0.18)

if [ "${1}" == pick ];then 
	color=$(gpick -pso 2>/dev/null)
else
	color="${1}"
fi

if [ "${#color}" == 7 ];then
	for i in "${lighten[@]}"
	do
	pal+=("$(pastel mix -f $i "#ffffff" "$color"|pastel format hex)")
	done
	pal+=("$color")

	for i in "${darken[@]}"
	do
	pal+=("$(pastel mix -f $i "$color" "#000000"|pastel format hex)")
	done
else
	exit 0
fi

# colorize one Conky
if [ "$2" == "dark" ];then
	win_bgcolor "${pal[9]}" "${3}"
	default_color "${pal[8]}" "${3}"
	color0 "${pal[3]}" "${3}"
	color1 "${pal[4]}" "${3}"
	color2 "${pal[5]}" "${3}"
	color3 "${pal[6]}" "${3}"
else # light
	win_bgcolor "${pal[4]}" "${3}"
	default_color "${pal[5]}" "${3}"
	color0 "${pal[8]}" "${3}"
	color1 "${pal[9]}" "${3}"
	color2 "${pal[1]}" "${3}"
	color3 "${pal[6]}" "${3}"
fi
}

bc_fstime (){
sd "template4 =.*$" "template4 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
size="$1"
sek=$((size/2))
sd "template5 =.*$" "template5 = [[${sek}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
bc_ftime () {
sd "template2 =.*$" "template2 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
bc_fsdate () {
sd "template6 =.*$" "template6 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
bc_fdate () {
sd "template3 =.*$" "template3 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
bc_tformat() {
sd "template0 =.*$" "template0 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
bc_dformat() {
sd "template1 =.*$" "template1 = [[${1}]]," "$HOME/.config/conky/Better_Clock_mbcolor.conkyrc"
}
lclick() {
mb-setvar conky_leftclick="${1}"
}
rclick() {
mb-setvar conky_rightclick="${1}"    
}
case "$1" in
    transparent_all) transparent_all "$2";;
    transparent) transparent "$2" "$3";;
    win_bgcolor_all) win_bgcolor_all "$2";;
    win_bgcolor) win_bgcolor "$2" "$3";;
    draw_borders_all) draw_borders_all "$2" ;;
    draw_borders) draw_borders "$2" "$3";;
    border_width)border_width "$2" "$3";;
    border_width_all)border_width_all "$2";;
    stippled_borders_all) stippled_borders_all "$2" ;;
    stippled_borders) stippled_borders "$2" "$3";;
    default_color_all) default_color_all "$2";;
    color0_all) color0_all "$2";;
    color1_all) color1_all "$2";;
    color2_all) color2_all "$2";;
    color3_all) color3_all "$2";;
    default_color) default_color "$2" "$3";;
    color0) color0 "$2" "$3";;
    color1) color1 "$2" "$3";;
    color2) color2 "$2" "$3";;
    color3) color3 "$2" "$3";;
    color8) color8 "$2" "$3";;
    color9) color9 "$2" "$3";;
    basefont_inc_all) basefont_inc_all;;
    basefont_dec_all) basefont_dec_all;;
    basefont_inc) basefont_inc "$2";;
    basefont_dec) basefont_dec "$2";;
    basefont_family) basefont_family "$2" "$3";;
    basefont_family_all) basefont_family_all "$2";;
    basefont_size) basefont_size "$2" "$3";;
    basefont_size_all) basefont_size_all "$2";;
    setfont_all) setfont_all;;
    serfont) setfont;;
    shades_all) shades_all "$2";;
    shades) shades "$2" "$3";;
    logosize) logosize "$2";;
    single) pipemenu "$2";;
    pos) pos "$2" "$3";;
    gap_x) gap_x "$2" "$3";;
    gap_y) gap_y "$2" "$3";;
    setpos) setpos "$2";;
    startone) startone "$2";;
    startall) startall;;
    kill) kill "$2";;
    toggleone) toggleone "$2";;
    restartone) restartone "$2";;
    contextmenu) contextmenu;;
    cmdmenu) cmdmenu "$2";;
    cmdmenu_edit) cmdmenu_edit "$2";;
    makemoveableall) makemoveableall;;
    saveposall) saveposall;;
    resetcolorsall) resetcolorsall ;;
    resetcolorsone) resetcolorsone "$2";;
    savescheme) savescheme "$2";;
    delscheme) delscheme "$2";;
    applyscheme) applyscheme "$2" "$3" "$4";;
    applyscheme_all) applyscheme_all "$2" "$3";;
    netdevice) netdevice ;; 
    convert_from_old) convert_from_old "$2";;
    baseconky_info) baseconky_info;;
    mono) mono "$2" "$3";;
    monoone) monoone "$2" "$3" "$4";;
    fix_transparent_conkies)fix_transparent_conkies;;
    bc_fstime)bc_fstime "$2";;
    bc_ftime)bc_ftime "$2";;
    bc_fsdate)bc_fsdate "$2";;
    bc_fdate)bc_fdate "$2";;
    bc_tformat)bc_tformat "$2";;
    bc_dformat)bc_dformat "$2";;
    lclick)lclick "$2";;
    rclick)rclick "$2";;
    *) : ;;
esac
