#!/usr/bin/env bash

# Read a directory to an array and sort it directories first.

: ${JGMENU_RESOURCE_OPENER=xdg-open}
    
    DIRECTORY=${1:-$HOME}

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

    for item in "$DIRECTORY"/*; do
        if [[ -d $item ]]; then
            dirs+=("${item##*/},^pipe(jgbrowser \"$DIRECTORY/${item##*/}\")")
            #((item_index++))

            # Find the position of the child directory in the
            # parent directory list.
           #[[ $item == "$OLDPWD" ]] &&
            #    ((previous_index=item_index))
        else
            files+=("${item##*/},xdg-open \"$DIRECTORY/${item##*/}\"")
        fi
    done
    
printf "%b\n" "^sep(<small><i>${DIRECTORY}</i></small>)"
printf "%b\n" "        Otwórz w menadżerze plików,${JGMENU_RESOURCE_OPENER} \"${DIRECTORY:-/}\""
printf "%b\n" "        Otwórz w termianlu,terminator --working-directory=\"${DIRECTORY:-/}\""
printf "%b\n" "^sep()"
printf "%b\n" "^sep(Katalogi)"
printf '%s\n' "${dirs[@]}"
printf "%b\n" "^sep(Pliki)"
printf '%s\n' "${files[@]}"
    #list=("${dirs[@]}" "${files[@]}")