#!/bin/bash

_xcodes() {
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    COMPREPLY=()
    opts="-h --help download install installed list runtimes select uninstall update version signout help"
    if [[ $COMP_CWORD == "1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case ${COMP_WORDS[1]} in
        (download)
            _xcodes_download 2
            return
            ;;
        (install)
            _xcodes_install 2
            return
            ;;
        (installed)
            _xcodes_installed 2
            return
            ;;
        (list)
            _xcodes_list 2
            return
            ;;
        (runtimes)
            _xcodes_runtimes 2
            return
            ;;
        (select)
            _xcodes_select 2
            return
            ;;
        (uninstall)
            _xcodes_uninstall 2
            return
            ;;
        (update)
            _xcodes_update 2
            return
            ;;
        (version)
            _xcodes_version 2
            return
            ;;
        (signout)
            _xcodes_signout 2
            return
            ;;
        (help)
            _xcodes_help 2
            return
            ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_download() {
    opts="--latest --latest-prerelease --aria2 --no-aria2 --directory --use-fastlane-auth --fastlane-user --data-source --color --no-color -h --help"
    opts="$opts $("${COMP_WORDS[0]}" ---completion download -- version "${COMP_WORDS[@]}")"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --aria2)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
        ;;
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
        --fastlane-user)
            COMPREPLY=( $(ls file:///opt/local/var/macports/build/xcodes-6af6afda/work/.home/.fastlane/spaceship) )
            return
        ;;
        --data-source)
            COMPREPLY=( $(compgen -W "apple xcodeReleases" -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_install() {
    opts="--path --latest --latest-prerelease --aria2 --no-aria2 --select --update -p --print-path --experimental-unxip --no-superuser --empty-trash --directory --use-fastlane-auth --fastlane-user --data-source --color --no-color -h --help"
    opts="$opts $("${COMP_WORDS[0]}" ---completion install -- version "${COMP_WORDS[@]}")"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --path)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
        ;;
        --aria2)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
        ;;
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
        --fastlane-user)
            COMPREPLY=( $(ls file:///opt/local/var/macports/build/xcodes-6af6afda/work/.home/.fastlane/spaceship) )
            return
        ;;
        --data-source)
            COMPREPLY=( $(compgen -W "apple xcodeReleases" -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_installed() {
    opts="--directory --color --no-color -h --help"
    opts="$opts $("${COMP_WORDS[0]}" ---completion installed -- version "${COMP_WORDS[@]}")"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_list() {
    opts="--directory --data-source --color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
        --data-source)
            COMPREPLY=( $(compgen -W "apple xcodeReleases" -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_runtimes() {
    opts="--include-betas -h --help download install"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case ${COMP_WORDS[$1]} in
        (download)
            _xcodes_runtimes_download $(($1+1))
            return
            ;;
        (install)
            _xcodes_runtimes_install $(($1+1))
            return
            ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_runtimes_download() {
    opts="--aria2 --no-aria2 --directory --color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --aria2)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
        ;;
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_runtimes_install() {
    opts="--aria2 --no-aria2 --directory --keep-archive --color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --aria2)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
        ;;
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_select() {
    opts="-p --print-path --directory --color --no-color -h --help"
    opts="$opts $("${COMP_WORDS[0]}" ---completion select -- versionOrPath "${COMP_WORDS[@]}")"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_uninstall() {
    opts="--empty-trash --directory --color --no-color -h --help"
    opts="$opts $("${COMP_WORDS[0]}" ---completion uninstall -- version "${COMP_WORDS[@]}")"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_update() {
    opts="--directory --data-source --color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    case $prev in
        --directory)
            COMPREPLY=( $(compgen -d -- "$cur") )
            return
        ;;
        --data-source)
            COMPREPLY=( $(compgen -W "apple xcodeReleases" -- "$cur") )
            return
        ;;
    esac
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_version() {
    opts="--color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_signout() {
    opts="--color --no-color -h --help"
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}
_xcodes_help() {
    opts=""
    if [[ $COMP_CWORD == "$1" ]]; then
        COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
        return
    fi
    COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}


complete -F _xcodes xcodes
