#compdef colorscript

DIR_COLORSCRIPTS="/opt/shell-color-scripts/colorscripts"
if command -v find &>/dev/null; then
    LS_CMD="$(command -v find) ${DIR_COLORSCRIPTS} -maxdepth 1 -type f | xargs -I$ basename $"
else
    LS_CMD="$(command -v ls) ${DIR_COLORSCRIPTS} | xargs -I$ basename $"
fi

function _colorscript {
    local curcontext="$curcontext" state line
    typeset -A opt_args

    _arguments \
        '1: :->subcmd'\
        '2: :->colorscript'

    case $state in
    subcmd)
        _arguments "1:subcmd:((help:'Print help'\
            list:'List all color scripts.'\
            exec:'Run a specific color script.'\
            blacklist:'Blacklist a specific color script.'\
            all:'Run all the color scripts.'\
            random:'Run a random color script.'))"
        ;;
    *)
        case $words[2] in
        "exec")
            for colorscript in $(sh -c $LS_CMD); do
                compadd ${colorscript}
            done
            ;;
        "blacklist")
            for colorscript in $(sh -c $LS_CMD); do
                compadd ${colorscript}
            done
            ;;
        *)
            ;;
        esac
    esac
}

_colorscript "$@"
