#!/bin/sh
#
# os-detect - detects operating system and outputs it in machine parsable format

# detects the operating system
if [ -r /proc/version ] && grep -iq Microsoft /proc/version; then
  os="windows_wsl"
elif [ -n "${OSTYPE-}" ]; then
  case "${OSTYPE-}" in
    solaris*) os="solaris" ;;
    darwin*) os="osx" ;;
    linux*) os="linux" ;;
    bsd*) os="bsd" ;;
    msys*) os="windows_msys" ;;
    win32*) os="windows_win32" ;;
    cygwin*) os="windows_cygwin" ;;
    *) os="unknown_${OSTYPE}" ;;
  esac
else
  os=$(uname -s 2>/dev/null | tr '[:upper:]' '[:lower:]')
  case "${os}" in
    msys*) os="windows" ;;
    mingw*) os="windows" ;;
    cygwin*) os="windows_cygwin" ;;
    win*) os="windows" ;;
    linux*) os="linux" ;;
    darwin*) os="mac" ;;
    *) os="unknown_${os}" ;;
  esac
fi

# attempt to detect linux distro
if [ "${os}" = "linux" ]; then
  # get the id from the standard "/etc/os-release" location
  if [ -r /etc/os-release ]; then
    distro="$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')"
  else
    # old distro versions may not follow the "os-release"
    # standard and could have info in other files, such as:
    # /etc/(lsb|system|redhat|centos)-release
    # IDs found here: https://github.com/chef/os_release
    system_info="$(cat /etc/[A-Za-z]*[_-][rv]e[lr]* | tr -d '"')"
    case "${system_info}" in
      *"NAME=Ubuntu"*) distro="ubuntu" ;;
      *"NAME=Pop!_OS"*) distro="pop" ;;
      *"NAME=Linux Mint"*) distro="mint" ;;
      *"NAME=Zorin"*) distro="zorin" ;;
      *"NAME=elementary OS"*) distro="elementary" ;;
      *"NAME=Debian"*) distro="debian" ;;
      *"NAME=Cumulus"*) distro="cumulus" ;;
      *"NAME=Raspbian"*) distro="raspbian" ;;
      *"NAME=Kali"*) distro="kali" ;;
      *"NAME=Xbian"*) distro="xbian" ;;
      *"NAME=Gentoo"*) distro="gentoo" ;;
      *"NAME=Arch Linux"*) distro="arch" ;;
      *"NAME=Manjaro"*) distro="manjaro" ;;
      *"NAME=NixOS"*) distro="nixos" ;;
      *"NAME=CentOS"*) distro="centos" ;;
      *"NAME=ClearOS"*) distro="clearos" ;;
      *"NAME=Fedora"*) distro="fedora" ;;
      *"NAME=Mageia"*) distro="mageia" ;;
      *"NAME=Rocky"*) distro="rocky" ;;
      *"NAME=Red Hat Enterprise Linux"*) distro="rhel" ;;
      *"NAME=openSUSE"*) distro="opensuse" ;;
      *"NAME=SLES"*) distro="sles" ;;
      *"NAME=SLED"*) distro="sled" ;;
      *"NAME=CASP"*) distro="casp" ;;
      *"NAME=XCP-ng"*) distro="XCP-ng" ;;
      *"NAME=XenServer"*) distro="xenenterprise" ;;
      *"NAME=Alma"*) distro="alma" ;;
      *"NAME=Alpine"*) distro="alpine" ;;
      *"NAME=Antergos"*) distro="antergos" ;;
      *"NAME=Slackware"*) distro="slackware" ;;
      *) distro="unknown" ;;
    esac
  fi

  # append debian/ubuntu to their derivatives for pattern matching
  # so you can do '*ubuntu' which will match derivatives too
  case "${distro}" in
    ubuntu | raspbian | kali | xbian) os="linux_${distro}__debian" ;;
    pop | xmint | zorin | elementary) os="linux_${distro}__ubuntu" ;;
    *) os="linux_${distro}" ;;
  esac
fi

# sun solaris and derived os reports to be the ancient sunos via uname
if [ "${os}" = "sunos" ]; then
  # current illumos versions have -o to check if illumos x solaris
  # shellcheck disable=SC2312
  if [ "$(uname -o)" = "illumos" ]; then
    os="illumos"
  else
    os="solaris"
  fi
fi

echo "${os}"
