#!/usr/bin/env bash
# 
# Import the dev0s library.
# Usage:
#   import from local.
#     $ bash /usr/local/lib/dev0s/lib/bash/import
#   import remotely.
#     $ curl -s https://raw.githubusercontent.com/vandenberghinc/dev0s/master/dev0s/lib/bash/import | bash 
#   import from .zshrc / .bashrc with an environment.
#     $ bash /usr/local/lib/dev0s/lib/bash/import --env
#

# utils.
function error() {
    # usage: error "Define parameter ..." $@ ; return
    # don't forget to pass the $@ argument to detect --error-exit etc.
    #
    error="$1"
    if [[ "$OS" != "macos" ]] ; then
        error="$(tr '[:lower:]' '[:upper:]' <<< ${error:0:1})${error:1}"
    fi
    if [[ ${error:0:7} == "Error: " ]] ; then
        error=${error:7}
    fi
    if [[ "$OS" != "macos" ]] ; then
        error="$(tr '[:lower:]' '[:upper:]' <<< ${error:0:1})${error:1}"
    fi
    if [[ "$(argument-present --error-exit $@)" == "true" ]] ; then
        echo "Error: $error"
        exit 1
    else
        echo "Error: $error"
        return 
    fi
}
function replace-str() {
    string=$1
    if [[ "$string" == "" ]] ; then
        error "Error: Specify the string (#1) parameter: $ replace-str 'hello wold' 'worl' 'world' " $@ ; return
    fi
    from=$2
    if [[ "$from" == "" ]] ; then
        error "Error: Specify the from (#2) parameter: $ replace-str 'hello wold' 'worl' 'world' " $@ ; return
    fi
    to=$3
    if [[ "$to" == "" ]] && [[ $(argument-present --except $@) == "false" ]] ; then
        error "Error: Specify the to (#3) parameter: $ replace-str 'hello wold' 'worl' 'world' " $@ ; return
    fi
    new=${string/$from/$to}
    echo $new
}
function clean-path() {
    program="clean-path"
    localpath=$1
    if [[ "$localpath" == "" ]] || [[ "$localpath" == "none" ]] ; then
        error "Error: specify the path (#1) parameter: $ $program /some//path/" $@ ; return
    fi
    localpath=${localpath/\/\///} ; localpath=${localpath/\/\///} ; localpath=${localpath/\/\///} ; localpath=${localpath/\/\///} ; localpath=${localpath/\/\///}
    while true; do 
        if [[ "$localpath" == *"//"* ]] ; then
            localpath=$(replace-str "$localpath" "//" "/" )
        else
            break
        fi
    done
    localpath=$(replace-str "$localpath" "//" "/" )
    echo $localpath
}
function argument-present() {
    c=0
    #default=$(get-argument --default $@)
    success=$default
    for var in "$@"
    do
        if (( c > 0 )) ; then
            if [[ "$var" == "$1" ]] ; then
                success="true"
                break
            fi
        fi
        ((c=c+1))
    done
    echo $success
}
function get-argument() {
    c=0
    success="false"
    value="none"
    for var in "$@"
    do
        if (( c > 0 )) ; then
            if [[ "$var" == "$1" ]] ; then
                success="true"
            elif [[ "$success" == "true" ]] ; then
                value=$var
                break
            fi
        fi
        ((c=c+1))
    done
    echo $value
}

# error exit.
error_exit="true"
if [[ "$(argument-present --no-error-exit $@)" == "true" ]] ; then
    error_exit="false"
fi

# library variable.
lib=$(get-argument --lib $@)
if [[ "$lib" == "none" ]] || [[ "$lib" == "" ]] ; then

    # default lib.
    lib="/usr/local/lib/dev0s/lib/"

    # install.
    if [[ ! -d "$lib" ]] ; then
        echo "Installing library dev0s."
        curl -s https://raw.githubusercontent.com/vandenberghinc/dev0s/master/requirements/installer.remote | bash 
        if [[ ! -d "/usr/local/lib/dev0s" ]] ; then
            echo "Error: failed to install library dev0s."
            if [[ "$error_exit" == "true" ]] ; then exit 1 ; else return ; fi
        fi
    fi
else
    lib=$(clean-path $lib)
fi
#echo "dev0s library: $lib"

# import utils.
export DEVOS_LIB=$lib
if [[ "$OSTYPE" == "darwin"* ]] ; then
    source $lib/bash/utils --lib $DEVOS_LIB
else
    . $lib/bash/utils --lib $DEVOS_LIB
fi

# imports lib.
import-bash $lib/bash/pull_push --lib $DEVOS_LIB
import-bash $lib/bash/packages --lib $DEVOS_LIB

# test imports.
if ! command -v "argument-present" &> /dev/null ; then
    echo "Error: failed to import library dev0s."
    if [[ "$error_exit" == "true" ]] ; then exit 1 ; else return ; fi
else
    if [[ "$DEVOS_IMPORTED" != "true" ]] && [[ "$(argument-present --silent)" == "false" ]] ; then
        echo "Successfully imported library dev0s."
    fi
fi

# success.
export DEVOS_IMPORTED="true"

#