#!/bin/bash


bars=( $(ls "$HOME/.config/phyos/dwmblocks/statusbar") )
declare bars
vals=$bars
declare vals

tput setaf 39
echo "### Choose your blocks by pressing enter, press STOP to finalize ###"
tput sgr0
echo
for ((i=0; i<${#bars[@]}; i++)); do
    vals[i]="$i: ${bars[i]::-4}"
done
sb="blocks = (\n"
c=$(printf "%s\n" "${vals[@]}" | gum choose | tr "\n" " ")
vals=("---STOP---" "${vals[@]}")
while true; do
    [[ -z $c ]] && exit 1
    [[ $c == "---STOP--- " ]] && break
    i=$(echo $c | awk '{print $1}'); i=${i::-1}
    for j in "${!vals[@]}"; do
        t=$(echo ${vals[j]} | awk '{print $1}'); t=${t::-1}
        if [[ $t = $i ]]; then
            tput setaf 46
            echo "${vals[j]}"
            unset 'vals[j]'
            tput sgr0
        fi
    done
    sb+="@include \"statusbar/${bars[i]}\",\n"
    c=$(printf "%s\n" "${vals[@]}" | gum choose | tr "\n" " ")
done

sb="${sb::-3}\n);"

printf "$sb" > "$HOME/.config/phyos/dwmblocks/dwmblocks.cfg"
pOS-powermenu restart
