#!/usr/bin/env bash

# Returns main command name
nv_cmd_name() {
    echo "nv"
}

nv_bold() {
    local bold=`tput bold`
    local normal=`tput sgr0`
    echo -n "${bold}$1${normal}"
}

# Output a usage message
nv_show_usage() {
    nv_bold "Usage";
    if [ "$SUBCOMMAND" = "" ]; then
        echo ": $(nv_cmd_name) $1"
    else
        echo ": $(nv_cmd_name) $SUBCOMMAND $1"
    fi
}

# Output a description for command
nv_show_desc() {
    nv_bold "Description"; echo ": $1"
}

# Pads the left-side of a string with a specific set of characters
#
# $ nv_lpad 3 5 0
# 00003
#
# http://techknack.net/bash-rpad-lpad-and-center-pad/
nv_lpad() {
    if [ "$1" ]; then
        local word="$1";
    else
        local word="";
    fi;

    if [ "$2" ]; then
        local len=$2;
    else
        local len=${#word};
    fi;

    if [ "$3" ]; then
        local padding="$3";
    else
        local padding=" ";
    fi;

    while [ ${#word} -lt $len ]; do
        word="$padding$word";
    done;
    echo "$word";
}

# Pads the right-side of a string with a specific set of characters
#
# $ nv_rpad "w00t" 15 ^
# w00t^^^^^^^^^^^
#
nv_rpad() {
    if [ "$1" ]; then
        local word="$1";
    else
        local word="";
    fi;

    if [ "$2" ]; then
        local len=$2;
    else
        local len=${#word};
    fi;

    if [ "$3" ]; then
        local padding="$3";
    else
        local padding=" ";
    fi;

    while [ ${#word} -lt $len ]; do
        word="$word$padding";
    done;
    echo "$word";
}

# Returns a clear plugin name
#   '--rust'     --> 'rust'
#   '--rust=0.9' --> 'rust'
nv_get_plugin_name() {
    echo $1 | sed 's/\--//' | sed 's/=.*//'
}

# Returns plugin version
#   '--rust=0.9' --> '0.9'
nv_get_plugin_version() {
    if [ "$(echo $1 | grep -o '=' | wc -l)" = "0" ]; then
        echo ""
    else
        echo $1 | sed 's/[-a-zA-Z0-9]*=//'
    fi
}

# Downloads url into cache with certain name
nv_download_file() {
    local url="$1"
    local file="$2"

    if [ "$file" = "" ]; then
        file=$(nv_get_filename $url)
    fi
    local file_in_cache=$(nv_get_arc_path $file)
    local cache_dir=$(nv_get_arc_path)

    local user_agent="envirius: $(nv version)"

    # if file exists we just check it content equel the source
    if type curl &>/dev/null; then
        curl --fail -A "$user_agent" -C - -L -o "$file_in_cache" "$url"
    elif type wget &>/dev/null; then
        wget -U "$user_agent" -c -P $cache_dir "$url"
    else
      echo "Please, install 'curl' or 'wget' and try again."
      exit 1
    fi

    echo "$file_in_cache"
}

# Returns filename only from full path or url
nv_get_filename() {
    echo "${1##*/}"
}

nv_get_filename_ext() {
    echo "$1" | egrep -o "[^.]+\.[^.]+$"
}

# Returns number of cpu on the server
nv_get_cpu_count() {
    case $(uname) in
        Linux)
            grep processor /proc/cpuinfo | wc -l
            ;;
        Darwin)
            system_profiler SPHardwareDataType \
                | grep 'Number of Processors' \
                | cut -d : -f 2
            ;;
    esac
}

# Get current time in seconds
nv_get_time() {
    echo `date +%s`
}

# Returns diff in secons with enter timestamp
nv_get_time_diff() {
    local time_ends=$(date +%s)
    local time_diff=$(( $time_ends - $1 ))
    echo "$time_diff"
}

# Returns full path for the environment directory
# If environment's name is not enter then returns
# full path to the directory where live all environments
nv_get_env_full_path() {
    local NV_ENVS_DIR="$NV_HOME/envs"
    if [ "$1" = "" ]; then
        echo $NV_ENVS_DIR
    else
        local env_name=$1
        echo "$NV_ENVS_DIR/$env_name"
    fi
}

# Returns full path for plugins directory
# If plugin's name is not entered then returns
# full path to the directory where live all plugins
nv_get_plugin_full_path() {
    local NV_ENVS_DIR="$NV_PATH/nv-plugins"
    if [ "$1" = "" ]; then
        echo $NV_ENVS_DIR
    else
        echo "$NV_ENVS_DIR/$1"
    fi
}

nv_get_command_full_path() {
    local NV_COMMANDS_DIR="$NV_PATH/nv-commands"
    if [ "$1" = "" ]; then
        echo $NV_COMMANDS_DIR
    else
        echo "$NV_COMMANDS_DIR/$1"
    fi
}

nv_get_cache_full_path() {
    local NV_CACHE_DIR="$NV_HOME/cache"
    if [ "$1" = "" ]; then
        echo $NV_CACHE_DIR
    else
        echo "$NV_CACHE_DIR/$1"
    fi
}

nv_get_first_non_opt_value() {
    local res=""

    for par in "$@"
    do
    case $par in
        # skip --<something>
        --*) ;;
        # first non `--<...>` entry is what we want
        *) res=$par; break;
    esac
    done

    echo "$res"
}

nv_get_system_arch() {
    local x32=$1
    local x64=$2

    local arch=$(uname -m)
    if [ "$arch" = "i686" ]; then
        echo "${x32}"
    else
        echo "${x64}"
    fi
}

nv_get_system() {
    local linux=$1
    local osx=$2
    local uname=$(uname)

    if [ "$uname" = "Linux" ]; then
        echo "$linux"
    elif [ "$uname" = "Darwin" ]; then
        echo "$osx"
    else
        echo "Not supported"
    fi
}

nv_get_env_info_path() {
    local env_name=$1
    local env_full_path=$(nv_get_env_full_path "$env_name")
    echo "$env_full_path/envirius.info"
}

nv_get_target_full_path() {
    local target=$1
    local name=$2
    local ver=$3
    if [ "$name" = "" ] && [ "$ver" = "" ]; then
        local build_path="${target}/"
    elif [ ! "$name" = "" ] && [ "$ver" = "" ]; then
        local build_path="${target}/${name}"
    else
        local build_path="${target}/${name}-${ver}-${target}"
    fi
    echo "$(nv_get_cache_full_path $build_path)"
}

nv_get_build_path() {
    echo "$(nv_get_target_full_path 'build' $@)"
}

nv_get_src_path() {
    echo "$(nv_get_target_full_path 'src' $@)"
}

nv_get_arc_path() {
    echo "$(nv_get_target_full_path 'arc' $@)"
}

nv_is_function() {
    local res=$(type $1 &> /dev/null; echo "$?")
    if [ "$res" = "0" ]; then
        echo "yes"
    else
        echo "no"
    fi
}

# http://stackoverflow.com/a/9911082/716726
nv_is_bash() {
    if [ -n "$BASH_VERSION" ]; then
        echo "yes"
    else
        echo "no"
    fi
}

nv_is_zsh() {
    if [ -n "$ZSH_VERSION" ]; then
        echo "yes"
    else
        echo "no"
    fi
}

# https://github.com/cloudflare/semver_bash/blob/master/semver.sh
nv_semver_get_re() {
    echo '[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
}

nv_semver_get_major() {
    local RE=$(nv_semver_get_re)
    echo $1 | sed -e "s#$RE#\1#"
}

nv_semver_get_minor() {
    local RE=$(nv_semver_get_re)
    echo $1 | sed -e "s#$RE#\2#"
}

nv_semver_get_patch() {
    local RE=$(nv_semver_get_re)
    echo $1 | sed -e "s#$RE#\3#"
}

nv_semver_get_special() {
    local RE=$(nv_semver_get_re)
    echo $1 | sed -e "s#$RE#\4#"
}

nv_echo_err() { echo "$@" 1>&2; }

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