#!/bin/bash

# Error handling
set -e

# For styling/colorizing output
txtbld=$(tput bold)
txtgreen=${txtbld}$(tput setaf 2)
txtred=${txtbld}$(tput setaf 1)
txtwhite=${txtbld}$(tput setaf 7)

# Dependency check
function dependencycheck()
{
    numdependencies="$#"
    dependencies=("$@")
    missingdependencies=0

    for ((i = 0; i < numdependencies; i++)) ; do
        if ! command -v "${dependencies[$i]}" &> /dev/null ; then
            echo "${txtred} ✗ dependency not met: ${dependencies[$i]}${txtwhite}"
            missingdependencies=$((missingdependencies+1))
        else
            echo "${txtgreen}  dependency met: ${dependencies[$i]}${txtwhite}"
        fi
    done

    if [ $missingdependencies -gt 0 ] ; then
        exit 1
    # comment out if used in other scripts
    else
        exit 0
    fi
}

# invoke function comment out if sourced
dependencycheck "$@"

# Barebones version without tput (for being sourced):
# function dependencycheck()
# {
    # numdependencies="$#"
    # dependencies=("$@")
    # missingdependencies=0

    # for ((i = 0; i < numdependencies; i++)) ; do
        # if ! command -v "${dependencies[$i]}" &> /dev/null ; then
            # echo "dependency not met: ${dependencies[$i]}"
            # missingdependencies=$((missingdependencies+1))
        # fi
    # done

    # if [ $missingdependencies -gt 0 ] ; then
        # echo "Please install needed dependencies"
        # exit 1
    # fi
# }
