<commit_before>#!/usr/bin/env bash

koopa::check_azure() { # {{{1
    # """
    # Check Azure VM integrity.
    # @note Updated 2020-07-04.
    # """
    local mount
    koopa::assert_has_no_args "$#"
    koopa::is_azure || return 0
    mount='/mnt/resource'
    if [[ -e "$mount" ]]
    then
        koopa::check_user "$mount" 'root'
        koopa::check_group "$mount" 'root'
        koopa::check_access_octal "$mount" '1777'
    fi
    koopa::check_mount '/mnt/rdrive'
    return 0
}

koopa::check_access_human() { # {{{1
    # """
    # Check if file or directory has expected human readable access.
    # @note Updated 2020-07-04.
    # """
    local access code file
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    access="$(koopa::stat_access_human "$file")"
    if [[ "$access" != "$code" ]]
    then
        koopa::warning "'${file}' current access '${access}' is not '${code}'."
    fi
    return 0
}

koopa::check_access_octal() { # {{{1
    # """
    # Check if file or directory has expected octal access.
    # @note Updated 2020-07-04.
    # """
    local access code file
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    access="$(koopa::stat_access_octal "$file")"
    if [[ "$access" != "$code" ]]
    then
        koopa::warning "'${file}' current access '${access}' is not '${code}'."
    fi
    return 0
}

koopa::check_data_disk() { # {{{1
    # """
    # Check data disk configuration.
    # @note Updated 2020-11-19.
    # """
    local data_disk_link_prefix opt_prefix
    koopa::assert_has_no_args "$#"
    koopa::is_linux || return 0
    # e.g. '/n'.
    data_disk_link_prefix="$(koopa::data_disk_link_prefix)"
    if [[ -L "$data_disk_link_prefix" ]] && [[ ! -e "$data_disk_link_prefix" ]]
    then
        koopa::warning "Invalid symlink: '${data_disk_link_prefix}'."
    fi
    # e.g. '/opt/koopa/opt'; or previously '/usr/local/koopa/opt'.
    opt_prefix="$(koopa::opt_prefix)"
    if [[ -L "$opt_prefix" ]] && [[ ! -e "$opt_prefix" ]]
    then
        koopa::warning "Invalid symlink: '${opt_prefix}'."
    fi
    return 0
}

# shellcheck disable=SC2120
koopa::check_disk() { # {{{1
    # """
    # Check that disk has enough free space.
    # @note Updated 2020-06-30.
    # """
    local limit used
    limit=90
    used="$(koopa::disk_pct_used "$@")"
    if [[ "$used" -gt "$limit" ]]
    then
        koopa::warning "Disk usage is ${used}%."
    fi
    return 0
}

koopa::check_exports() { # {{{1
    # """
    # Check exported environment variables.
    # @note Updated 2020-07-05.
    #
    # Warn the user if they are setting unrecommended values.
    # """
    koopa::assert_has_no_args "$#"
    koopa::is_rstudio && return 0
    local vars
    vars=(
        'JAVA_HOME'
        'LD_LIBRARY_PATH'
        'PYTHONHOME'
        'R_HOME'
    )
    koopa::warn_if_export "${vars[@]}"
    return 0
}

koopa::check_group() { # {{{1
    # """
    # Check if file or directory has an expected group.
    # @note Updated 2020-07-04.
    # """
    local code file group
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    group="$(koopa::stat_group "$file")"
    if [[ "$group" != "$code" ]]
    then
        koopa::warning "'${file}' current group '${group}' is not '${code}'."
        return 1
    fi
    return 0
}

koopa::check_mount() { # {{{1
    # """
    # Check if a drive is mounted.
    # Usage of find is recommended over ls here.
    # @note Updated 2020-07-04.
    # """
    local mnt
    koopa::assert_has_args "$#"
    koopa::assert_is_installed find
    mnt="${1:?}"
    if [[ "$(find "$mnt" -mindepth 1 -maxdepth 1 | wc -l)" -eq 0 ]]
    then
        koopa::warning "'${mnt}' is unmounted."
        return 1
    fi
    return 0
}

# shellcheck disable=SC2119
koopa::check_system() { # {{{1
    # """
    # Check system.
    # @note Updated 2020-01-04.
    # """
    koopa::assert_has_no_args "$#"
    koopa::rscript_vanilla 'checkSystem'
    koopa::check_exports
    koopa::check_disk
    koopa::check_data_disk
    return 0
}

koopa::check_user() { # {{{1
    # """
    # Check if file or directory is owned by an expected user.
    # @note Updated 2020-07-04.
    # """
    local current_user expected_user file
    koopa::assert_has_args "$#"
    file="${1:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist on disk."
        return 1
    fi
    file="$(realpath "$file")"<commit_msg>Improve check system error message<commit_after>#!/usr/bin/env bash

koopa::check_azure() { # {{{1
    # """
    # Check Azure VM integrity.
    # @note Updated 2020-07-04.
    # """
    local mount
    koopa::assert_has_no_args "$#"
    koopa::is_azure || return 0
    mount='/mnt/resource'
    if [[ -e "$mount" ]]
    then
        koopa::check_user "$mount" 'root'
        koopa::check_group "$mount" 'root'
        koopa::check_access_octal "$mount" '1777'
    fi
    koopa::check_mount '/mnt/rdrive'
    return 0
}

koopa::check_access_human() { # {{{1
    # """
    # Check if file or directory has expected human readable access.
    # @note Updated 2020-07-04.
    # """
    local access code file
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    access="$(koopa::stat_access_human "$file")"
    if [[ "$access" != "$code" ]]
    then
        koopa::warning "'${file}' current access '${access}' is not '${code}'."
    fi
    return 0
}

koopa::check_access_octal() { # {{{1
    # """
    # Check if file or directory has expected octal access.
    # @note Updated 2020-07-04.
    # """
    local access code file
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    access="$(koopa::stat_access_octal "$file")"
    if [[ "$access" != "$code" ]]
    then
        koopa::warning "'${file}' current access '${access}' is not '${code}'."
    fi
    return 0
}

koopa::check_data_disk() { # {{{1
    # """
    # Check data disk configuration.
    # @note Updated 2020-11-19.
    # """
    local data_disk_link_prefix opt_prefix
    koopa::assert_has_no_args "$#"
    koopa::is_linux || return 0
    # e.g. '/n'.
    data_disk_link_prefix="$(koopa::data_disk_link_prefix)"
    if [[ -L "$data_disk_link_prefix" ]] && [[ ! -e "$data_disk_link_prefix" ]]
    then
        koopa::warning "Invalid symlink: '${data_disk_link_prefix}'."
    fi
    # e.g. '/opt/koopa/opt'; or previously '/usr/local/koopa/opt'.
    opt_prefix="$(koopa::opt_prefix)"
    if [[ -L "$opt_prefix" ]] && [[ ! -e "$opt_prefix" ]]
    then
        koopa::warning "Invalid symlink: '${opt_prefix}'."
    fi
    return 0
}

# shellcheck disable=SC2120
koopa::check_disk() { # {{{1
    # """
    # Check that disk has enough free space.
    # @note Updated 2020-06-30.
    # """
    local limit used
    limit=90
    used="$(koopa::disk_pct_used "$@")"
    if [[ "$used" -gt "$limit" ]]
    then
        koopa::warning "Disk usage is ${used}%."
    fi
    return 0
}

koopa::check_exports() { # {{{1
    # """
    # Check exported environment variables.
    # @note Updated 2020-07-05.
    #
    # Warn the user if they are setting unrecommended values.
    # """
    koopa::assert_has_no_args "$#"
    koopa::is_rstudio && return 0
    local vars
    vars=(
        'JAVA_HOME'
        'LD_LIBRARY_PATH'
        'PYTHONHOME'
        'R_HOME'
    )
    koopa::warn_if_export "${vars[@]}"
    return 0
}

koopa::check_group() { # {{{1
    # """
    # Check if file or directory has an expected group.
    # @note Updated 2020-07-04.
    # """
    local code file group
    koopa::assert_has_args "$#"
    file="${1:?}"
    code="${2:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist."
        return 1
    fi
    group="$(koopa::stat_group "$file")"
    if [[ "$group" != "$code" ]]
    then
        koopa::warning "'${file}' current group '${group}' is not '${code}'."
        return 1
    fi
    return 0
}

koopa::check_mount() { # {{{1
    # """
    # Check if a drive is mounted.
    # Usage of find is recommended over ls here.
    # @note Updated 2020-07-04.
    # """
    local mnt
    koopa::assert_has_args "$#"
    koopa::assert_is_installed find
    mnt="${1:?}"
    if [[ "$(find "$mnt" -mindepth 1 -maxdepth 1 | wc -l)" -eq 0 ]]
    then
        koopa::warning "'${mnt}' is unmounted."
        return 1
    fi
    return 0
}

# shellcheck disable=SC2119
koopa::check_system() { # {{{1
    # """
    # Check system.
    # @note Updated 2021-02-15.
    # """
    koopa::assert_has_no_args "$#"
    koopa::assert_is_installed R
    if ! koopa::is_r_package_installed koopa
    then
        koopa::stop \
            'koopa R package is not installed.' \
            "Run 'koopa install r-koopa' to resolve."
    fi
    koopa::rscript_vanilla 'checkSystem'
    koopa::check_exports
    koopa::check_disk
    koopa::check_data_disk
    return 0
}

koopa::check_user() { # {{{1
    # """
    # Check if file or directory is owned by an expected user.
    # @note Updated 2020-07-04.
    # """
    local current_user expected_user file
    koopa::assert_has_args "$#"
    file="${1:?}"
    if [[ ! -e "$file" ]]
    then
        koopa::warning "'${file}' does not exist on disk."
        return 1
    fi
    file="$(realpath "$file")"
{'fraction_non_alphanumeric': 0.17401083733769554, 'fraction_numerical': 0.027195583273693896, 'mean_word_length': 2.4166957736639887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2360654', 'n_tokens_mistral': 3750, 'n_tokens_neox': 3429, 'n_words': 831}