#!/usr/bin/env bash
# Daniel Napora 2020 - 2025 <danieln@maboxlinux.org>
. "$HOME"/.config/mabox/mabox.conf
if [ -f "$HOME/.config/gtk-3.0/bookmarks" ];then
  BOOKMARKS="$HOME/.config/gtk-3.0/bookmarks"
  else
  BOOKMARKS="$HOME/.gtk-bookmarks"
fi

case $LANG in
    pl*)
        DIRECTORIES="Katalogi"
        FILES="Pliki"
        ADDBOOKMARK="Dodaj do zakładek"
        QDIRSTAT="QDirStat (statystyki katalogów)"
        OPENDIR="Otwórz w menadżerze plików"
        OPENTERM="Otwórz w terminalu"
        ;;
    es*)
        DIRECTORIES="Directorios"
        FILES="Archivos"
        ADDBOOKMARK="Agregar a marcadores"
        QDIRSTAT="QDirStat (directory statistics)"
        OPENDIR="Abrir Gestor de archivos aquí"
        OPENTERM="Abrir terminal aquí"
        ;;
    *)
        DIRECTORIES="Directories"
        FILES="Files"
        ADDBOOKMARK="Add to Bookmarks"
        QDIRSTAT="QDirStat (directory statistics)"
        OPENDIR="Open filemanager here"
        OPENTERM="Open terminal here"
     ;;
esac
    
: "${JGMENU_RESOURCE_OPENER="xdg-open"}"

    
    DIRECTORY=${1:-$HOME}
    OPTION=${2:-""}

    # If '$DIRECTORY' is '/', unset it to avoid '//'.
    [[ $DIRECTORY == / ]] && DIRECTORY=

if [ "$jgbrowser_show_hidden" == false ];then
    for item in "$DIRECTORY"/*; do
        if [[ -d $item ]]; then
            dirs+=("${item##*/},^pipe(jgbrowser \"$DIRECTORY/${item##*/}\" \"${2}\")")
        else
        if [[ -f $item ]]; then
            files+=("${item##*/},xdg-open \"$DIRECTORY/${item##*/}\"")
        fi
        fi
    done

else
    for item in "$DIRECTORY"/* "$DIRECTORY"/.[^.]*; do
        if [[ -d $item ]]; then
            dirs+=("${item##*/},^pipe(jgbrowser \"$DIRECTORY/${item##*/}\" \"${2}\")")
        else
        if [[ -f $item ]]; then
            files+=("${item##*/},xdg-open \"$DIRECTORY/${item##*/}\"")
        fi
        fi
    done
fi
printf "%b\n" "^sep(<small><i>${DIRECTORY:-/}</i></small>)"
printf "%b\n" "<big></big> $OPENDIR,${JGMENU_RESOURCE_OPENER} \"${DIRECTORY:-/}\""
printf "%b\n" "<big></big> $OPENTERM,exo-open --launch TerminalEmulator --working-directory=\"${DIRECTORY:-/}\""


if [[ "${2}" == "-q" ]];then
printf "%b\n" "^sep()"
printf "%b\n" "󰀻  ${QDIRSTAT},qdirstat ${DIRECTORY:-/}"
elif [[ "${2}" == "-b" ]];then
:
else
#printf "%b\n" "  ${ADDBOOKMARK},\"\"\"echo \"  ${DIRECTORY##*/},^pipe(jgbrowser ${DIRECTORY:-/})\" >> $HOME/.config/mabox/places.csv;mb-places -s\"\"\""
printf "%b\n" "^sep()"
printf "%b\n" "<big>󰃅</big>  ${ADDBOOKMARK},\"\"\"echo \"file://${DIRECTORY:-/} ${DIRECTORY##*/} \" >> ${BOOKMARKS};mb-places -s\"\"\""
fi



#printf "%b\n" "    $OPENTERM,terminator --working-directory=\"${DIRECTORY:-/}\""
if [ ${#dirs[@]} -ne 0 ]; then
    printf "%b\n" "^sep($DIRECTORIES <small>(${#dirs[@]})</small>)"
    printf "%s\n" "${dirs[@]/&/&amp;}"
fi
if [ ${#files[@]} -ne 0 ]; then
    printf "%b\n" "^sep($FILES <small>(${#files[@]})</small>)"
    #printf "%s\n" "${files[@]}"
    #okfiles=("${files[@]/</ &lt;}")
    printf "%s\n" "${files[@]/&/&amp;}"
fi

