#!/bin/bash

CONKYDEFAULT="$HOME/.conkyrc"
CONKYPATH="$HOME/.config/conky"
CSESSIONFILE="$CONKYPATH/conky-sessionfile"
SESSIONS="$CONKYPATH/saved-sessions"
CZEN="mb-conky-manager"
CSESSION="mb-conky-session"

if [ ! -e "$CSESSIONFILE" ] 2> /dev/null ; then
    echo "Error: Failed to locate conky-sessionfile in $CONKYPATH" >&2
    echo "conky -c $HOME/.conkyrc & sleep 1" > "$CSESSIONFILE"
fi
if [ ! -e "$SESSIONS" ] 2> /dev/null ; then
    echo "$0: Failed to locate saved-sessions in $CONKYPATH" >&2
fi

case $LANG in
    pl*)
    ENABLE="Uruchom sesję Conky"
    DISABLE="Zatrzymaj sesję Conky"
    CHOOSE_CONKY="Wybierz Conky..."
    SESSIONS_LBL="Sesje Conky"
    NEW_SESSION="Nowa sesja Conky"
    RELOAD="Przeładuj Conky"
    SAVED_SESSIONS="Zachowane sesje"
    DEFAULT_CONKYRC="Domyślne conkyrc"
    RUNNING_CONKY="Działające Conky"
    EDIT_CONKY="Edytuj Conky"
    CONKY_EDITOR="Wybierz plik Conky do edycji..."
    NONE="Brak"
    DEFAULT="Domyślna"
    REMOVE="Usuń to menu z panelu"
    ;;
    es*)
    ENABLE="Start Conky session"
    DISABLE="Stop Conky session"
    CHOOSE_CONKY="Elegir recuadro Conky..."
    SESSIONS_LBL="Conky Sesiones"
    NEW_SESSION="Guardar sesión actual Conky"
    RELOAD="Recargar recuadros Conky"
    SAVED_SESSIONS="Sesiones Conky guardadas"
    DEFAULT_CONKYRC="Archivo conkyrc x defecto"
    RUNNING_CONKY="Recuadros en ejecución"
    EDIT_CONKY="Edición manual recuadro"
    CONKY_EDITOR="Editor de recuadro"
    NONE="Ninguno"
    DEFAULT="Default"
    REMOVE="Eliminar este menú del panel"
    ;;
    *)
    ENABLE="Start Conky session"
    DISABLE="Stop Conky session"
    CHOOSE_CONKY="Choose Conky..."
    SESSIONS_LBL="Conky Sessions"
    NEW_SESSION="New Conky session"
    RELOAD="Reload Conky"
    SAVED_SESSIONS="Saved sessions"
    DEFAULT_CONKYRC="Default conkyrc"
    RUNNING_CONKY="Running Conky"
    EDIT_CONKY="Edit Conky"
    CONKY_EDITOR="Conky Editor"
    NONE="None"
    DEFAULT="Default"
    REMOVE="Remove this menu from panel"
    ;;
esac

me="jgconky-pipe -s"

EditMenu(){
    if [[ $(pidof conky) ]];then
        out+=("$RUNNING_CONKY,^checkout(RUNNING)")
        out2+=("^tag(RUNNING)")
        out2+=("^sep($RUNNING_CONKY)")
        while read -r session ;do  # get running conkys from sessionfile
            [[ -z $session ]] && continue
            CPATH=$(echo "$session" | awk '{print $3}')
            #CONKY=$(echo "$CPATH" | awk -F"/" '{print $(NF-1)"/"$NF }')
            CONKY=${CPATH##*/}
            out2+=("$CONKY,xdg-open $CPATH")
        done < "$CSESSIONFILE"
        
    else
        out2+=("^tag(RUNNING)")
        out2+=("$NONE")
    fi
}


out+=("^sep(Conky)")
if [[ $(pidof conky) ]];then
    out+=("$DISABLE,conky_toggle")
else
    out+=("$ENABLE,conky_toggle;$me")
fi

if type "$CZEN" &>/dev/null;then  # mb-conky-manager script found
        out+=("$CHOOSE_CONKY,$CZEN")
fi

if type "$CSESSION" &>/dev/null;then  # mb-conky-session script found
        out+=("$RELOAD,$CSESSION")
fi

out+=("^sep($EDIT_CONKY)")
if type mb-conkyedit &>/dev/null;then  # mb-conkyedit script found
            out+=("$CONKY_EDITOR,mb-conkyedit")
            EditMenu
        else
            EditMenu
fi

out+=("^sep($SESSIONS_LBL)")
    if type "$CZEN" &>/dev/null;then  # mb-conkyzen script found
        out+=("$NEW_SESSION,$CZEN -z")
    fi

    if type "$CSESSION" &>/dev/null;then  # mb-conky-session script found
        out+=("^sep($SAVED_SESSIONS)")
        if [[ -e "$SESSIONS" ]]
            then
                while read -r session ;do
                    [[ -z $session ]] && continue   # skip if line is blank
                    out+=("${session##*/},$CSESSION $session")
                done < "$SESSIONS"
        else
            echo "$0: Failed to locate saved-sessions in $CONKYPATH" >&2
        fi
    fi
out+=("^sep(Colorizer)")
out+=("Colorizer - Conky,colorizer-conky -s")


if [ $1 == "-s" ]; then
. /usr/share/mb-jgtools/pipemenu-standalone.cfg
. $HOME/.config/mabox/mabox.conf

if [[ $panels_heightpos == "top" ]]; then
MENU_VALIGN="top"
MENU_MARGIN_Y=${panels_topmargin:-0}
elif [[ $panels_heightpos == "bottom" ]]; then
MENU_VALIGN="bottom"
MENU_MARGIN_Y=${panels_topmargin:-0}
else 
    MENU_VALIGN="center"
fi
MENU_PADDING_TOP=${jgtools_padding:-0}
MENU_HALIGN="left"
MENU_MARGIN_Y=40
jgmenu_icon_size=0
menu_margin_x=${submenu_spacing:-0}
[ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius

[ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
[ -z $jgmenu_use_borders ] && menu_border=0

mkconfigfile

cat <<EOF > ${MENU_ITEMS}

$(printf '%s\n' "${out[@]}")
$(printf '%s\n' "${out2[@]}")
EOF

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

exit 0

fi



printf '%s\n' "${out[@]}"


if [ $1 == "-r" ]; then
printf '%s\n' "^sep()"
printf '%s\n' "<i>$REMOVE</i>,mb-setvar places_conkypipe=false;mb-jgtools places"
fi
printf '%s\n' "${out2[@]}"