#!/bin/sh -e

if [ -n "${DEBUG}" ]; then
  set -x
fi

uname_os() {
  os=$(uname -s | tr '[:upper:]' '[:lower:]')
  echo "${os}"
}
OS=$(uname_os)

echo_stderr() {
  # send message to stderr. exclude >&2 if you dont want it that way.
  printf '%s\n' "$1" >&2  
}

fail() {
  echo_stderr "$1"
  # return a code specified by $2 or 1 by default.
  exit "${2-1}"
}

info() { printf '%s\n' "$*" >&2; }

has() {
  command -v "$1" >/dev/null 2>&1
}

check_dependency() {
  if ! (builtin command -V "$1" >/dev/null 2>&1); then
    fail "Missing dependency: can't find $1 in your PATH"
  fi
}

only_run_on() {
  if [ "${OS}" != "darwin" ]; then
    fail "This script only runs on $1, this machine is running ${OS}"
  else
    info "${OS} is valid..."
  fi
}

only_run_on darwin
check_dependency osascript

