#!/bin/bash
# bash_completion for kerl

# shellcheck disable=SC2207  # Prefer mapfile or read -a to split command output (or quote to avoid splitting)
# shellcheck disable=SC2250  # Prefer putting braces around variable references even when not strictly required
# shellcheck disable=SC2292  # Prefer `[[ ]]` over `[ ]` for tests in Bash/Ksh
# shellcheck disable=SC2312  # Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore)

_kerl() {
    if [[ -z ${KERL_BASE_DIR+x} ]]; then
        KERL_BASE_DIR="$HOME/.kerl"
    fi

    local command cur prev releases builds installations file

    command=${COMP_WORDS[1]}
    cur=${COMP_WORDS[${#COMP_WORDS[@]} - 1]}
    prev=${COMP_WORDS[${#COMP_WORDS[@]} - 2]}

    if [ "$COMP_CWORD" -eq 1 ]; then
        COMPREPLY=($(compgen -W 'build install build-install deploy update list delete path active plt status prompt cleanup emit-activate upgrade version' -- "$cur"))
    fi

    case $command in
    build)
        if [ "$COMP_CWORD" -eq 2 ]; then
            file="$KERL_BASE_DIR"/otp_releases
            if [ -f "$file" ]; then
                releases=$(cat "$file")
            fi
            COMPREPLY=($(compgen -W "git $releases" -- "$cur"))
        fi
        ;;
    install)
        file="$KERL_BASE_DIR"/otp_builds
        if [ "$COMP_CWORD" -eq 2 ] && [ -f "$file" ]; then
            builds=$(cut -d ',' -f 2 "$file")
            COMPREPLY=($(compgen -W "$builds" -- "$cur"))
        fi
        ;;
    build-install)
        if [ "$COMP_CWORD" -eq 2 ]; then
            file="$KERL_BASE_DIR"/otp_releases
            if [ -f "$file" ]; then
                releases=$(cat "$file")
            fi
            COMPREPLY=($(compgen -W "git $releases" -- "$cur"))
        fi
        ;;
    deploy)
        file="$KERL_BASE_DIR"/otp_installations
        if [ "$COMP_CWORD" -eq 3 ] && [ -f "$file" ]; then
            installations=$(cut -d ' ' -f 2 "$file")
            COMPREPLY=($(compgen -W "$installations" -- "$cur"))
        fi
        ;;
    update)
        if [ "$COMP_CWORD" -eq 2 ]; then
            COMPREPLY=($(compgen -W 'releases' -- "$cur"))
        fi
        ;;
    list)
        if [ "$COMP_CWORD" -eq 2 ]; then
            COMPREPLY=($(compgen -W 'releases builds installations' -- "$cur"))
        elif [ "$COMP_CWORD" -eq 3 ] && [ "$prev" == "releases" ]; then
            COMPREPLY=($(compgen -W 'all' -- "$cur"))
        fi
        ;;
    delete)
        if [ "$COMP_CWORD" -eq 2 ]; then
            COMPREPLY=($(compgen -W 'build installation' -- "$cur"))
        elif [ "$COMP_CWORD" -eq 3 ]; then
            if [ "$prev" == "build" ]; then
                file="$KERL_BASE_DIR"/otp_builds
                if [ -f "$file" ]; then
                    builds=$(cut -d ',' -f 2 "$file")
                    COMPREPLY=($(compgen -W "$builds" -- "$cur"))
                fi
            elif [ "$prev" == "installation" ]; then
                file="$KERL_BASE_DIR"/otp_installations
                if [ -f "$file" ]; then
                    installations=$(cut -d ' ' -f 2 "$file")
                    COMPREPLY=($(compgen -W "$installations" -- "$cur"))
                fi
            fi
        fi
        ;;
    path)
        if [ "$COMP_CWORD" -eq 2 ]; then
            file="$KERL_BASE_DIR"/otp_installations
            if [ -f "$file" ]; then
                installations=$(cut -d ' ' -f 2 "$file" | xargs basename)
                COMPREPLY=($(compgen -W "$installations" -- "$cur"))
            fi
        fi
        ;;
    cleanup)
        if [ "$COMP_CWORD" -eq 2 ]; then
            file="$KERL_BASE_DIR"/otp_builds
            if [ -f "$file" ]; then
                builds=$(cut -d ',' -f 2 "$file")
            fi
            COMPREPLY=($(compgen -W "all $builds" -- "$cur"))
        fi
        ;;
    emit-activate)
        if [ "$COMP_CWORD" -eq 2 ]; then
            file="$KERL_BASE_DIR"/otp_releases
            if [ -f "$file" ]; then
                releases=$(cat "$file")
                COMPREPLY=($(compgen -W "$releases" -- "$cur"))
            fi
        elif [ "$COMP_CWORD" -eq 3 ]; then
            file="$KERL_BASE_DIR"/otp_builds
            if [ -f "$file" ]; then
                builds=$(cut -d ',' -f 2 "$file")
                COMPREPLY=($(compgen -W "$builds" -- "$cur"))
            fi
        elif [ "$COMP_CWORD" -eq 4 ]; then
            file="$KERL_BASE_DIR"/otp_installations
            if [ -f "$file" ]; then
                installations=$(cut -d ' ' -f 2 "$file")
                COMPREPLY=($(compgen -W "$installations" -- "$cur"))
            fi
        elif [ "$COMP_CWORD" -eq 5 ]; then
            COMPREPLY=($(compgen -W "sh bash fish csh" -- "$cur"))
        fi
        ;;
    *) ;;
    esac
}

complete -F _kerl kerl
