#!/bin/bash

_PLATFORM=`uname`
_MISSING=''

function check_installed {
    local _name=$1
    local _command=$2
    local _brew_name=$3
    local _apt_name=$4

    command -v $_command > /dev/null && return 0

    local _message="$_name is not installed"
    if [ $_PLATFORM == "Darwin" ]; then
        _message="$_message, try: 'brew install $_brew_name'"
        _MISSING="$_MISSING $_brew_name"
    else
        _message="$_message, try: 'sudo apt-get install -y $_apt_name'"
        _MISSING="$_MISSING $_apt_name"
    fi
    echo $_message
}

#               Name        Command         Brew package name       apt package name
check_installed gfortran    gfortran        gcc                     gfortran
check_installed freetype    freetype-config freetype                libfreetype6-dev
check_installed pkg-config  pkg-config      pkg-config              pkg-config
check_installed hdf5        h5cc            homebrew/science/hdf5   libhdf5-dev

if [ -n "$_MISSING" ]; then
    echo
    echo "You don't have all prerequisites installed, can't build"
    echo "You can install them all at once by running this command:"
    echo
    if [ $_PLATFORM == "Darwin" ]; then
        echo "brew install$_MISSING"
    else
        echo "sudo apt-get install -y$_MISSING"
    fi
    echo
    exit 1
else
    echo "All prerequisites met"
fi
