# dwb completion support.

__script() {
    [[ -d ~/.config/dwb/userscripts/ ]] || return 0
    files=( ~/.config/dwb/userscripts/*.js )
    COMPREPLY=( $( compgen -W "$files" -- $cur ) )
}

__profile() {
    COMPREPLY=( $( compgen -W "$( while read line; do
        [[ "$line" == \[* ]] && {
            line="${line#\[}"
            printf "${line%\]}"
        } done < "${HOME}/.config/dwb/settings" )" -- $cur ) )
}


__session() {
    local s p_found profile
    s=${#COMP_WORDS[*]}
    p_found=false

    for (( i=1; i <= s; i++ )); do
        $p_found && { profile=${COMP_WORDS[$i]}; break;}
        [[ ${COMP_WORDS[$i]} == -p ]] ||\
        [[ ${COMP_WORDS[$i]} == --profile ]] &&\
            p_found=true
    done
    $p_found || profile=default

    COMPREPLY=( $( compgen -W "$(
        while read line; do
            [[ "$line" == 'g:'* ]] && {
                line="${line#g\:}"
                printf "${line#\*} "
            }
        done < "${HOME}/.config/dwb/${profile}/session" )" -- $cur ) )
}

__execute() {
    local cmds=""
    COMPREPLY=( $( compgen -W "${cmds}" -- $cur ) )
}

__extension() {
    COMPREPLY=( $( compgen -W "$( while read line; do
        printf "${line% *} "
    done < "${HOME}/.local/share/dwb/extensions/.metadata" )" -- $cur ) )
}

__get_installed() {
    while read line; do
        printf "${line% *} "
    done < "${HOME}/.local/share/dwb/extensions/.installed"
}

__get_disabled() {
    if [ -f "${HOME}/.config/dwb/userscripts/extension_loader.js" ]; then
        while read line; do
            if [[ "$line" == '/*<'*'__DISABLED' ]]; then
                line="${line#/\*<}"
                printf "${line%___DISABLED} "
            fi
        done < ${HOME}/.config/dwb/userscripts/extension_loader.js
    fi
}

__installed_extension() {
    COMPREPLY=( $( compgen -W "$(__get_installed)" -- $cur ) )
}

__disabled_extension() {
    COMPREPLY=( $( compgen -W "$(__get_disabled)" -- $cur ) )
}
__enabled_extension() {
    local disabled="$(__get_disabled)"
    COMPREPLY=( $( compgen -W "$( for installed in $(__get_installed); do 
        [[  " ${disabled} " != *" ${installed} "* ]] && printf "${installed} "
    done)" -- $cur ) )
}

_dwb() {
    local cur prev opts lopts
    _init_completion || return 

    opts="-h -c -e -f -l -n -r -R -p -x -v -S"
    lopts="--help --check-script --embed --force --list-sessions --new-instance
    --restore --override-restore --profile --execute --version --enable-scripts
    --set-as-default"

    case "${prev}" in
        -c|--check-script)
            __script
            return 0;;
        -r|--restore)
            __session
            return 0;;
        -p|--profile)
            __profile
            return 0;;
        -x|--execute)
            __execute
            return 0;;
    esac

    case "${cur}" in
        --*)
            COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
            return 0;;
        -*)
            COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
            return 0;;
        *)
            _filedir
            return 0;;
    esac
} &&
complete -F _dwb dwb

_dwbem() {
    local cur prev opts lopts
    _init_completion || return 

    opts="-h -a -b -B -c -d -e -E -i -I -l -L -n -N -r -p -u -U"
    lopts="--help --list-all --bind --setbind --config --disable --enable --edit
    --install --info --list-installed --setload --no-config --no-confirm
    --remove --proxy --upgrade --update"

    extinstops=" -B --setbind -c --config -d --disable -e --enable -E --edit
    -L --setload -r --remove -U --update "
    extallops=" -i --install -I --info "

    [[ " -d --disable " == *" ${prev} "* ]] && {
        __enabled_extension
        return 0
    }
    [[ " -e --enable " == *" ${prev} "* ]] && {
        __disabled_extension
        return 0
    }
    [[ "${extinstops}" == *" ${prev} "* ]] && {
        __installed_extension
        return 0
    }
    [[ "${extinstops}${extallops}" == *" ${prev} "* ]] && {
        __extension
        return 0
    }

    case "${cur}" in
        --*)
            COMPREPLY=( $( compgen -W "${lopts}" -- $cur ) )
            return 0;;
        *)
            COMPREPLY=( $( compgen -W "${opts} ${lopts}" -- $cur ) )
            return 0;;
    esac
} && 
complete -F _dwbem dwbem

# vim: set ft=sh:
