#!/bin/bash -e

echo_error() {
    local RED=`tput setaf 1`
    local RESET=`tput sgr0`
    echo "${RED}ERROR: ${1}${RESET}"
}

REALPATH=
REALPATHS=(
    'realpath'
    'grealpath'
    'greadlink -f'
)
for cmd in "${REALPATHS[@]}"; do
if command -v "${cmd%% *}" &>/dev/null; then
    REALPATH="${cmd}"
    break
fi
done

if [ -z "${REALPATH}" ]; then
    echo_error 'Unable to find suitable command for realpath.'
    if [ $(uname) == 'Darwin' ]; then
        echo 'GNU coreutils are required to build openBalena on macOS. To install with brew, run'
        echo ''
        echo '    brew install coreutils'
        echo ''
    fi
    exit 1
fi

realpath() {
  echo $(command ${REALPATH} "$@")
}