1#!/usr/bin/env bash
2# Derived from: https://gist.github.com/marcusandre/4b88c2428220ea255b83
3get_os() {
4  local ostype
5  ostype=$(<<< "$OSTYPE" tr '[:upper:]' '[:lower:]')
6  if [ -z "$ostype" ]; then
7    ostype=$(uname | tr '[:upper:]' '[:lower:]')
8  fi
9
10  case $ostype in
11    freebsd*) echo "freebsd" ;;
12    netbsd*) echo "netbsd" ;;
13    openbsd*) echo "openbsd" ;;
14    darwin*) echo "macos" ;;
15    linux*) echo "linux" ;;
16    cygwin*) echo "cygwin" ;;
17    msys*) echo "msys" ;;
18    mingw*) echo "win" ;;
19    *) echo "unknown"; exit 1 ;;
20  esac
21}
22
23get_linux_dist() {
24  if [[ -f /etc/os-release ]]; then
25    sh -c '. /etc/os-release && echo $ID'
26  elif type lsb_release >/dev/null 2>&1; then
27    lsb_release -si | tr '[:upper:]' '[:lower:]'
28  fi
29}
30
31# If target does not exist, create symlink from source to target.
32ensure_symlink_to_target() {
33  local from="${1:?Missing source}"
34  local to="${2:?Missing target}"
35
36  if [[ -e "${from}" && ! -e "${to}" ]]; then
37    if ! sudo ln -s "${from}" "${to}"
38    then
39      >&2 echo "Error: ${to} still not available after symlink.  Aborting."
40      exit 1
41    fi
42  fi
43}
44