#!/bin/bash
#
#   BunsenLabs Conky Editor
#
#   Written by damo <damo@bunsenlabs.org> for BunsenLabs Linux, April 2015
#
#   Repackaged for Manjaro by napcok <napcok@gmail.com>, March 2016
#
########################################################################
#
#   Conkys must be in $CONKYPATH
#   The name must end with "conky" or "conkyrc"
#
#   Checkmarked conkys will be opened in the text editor
#   Multiple conkys can be chosen
#
########################################################################

CONKYPATH="$HOME/.config/conky"

### DIALOG VARIABLES
DLGDEC="yad --center --borders=20 --width=400 --height=500 "
WINICON="--window-icon=distributor-logo-mabox --image=conky "
OK="--button=OK:0"
case $LANG in
    pl*)
    TITLE="Edytor Conky"
    CANCEL="--button=Anuluj:1"
    CE_DESC="Wybierz z listy Conky do edycji.\nMożesz wybrać kilka plików konfiguracyjnych.\nMożesz dodać własne pliki konfiguracyjne Conky\ndo katalogu <i>$CONKYPATH</i>"
    CE_CHOSE="Wybór"
    CE_CONFFILE="Plik konfiguracyjny"
    ;;
    *)
    TITLE="Conky Editor"
    CANCEL="--button=Cancel:1"
    CE_DESC="Choose Conky for edit.\nYou can choose multiple files.\nYou can put your own Conky configurations\ninto <i>$CONKYPATH</i> directory."
    CE_CHOSE="Choose"
    CE_CONFFILE="Configuration file"
    ;;
esac

########## FUNCTIONS ###################################################

fillArrays(){
    num="$1"
    conkysPath[$num]="$2"   # full filepath to conky
    conkysArr[$num]="$3"    # displayed name: "directory/*conky(rc)" 
}

findConky(){
# search dirs for conkys files - looking for "conky" in the name
# if "*conky(rc)" then display it

num=0
# find files in CONKYPATH with conky in the name
for x in $(find "$CONKYPATH" -type f );do 
    f=$(basename "$x")    # filename from filepath
    if [[ $f = *conkyrc ]] || [[ $f = *conky ]];then
        # filename ends with *conky or *conkyrc
        # get directory/conkyname to display in list
        CONKY=$( echo "$x" | awk -F"/" '{print $(NF-1)"/"$NF}')
        fillArrays $num "$x" "$CONKY"
        num=$(($num+1)) 
    fi
done
}
######## END FUNCTIONS #################################################

# get conky directories in .conky, add to array
findConky

# loop through arrays, and build msg text for yad dialog
unset LISTCONKY
for ((j=0; j<${#conkysArr[*]}; j++));do
    LISTCONKY="$LISTCONKY FALSE ${conkysArr[j]}"
done

## Populate yad dialog from array, get return value(s)
RET=$($DLGDEC $WINICON --list --title="$TITLE" \
    --text="$CE_DESC" \
    --checklist \
    --column="$CE_CHOSE:CHK" --column="$CE_CONFFILE:TXT" $LISTCONKY --separator=":"\
    $OK $CANCEL \
    )

if [[ $? == 1 ]]; then # cancel button pressed
    exit 0
else
    i=0
    OIFS=$IFS   # save Internal Field Separator
    IFS=":"     # separator is ":" in returned choices
    for name in $RET; do 
        retConky[$i]="$name"
        i=$(($i+1))
    done
    IFS=$OIFS   # reset IFS back to default
    
    # Find the chosen conkys and edit them
    for name in ${retConky[*]};do       # loop through checkmarked conky names
        for ((j=0; j<${#conkysPath[*]}; j++));do  # traverse through elements
            for f in ${conkysPath[j]};do    # compare with choice from dialog
                display=$( echo "$f" | awk -F"/" '{print $(NF-1)"/"$NF}')
                if [[ $display = $name ]];then
                    xdg-open "$f"
                fi
            done
        done
    done 
fi

exit 0
