function _wpg() {
    local optforprofiles
    _get_comp_words_by_ref cur
    # themes
    optforprofiles="-s -e -d -z -m -LA -A --brt --sat"
    # themes, pywal themes
	optforvariabletheme="-Ti"
    # themes, filenames
    optvariable="-i -o"
	# filenames
    optfordefault="-y -a -xa -ax"
	# templates
    optfortemplates="-xd -dx"
	# pywal backends
	optforbackends="--backends"
	# pywal themes
	optforthemes="--pywal"

    if [[ $COMP_CWORD = 1 ]]; then
        COMPREPLY=($(compgen -W "$(_parse_usage wpg)" -- "$cur"))
    elif [[ $COMP_CWORD < 4 || ${COMP_WORDS[1]} != "-s" ]]; then
        for opt in $optforprofiles; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            fi
        done
        for opt in $optbackends; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --backends)" -- "$cur"))
            fi
        done
        for opt in $optforthemes; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --pywal)" -- "$cur"))
            fi
        done
        for opt in $optvariable; do
            if [[ $opt = ${COMP_WORDS[1]} && $COMP_CWORD < 3 ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            elif [[ $opt = ${COMP_WORDS[1]} ]]; then
                compopt -o default; COMPREPLY=()
            fi
        done
        for opt in $optforvariabletheme; do
            if [[ $opt = ${COMP_WORDS[1]} && $COMP_CWORD < 3 ]]; then
                COMPREPLY=($(compgen -W "$(wpg -l)" -- "$cur"))
            elif [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg --pywal)" -- "$cur"))
            fi
        done
        for opt in $optfordefault; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                compopt -o default; COMPREPLY=()
            fi
        done
        for opt in $optfortemplates; do
            if [[ $opt = ${COMP_WORDS[1]} ]]; then
                COMPREPLY=($(compgen -W "$(wpg -xl)" -- "$cur"))
            fi
        done
    fi
}

complete -F _wpg wpg
