132 lines · 2.9 KB
#!/usr/bin/env bash

# Bash 4.0+ only. Faster than using tr or awk + no fork.
toLower() {
    if [[ $# -eq 0 ]]; then
        while IFS= read -r line; do
            echo "${line,,}"
        done
    else
        local str="$*"
        echo "${str,,}"
    fi
}

# Return a string corresponding to the current OS.
which_os() {
    if [[ -f /etc/os-release ]]; then
        # shellcheck disable=SC1091
        source /etc/os-release
        local name
        # shellcheck disable=SC2153
        name=$(echo "$NAME" | tr '[:upper:]' '[:lower:]')
        case $name in
        fedora*)
            printf "fedora"
            return
            ;;
        esac
    fi

    case "$OSTYPE" in
    darwin*)
        printf "darwin"
        return
        ;;

    linux*)
        if [[ -f "/etc/issue" ]]; then
            issue=$(cat /etc/issue)
            if [[ $issue =~ "NixOS" ]]; then