#!/bin/bash
# mb-places
#
. /usr/share/mb-jgtools/jgtools.inc

PLACES="$HOME/.config/mabox/places.csv"

gtkbookmarks() {
  if [ -f "$HOME/.config/gtk-3.0/bookmarks" ];then
  BOOKMARKS="$HOME/.config/gtk-3.0/bookmarks"
  else
  BOOKMARKS="$HOME/.gtk-bookmarks"
  fi
    if [[ -s "$BOOKMARKS" ]]; then
    case $LANG in
        pl*) GTK_BOOKMARKS="Zakładki";;
        es*) GTK_BOOKMARKS="Marcadores" ;;
        de*) GTK_BOOKMARKS="Lesezeichen" ;;
        *) GTK_BOOKMARKS="Bookmarks" ;;
    esac
    out+=("^sep($GTK_BOOKMARKS)")
        while read -r line
        do
        case $line in
        file*)
        uri=$(echo $line | cut -b 8- | cut -d' ' -f1 | sed 's/%20/ /g')
        label=$(echo $line | cut -d' ' -f2-4 | sed 's/%20/ /g')
        out+=(" ${label},^pipe(jgbrowser \"${uri}\" -b)")
        ;;
        sftp*|ssh*)
        uri=$(echo $line | cut -d' ' -f1 | sed 's/%20/ /g')
        label=$(echo $line | cut -d' ' -f2-4 | sed 's/%20/ /g')
        out+=("󰡰  ${label}, pcmanfm -n ${uri}")
        ;;
        ftp*)
        uri=$(echo $line | cut -d' ' -f1 | sed 's/%20/ /g')
        label=$(echo $line | cut -d' ' -f2-4 | sed 's/%20/ /g')
        out+=("󰡰  ${label}, pcmanfm -n ${uri}")
        ;;
        smb*)
        uri=$(echo $line | cut -d' ' -f1 | sed 's/%20/ /g')
        label=$(echo $line | cut -d' ' -f2-4 | sed 's/%20/ /g')
        out+=("󰡰  ${label}, pcmanfm -n ${uri}")
        ;;
        esac
        done <"$BOOKMARKS"
    #printf "%b\n" "^sep($GTK_BOOKMARKS)"
    #printf '%s\n' "${dirs[@]}"
    fi
}


main() {
case $LANG in
    pl*)
        LABEL="Miejsca"
        ROOTDIR="Katalog główny"
        HOME_DIR="Katalog domowy"
        MOUNTPOINTS="Dyski (Punkty montowania)"
        SHOW_HIDDEN="Wyświetlanie ukrytych plików"
        EDIT="Edytuj"
        FILETOOLS="Narzędzia plikowe"
        INST="Instaluj"
        SETTINGS="Ustawienia"
        ;;
    es*)
        LABEL="Places"
        ROOTDIR="Root Directory"
        HOME_DIR="Home Directory"
        MOUNTPOINTS="Discs (mount points)"
        SHOW_HIDDEN="Show hidden files"
        EDIT="Edit"
        FILETOOLS="File Tools"
        INST="Install"
        SETTINGS="Settings"
        ;;
    *)
        LABEL="Places"
        ROOTDIR="Root Directory"
        HOME_DIR="Home Directory"
        MOUNTPOINTS="Discs (mount points)"
        SHOW_HIDDEN="Show hidden files"
        EDIT="Edit"
        FILETOOLS="File Tools"
        INST="Install"
        SETTINGS="Settings"
        ;;
esac

#if [ ! -f ${PLACES} ]; then
#cat <<EOF > ${PLACES}
out+=("^sep($LABEL <span font_weight='normal'>[  + . ]</span>)")
#out+=("^sep($LABEL <small>[W-.]</small>)")
out+=("󰋜  $HOME_DIR (<b>~</b>),^pipe(jgbrowser),folder-home")
out+=("  $(basename $(xdg-user-dir DOWNLOAD)),^pipe(jgbrowser $(xdg-user-dir DOWNLOAD)),folder-download")
out+=("  $(basename $(xdg-user-dir DOCUMENTS)),^pipe(jgbrowser $(xdg-user-dir DOCUMENTS)),folder-documents")
out+=("󰌳  $(basename $(xdg-user-dir MUSIC)),^pipe(jgbrowser $(xdg-user-dir MUSIC)),folder-music")
out+=("  $(basename $(xdg-user-dir PICTURES)),^pipe(jgbrowser $(xdg-user-dir PICTURES)),folder-pictures")
out+=("  $(basename $(xdg-user-dir VIDEOS)),^pipe(jgbrowser $(xdg-user-dir VIDEOS)),folder-video")
#^sep()
#  ~/.config,^pipe(jgbrowser ~/.config),folder
#EOF
#fi
#mapfile -t out < "$PLACES"
gtkbookmarks

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:14}
[[ "${MOUNT}" = *"media"* ]] && ICON="<big>󱊟</big>" || ICON="<big>󰋊</big>"
out+=("$ICON   $MOUNTLBL,^pipe(jgbrowser ${MOUNT})")
done < <(df -h | grep '^/dev'| grep -v "boot" | grep -v 'loop')

out2+=("^tag(inst)")

out+=("^sep($FILETOOLS)")
if hash pcmanfm &>/dev/null; then
out+=("<big></big>  PCManFM,pcmanfm,file-manager")
fi
if hash thunar &>/dev/null; then
out+=("<big></big>  Thunar,thunar,file-manager")
fi
if hash fsearch &>/dev/null; then
out+=("<big>󰥨</big>  FSearch,fsearch,search")
fi

if hash doublecmd &>/dev/null; then
out+=("<big></big>  Double Commander,doublecmd,doublecmd")
else
out2+=("$INST Double Commander,pamac-installer doublecmd-gtk2,package-manager-icon")
fi


if hash qdirstat &>/dev/null; then
out+=("<big>󰀻</big>   QDirStat,qdirstat,qdirstat")
else
out2+=("$INST QDirStat,pamac-installer qdirstat,package-manager-icon")
fi

if (( ${#out2[@]} > 1 )); then
out+=("^sep()")
out+=("$INST ...,^checkout(inst),package-manager-icon ")
fi

out+=("^sep($SETTINGS)")
[[ $jgbrowser_show_hidden = false ]] && out+=("<big>${cboff}</big>  <i>$SHOW_HIDDEN</i>,mb-setvar jgbrowser_show_hidden=true;mb-places -s") || out+=("<big>${cbon}</big>  $SHOW_HIDDEN,mb-setvar jgbrowser_show_hidden=false;mb-places -s")
#out+=("   $EDIT $LABEL,xdg-open $PLACES,geany")
}


### RUN
main

if [[ "$1" == "-s" || "$1" == "ipc" || "$1" == "-d" ]]; then

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"

if [[ "$1" == "ipc" ]]; then 
POSITION_MODE="ipc"
else
POSITION_MODE="fixed"
fi
[[ "$1" == "-d" ]] && POSITION_MODE=pointer

[ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
[ -z $jgmenu_use_borders ] && menu_border=0
icons=0
iconmargin=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_jgdesktops=false;mb-jgtools places"
fi
printf '%s\n' "${out2[@]}"
