#! /usr/bin/env bash

# Defines general utility functions.

# Label: Caffeinate Machine
# Description: Keep machine running for a very long time.
caffeinate_machine() {
  if [[ -n "$(pgrep -x caffeinate)" ]]; then
    printf "Machine is already caffeinated!\n"
  else
    caffeinate -s -u -d -i -t 3153600000 > /dev/null &
    printf "Machine caffeinated.\n"
  fi
}
export -f caffeinate_machine

# Label: Clean Work Path
# Description: Clean work path of artifacts.
clean_work_path() {
  rm -rf "$MAC_OS_WORK_PATH"
}
export -f clean_work_path

# Label: Get Basename
# Description: Answer file or directory basename.
# Parameters: $1 (required): Path.
get_basename() {
  printf "%s" "${1##*/}"
}
export -f get_basename

# Label: Get Homebrew Root
# Description: Answer Homebrew root path.
get_homebrew_root() {
  if [[ "$(/usr/bin/arch)" == "arm64" ]]; then
    printf "%s" "/opt/homebrew"
  else
    printf "%s" "/usr/local/Homebrew"
  fi
}
export -f get_homebrew_root

# Label: Get Homebrew Bin Root
# Description: Answer Homebrew binary root path.
get_homebrew_bin_root() {
  if [[ "$(/usr/bin/arch)" == "arm64" ]]; then
    printf "%s" "/opt/homebrew/bin"
  else
    printf "%s" "/usr/local/bin"
  fi
}
export -f get_homebrew_bin_root

# Label: Check Mac App Store Install
# Description: Check Mac App Store (mas) CLI has been installed.
check_mas_install() {
  if ! command -v mas > /dev/null; then
    printf "%s\n" "ERROR: Mac App Store (mas) CLI can't be found."
    printf "%s\n" "       Please ensure mas (i.e. brew install mas) is installed."
    exit 1
  fi
}
export -f check_mas_install
