#!/usr/bin/env bash

# path to then soure code
if [ "$NV_PATH" = "" ]; then
    export NV_PATH=$HOME/.envirius
fi

# path to the cache/environment/etc
if [ "$NV_HOME" = "" ]; then
    export NV_HOME=$HOME/.envirius
fi

# load common functions
. $NV_PATH/nv_common


nv_usage() {
    nv version
    nv_show_usage "<subcommand>"
    echo
    echo "Subcommands:"
    nv ls-commands
    echo
    echo "Try '$(nv_cmd_name) <subcommand> --help' for details."
    echo "For more information, see: https://github.com/ekalinin/envirius"
}


nv() {
    # enable debug mode
    if [ "$DEBUG" = "yes" ]; then
        set -x
    else
        set +x
    fi

    local cache=$(nv_get_cache_full_path)
    local src=$(nv_get_src_path)
    local build=$(nv_get_build_path)
    local arc=$(nv_get_arc_path)

    mkdir -p $(nv_get_env_full_path)
    mkdir -p "$cache"
    mkdir -p "$build"
    mkdir -p "$src"
    mkdir -p "$arc"

    # some migrations
    local migrated_sign=$(nv_get_cache_full_path 'migrated_on_0.7.0')
    if [ ! -f $migrated_sign ]; then
        local -a cmn_args
        cmn_args="-mindepth 1 -maxdepth 1"
        find "$cache" $cmn_args -type d -iname "*-build" -exec mv {} "$build" \;
        find "$cache" $cmn_args -type d -iname "*-src" -exec mv {} "$src" \;
        find "$cache" $cmn_args -type f -iname "*tar*" -exec mv {} "$arc" \;
        touch $migrated_sign
    fi;

    # get command from first command line arg
    SUBCOMMAND="$1"; shift
    local command_full_path=$(nv_get_command_full_path $SUBCOMMAND)

    # show help if command is empty
    if [ "$SUBCOMMAND" = "" ]; then
        nv_usage
        return
    # show help if command not exists
    elif [ ! -e "$command_full_path" ]; then
        nv_usage
        return
    fi

    # run command
    . "$command_full_path"

    SUBACTION="default"
    if [ "$1" = "--help" ]; then
        SUBACTION="help";
    fi

    # run subaction
    nv_cmd_$SUBACTION "$@"
    unset SUBCOMMAND
    unset SUBACTION
}

if [ "$(nv_is_zsh)" = "yes" ]; then
    autoload -U +X bashcompinit && \
        bashcompinit
fi

if [ "$(nv_is_bash)" = "yes" ] || [ "$(nv_is_zsh),$+functions[bashcompinit]" = "yes,1" ]; then

    _nv() {
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
        cmd="${COMP_WORDS[1]}"

        if [ $COMP_CWORD == 1 ] ; then
            cmd_list=$(nv ls-commands | cut -d ' ' -f 3)
            COMPREPLY=( $(compgen -W "${cmd_list}" -- ${cur}) )
            return 0
        fi

        # ls-versions|mk --<plugin>[ --<plugin>]
        if [[ ${cur} == -* && ("${cmd}" == "ls-versions" || "${cmd}" == "mk") ]] ; then
            local opt_plugins=$(nv ls-plugins | awk '{print "--"$1}')
            local opt_others=$(nv ${cmd} --help | grep "  \-\-" | cut -d ' ' -f 3)
            COMPREPLY=( $(compgen -W "${opt_plugins} ${opt_others}" -- ${cur}) )
            return 0
        fi

        # rm <environment-name>[ <environment-name>]
        if [[ "${cmd}" == "rm" ]] ; then
            local env_list=$(nv ls --no-meta | tail -n+2)
            COMPREPLY=( $(compgen -W "${env_list}" -- ${cur}) )
            return 0
        fi

        # any option from command's help output
        if [[ ${cur} == -* ]] ; then
            local command_opts=$(nv ${cmd} --help | grep "\-\-" | cut -d ' ' -f 3)
            COMPREPLY=( $(compgen -W "${command_opts}" -- ${cur}) )
            return 0
        fi

        # on|activate|use|do <environment-name>
        if [[ "${cmd}" == "on" || "${cmd}" == "activate" || "${cmd}" == "use" || "${cmd}" == "do" ]] ; then
            local env_list=$(nv ls --no-meta | tail -n+2)
            COMPREPLY=( $(compgen -W "${env_list}" -- ${cur}) )
            return 0
        fi
    }

    complete -F _nv nv

elif [ "$(nv_is_zsh),$+functions[bashcompinit]" = "yes,0" ]; then

    local -a cmds
    cmds=($(nv ls-commands | cut -d ' ' -f 3))
    compctl -k cmds nv

fi

# /* vim: set filetype=sh : */
