diff --git a/kreypi/README.md b/kreypi/README.md index 1e7869e..6f4c12f 100644 --- a/kreypi/README.md +++ b/kreypi/README.md @@ -1,80 +1,5 @@ # KREYPI! -Kreyren's way to be more effective writing bash scripts +Kreyren's way to be more effective writing shell/bash scripts ## Usage -To use this BASH API place this codeblock at the top of your bash script: - -```bash -### START OF KREYPI INIT ### -# https://github.com/RXT067/Scripts/tree/kreyren/kreypi - -# Do not make additional functions here since we are going to source a library - -# Check for root -if [ "$(id -u)" != 0 ]; then - printf 'FATAL: %s\n' "This script is using KREYPI library which needs to be exported in /lib/shell using root permission" - exit 3 -elif [ "$(id -u)" = 0 ]; then - # shellcheck disable=SC2154 - [ -n "$debug" ] && printf 'DEBUG: %s\n' "Script executed from an user with ID $(id -u)" -else - printf 'FATAL: %s\n' "Unexpected happend in KREYPI_INIT for checking root" - exit 255 -fi - -# Create a new directory for shell libraries if not present already -if [ ! -e /lib/shell ]; then - mkdir /lib/shell || { printf 'FATAL: %s\n' "Unable to make a new directory in '/lib/shell', is this non-standard file hierarchy?" ; exit 1 ;} -elif [ -f /lib/shell ]; then - printf 'FATAL: %s\n' "File '/lib/shell' is a file which is unexpected, expecting directory to export kreypi library" - exit 1 -elif [ -d /lib/shell ]; then - # shellcheck disable=SC2154 - [ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory '/lib/shell' already exists, no need to make it" -else - printf 'FATAL: %s\n' "Unexpected result in KREYPI_INIT checking for /lib/shell" - exit 255 -fi - -# Fetch the library -if [ -e /lib/shell/kreypi.sh ]; then - # shellcheck disable=SC2154 - [ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, skipping fetch" -elif command -v wget >/dev/null; then - wget https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -O /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using wget" ; exit 1;} -elif command -v curl >/dev/null; then - curl https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh -o /lib/shell/kreypi.sh || { printf 'FATAL: %s\n' "Unable to fetch kreypi.sh from https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh in /lib/shell/kreypi.sh using curl" ; exit 1 ;} -else - printf 'FATAL: %s\n' "Unable to download kreypi library from 'https://raw.githubusercontent.com/RXT067/Scripts/kreyren/kreypi/kreypi.sh' in '/lib/shell/kreypi.sh'" - exit 255 -fi - -# Sanitycheck for /lib/shell -if [ -e /lib/shell ]; then - # shellcheck disable=SC2154 - [ -n "$debug" ] && printf 'DEBUG: %s\n' "Directory in '/lib/shell' already exists, passing sanity check" -elif [ ! -e /lib/shell ]; then - printf 'FATAL: %s\n' "Sanitycheck for /lib/shell failed" - exit 1 -else - printf 'FATAL: %s\n' "Unexpected happend in sanitycheck for /lib/shell" - exit 255 -fi - -# Source KREYPI -if [ -e "/lib/shell/kreypi.sh" ]; then - # 'source' can not be used on POSIX sh - # shellcheck source="/lib/shell/kreypi.sh" - . "/lib/shell/kreypi.sh" || { printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh'" ; exit 1 ;} - # shellcheck disable=SC2154 - [ -n "$debug" ] && printf 'DEBUG: %s\n' "Kreypi in '/lib/shell/kreypi.sh' has been successfully sourced" -elif [ ! -e "/lib/shell/kreypi.sh" ]; then - printf 'FATAL: %s\n' "Unable to source '/lib/shell/kreypi.sh' since path does not exists" - exit 1 -else - printf 'FATAL: %s\n' "Unexpected happend in sourcing KREYPI_INIT" - exit 255 -fi - -### END OF KREYPI INIT ### -``` +FIXME \ No newline at end of file diff --git a/kreypi/functions/checkroot.sh b/kreypi/functions/checkroot.sh new file mode 100644 index 0000000..ecf1836 --- /dev/null +++ b/kreypi/functions/checkroot.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: ' +Check if script has been executed with root permission if not and variale KREPI_CHECKROOT_WORKAROUND_ROOT is set it will try to invoke itself as root' + +# Checkroot - Check if executed as root, if not tries to use sudo if KREYPI_CHECKROOT_USE_SUDO variable is not blank +checkroot() { + die fixme "Checkroot needs more sanitization" + + if [ "$(id -u)" = 0 ]; then + return 0 + elif command -v sudo >/dev/null && [ -n "$KREYPI_CHECKROOT_USE_SUDO" ] && [ -n "$(id -u)" ]; then + info "Failed to aquire root permission, trying reinvoking with 'sudo' prefix" + exec sudo "$0 -c\"$*\"" + elif ! command -v sudo >/dev/null && [ -n "$KREYREN" ] && [ -n "$(id -u)" ]; then + einfo "Failed to aquire root permission, trying reinvoking as root user." + exec su -c "$0 $*" + elif [ "$(id -u)" != 0 ]; then + die 3 + else + die 255 "checkroot" + fi +} \ No newline at end of file diff --git a/kreypi/functions/eexeccheck.sh b/kreypi/functions/eexeccheck.sh new file mode 100644 index 0000000..17a106c --- /dev/null +++ b/kreypi/functions/eexeccheck.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: ' +Wrapper used to check if parsed command is executable on the system + +SYNOPSIS: eexechcheck [command] + +Returns 0 if command is executable or 126 if it is not + +Expected to be piped in die if fatal is expected + + eexeccheck wget | die' + +# Check executable +eexeccheck() { + eexeccheckCommand="$1" + + fixme "Terminator for eexecheck is not configured to output command name in output" + die 1 "I'm not sure how i want to implement this, stubbing for now - Kreyren" + + if ! command -v "$eexeccheckCommand" >/dev/null; then + # Not executable + return 126 + elif command -v "$eexeccheckCommand" >/dev/null; then + # Is executable + return 0 + else + die 255 "check_exec" + fi + + unset eexeccheckCommand +} \ No newline at end of file diff --git a/kreypi/functions/eextract.sh b/kreypi/functions/eextract.sh new file mode 100644 index 0000000..c93e24a --- /dev/null +++ b/kreypi/functions/eextract.sh @@ -0,0 +1,28 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: ' +Wrapper used to extract various archives in target directory + +SYNOPSIS: eextract [archive] [destdir] (expected files)' + +eextract() { + # Capture arguments + while [ $# -ge 1 ]; do case "$1" in + *.tar.gz) file="$1" ; shift 1 ;; + */) targetdir="$1" ; shift 1 ;; + *) die 2 "Unsupported argument parsed in extractor - '$1'" + esac; done + + die fixme "extractor is not finished" + + # Action based on file imported + case "$file" in + *.tar.xz) + if command -v tar >/dev/null; then + tar -Jxpf "$file" -C "$targetdir" || die 1 "Unable to extract archive '$file' to '$targetdir' using tar" + fi + ;; + *) die 255 "Unexpected file parsed in extractor" + esac +} diff --git a/kreypi/functions/efetch.sh b/kreypi/functions/efetch.sh new file mode 100644 index 0000000..e69de29 diff --git a/kreypi/functions/egit.sh b/kreypi/functions/egit.sh new file mode 100644 index 0000000..a289a1c --- /dev/null +++ b/kreypi/functions/egit.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: " +Wrapper used for git command" + +egit() { + argument="$1" + MYFUNCNAME="egit" + + case "$argument" in + clone) + git_url="$3" + destdir="$4" + + # Check if 'git' is executable + if ! command -v "git" >/dev/null; then die 126 "command 'git' is not executable"; fi + + # Sanitization for variable 'git_url' + case $git_url in + https://*.git) true ;; + *) die 1 "$MYFUNCNAME: Argument '$1' doesn't match 'https://*.git'" + esac + + # Sanitization for variable 'destdir' + if [ -d "$destdir" ]; then + true + elif [ ! -d "$destdir" ]; then + case $destdir in + /*) true ;; + # Sanitization to avoid making a git repositories in a current working directory + "") die 2 "$MYFUNCNAME-$argument is not supported to run without a specified directory" ;; + *) die 1 "Variable destdir '$destdir' is not a valid directory for command '$MYFUNCNAME $argument $git_url $destdir'" + esac + else + die 255 "$MYFUNCNAME $argument - destdir" + fi + + fixme "$MYFUNCNAME $argument: Check if directory already cloned git repository" + + # Action + git clone "$git_url" "$destdir" + + git_err_code="$?" + + fixme "Add translate for $MYFUNCNAME $argument" + case $git_err_code in + 0) debug "Command 'git $argument $git_url $destdir' returned true" ;; + 128) info "Command 'git' already cloned '$git_url' in '$destdir'" ;; + *) die 1 "Command 'git clone $git_url $destdir' returned an unknown error code: $git_err_code" + esac + + unset git_url destdir git_err_code + ;; + *) die fixme "Argument $argument is not supported by $MYFUNCNAME" + esac + + unset argument MYFUNCNAME +} \ No newline at end of file diff --git a/kreypi/functions/emkdir.sh b/kreypi/functions/emkdir.sh new file mode 100644 index 0000000..1465771 --- /dev/null +++ b/kreypi/functions/emkdir.sh @@ -0,0 +1,79 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: ' +Wrapper for `mkdir` command + +SYNOPSIS: emkdir pathname/to/directory permission user group + +Currently accepted values for permission are {0000..7777}' + +emkdir() { + # TODO: capture everything that has syntax of path in $1 + + # Naming set this way to avoid conflicts with other variables since we are unseting these at the end + emkdirTargetdir="$1" + emkdirPermission="$2" + emkdirUserperm="$3" + emkdirGroupperm="$4" + + # Path check + if [ ! -d "$emkdirTargetdir" ]; then + debug "Creating a directory in '$emkdirTargetdir'" + mkdir "$emkdirTargetdir" || die 1 "Unable to make a new directory in '$emkdirTargetdir'" + elif [ -d "$emkdirTargetdir" ]; then + debug "Directory '$emkdirTargetdir' already exists, skipping creation" + elif [ -f "$emkdirTargetdir" ]; then + die 1 "Path '$emkdirTargetdir' is a file which is unexpected, skipping creation of directory" + else + die 255 "emkdir - path check" + fi + + # Check permission + case "$emkdirPermission" in + [0-9][0-9][0-9][0-9]) + if [ "$(stat -c "%#a" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirPermission" ]; then + debug "Changing permisson of '$emkdirTargetdir' on '$emkdirPermission'" + chmod "$emkdirPermission" "$emkdirTargetdir" || die 1 "Unable to change permission '$emkdirPermission' for '$emkdirTargetdir'" + elif [ "$(stat -c "%#a" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirPermission" ]; then + debug "Directory '$emkdirTargetdir' already have permission set on '$emkdirPermission'" + else + die 255 "Checking permission for '$emkdirTargetdir'" + fi ;; + *) die 2 "Second argument '$emkdirPermission' does not match syntax '[0-9][0-9][0-9][0-9]'" + esac + + # Check user permission + if [ -n "$emkdirUserperm" ]; then + if [ "$(stat -c "%U" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirUserperm" ]; then + debug "Changing user permission of '$emkdirTargetdir' on '$emkdirUserperm'" + chown "$emkdirUserperm" "$emkdirTargetdir" || die 1 "Unable to change user permission of '$emkdirTargetdir' on '$emkdirUserperm'" + elif [ "$(stat -c "%U" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirUserperm" ]; then + debug "User permission of '$emkdirTargetdir' is already '$emkdirUserperm'" + else + die 255 "emkdir checking for userperm" + fi + elif [ -n "$emkdirUserperm" ]; then + debug "User permission for '$emkdirTargetdir' is not specified, skipping changing" + else + die 255 "emkdir check for userperm variable" + fi + + # Check group permission + if [ -n "$emkdirGroupperm" ]; then + if [ "$(stat -c "%G" "$emkdirTargetdir" 2>/dev/null)" != "$emkdirGroupperm" ]; then + debug "Changing group permission of '$emkdirTargetdir' on '$emkdirGroupperm'" + chgrp "$emkdirGroupperm" "$emkdirTargetdir" || die 1 "Unable to change group permission of '$emkdirTargetdir' on '$emkdirGroupperm'" + elif [ "$(stat -c "%G" "$emkdirTargetdir" 2>/dev/null)" = "$emkdirGroupperm" ]; then + debug "Group permission of '$emkdirTargetdir' is already '$emkdirGroupperm'" + else + die 255 "Checking group permission of '$emkdirTargetdir'" + fi + elif [ -z "$emkdirGroupperm" ]; then + debug "Group permission is not specified for '$emkdirTargetdir', skipping change" + else + die 255 "emkdir checking for groupperm variable" + fi + + unset emkdirTargetdir emkdirPermission emkdirUserperm emkdirGroupperm +} \ No newline at end of file diff --git a/kreypi/kreypi.sh b/kreypi/kreypi.sh index 2a3a4d4..9b68f29 100644 --- a/kreypi/kreypi.sh +++ b/kreypi/kreypi.sh @@ -4,111 +4,8 @@ # Kreypi (Krey's API) for shell -# Output manipulation -info() { printf 'INFO: %s\n' "$1" ;} -warn() { printf 'WARN: %s\n' "$1" 1>&2 ;} - -fixme() { - case "$1" in - posix) printf 'POSIXBLOCK: %s\n' "$2" ;; - *) printf 'FIXME: %s\n' "$1" - esac -} - -# shellcheck disable=SC2154 # Variable 'debug' is set by the end-user -debug() { [ -n "$debug" ] && printf "DEBUG: %s\n" "$1" 1>&2 ;} - -# SYNOPSIS: $0 [error_code [num:0~255]] (message) -## TODO: Add debug msg option -# http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF -die() { - err_code="$1" - message="$2" - MYFUNCNAME=die - - # HELPER: Handle die output - die_output() { - if [ -n "$message" ]; then - case $LANG in - en*) printf 'FATAL: %s\n' "$message" 1>&2 ;; - cz*) printf 'FATALNÍ: %s\n' "$message" 1>&2 ;; - sk*) printf 'ČOBOLO: %s\n' "$message" 1>&2 ;; - # ^^^^^^ - Now we wait for angry čobolaks - *) printf 'FATAL: %s\n' "$message" 1>&2 - esac - elif [ -z "$message" ]; then - die_message - else - die 255 "die, $err_code" - fi - - exit "$err_code" - } - - case "$err_code" in - 0|true) - debug "Function $MYFUNCNAME returned true" - return 0 - ;; - 1|false) # False - die_message() { - case $LANG in - en*) printf 'FATAL: %s\n' "Function $MYFUNCNAME returned false" ;; - *) printf 'FATAL: %s\n' "Function $MYFUNCNAME returned false" - esac - } - - die_output - ;; - 2) # Syntax err - die_message() { - case $LANG in - en*) printf 'FATAL: %s\n' "Function $MYFUNCNAME returned $err_code" ;; - *) printf 'FATAL: %s\n' "Function $MYFUNCNAME returned $err_code" - esac - } - - die_output - ;; - 3) # Permission issue - die_message() { - case $LANG in - en*) printf 'FATAL: %s\n' "Unable to elevate root access from $(id -u)" ;; - *) printf 'FATAL: %s\n' "Unable to elevate root access from $(id -u)" - esac - } - - die_output - if [ -n "$message" ]; then printf 'FATAL: %s\n' "$message" 1>&2 ; exit 3 - elif [ -z "$message" ]; then printf 'FATAL: %s\n' "Unable to elevate root access from $([ -n "$(id -u)" ] && printf "EUID ($(id -u))")\n" 1>&2 ; exit 3 - else die 'wtf' - fi - ;; - 126) # Not executable - die 126 "FIXME(die): Not executable" - ;; - 130) # Killed by user - die 130 "Killed by user" - ;; - # Custom - wtf|255) - die_message() { - case $LANG in - en*) printf 'FATAL: %s\n' "Unexpected result in '$message'" ;; - *) printf 'FATAL: %s\n' "Unexpected result in '$message'" - esac - } - - die_output - ;; - ping) printf 'FATAL: %s\n' "Killed by ping\n" ; exit 1 ;; - # In case `die message` was used - *) printf 'FATAL: %s\n' "%s\n" "$err_code" 1>&2 ; exit 1 - esac - - unset err_code message MYFUNCNAME -} +# FIXME: Prepared for deprecation # Wrapper for git egit() { diff --git a/kreypi/output/edebug.sh b/kreypi/output/edebug.sh new file mode 100644 index 0000000..e58349e --- /dev/null +++ b/kreypi/output/edebug.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +:' +Command used to output verbose messages for development that are considered as too many informations for the end-user + +SYNOPSIS: debug [message] + +Example: + + if command -v wget >/dev/null; then + debug "Executing command curl to download some_url in some_path" + wget some_url -O some_path + fi +' + +# shellcheck disable=SC2154 # Variable 'DEBUG' is expected to be set by the end-user -> No need to check for unused +edebug() { [ -n "$DEBUG" ] && printf "DEBUG: %s\n" "$1" 1>&2 ;} \ No newline at end of file diff --git a/kreypi/output/efixme.sh b/kreypi/output/efixme.sh new file mode 100644 index 0000000..20702c4 --- /dev/null +++ b/kreypi/output/efixme.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +:' +Command used to output a FIXME message for features that are not yet implmented + +SYNOPSIS: fixme [message] + +Example: + + if command -v wget >/dev/null; then + fixme "only wget is currently supported, add more options for download" + wget someurl -O somepath + elif command -v curl >/dev/null; then + die fixme "curl is not implemented" + else + die 255 "example of wget to fixme intro" + fi + +Messages are enabled by default + +Can be disabled by seting variable IGNORE_FIXME on non-zero' + +fixme() { + [ -z "$IGNORE_FIXME" ] && printf 'FIXME: %s\n' "$1" +} \ No newline at end of file diff --git a/kreypi/output/einfo.sh b/kreypi/output/einfo.sh new file mode 100644 index 0000000..d749df2 --- /dev/null +++ b/kreypi/output/einfo.sh @@ -0,0 +1,16 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +:' +Command used to output relevant information for end-user + +SYNOPSIS: einfo [message] + +Example: + + einfo "Downloading some_url in some_path" + wget some_url -O some_path + +' + +einfo() { printf 'INFO: %s\n' "$1" ;} \ No newline at end of file diff --git a/kreypi/output/ewarn.sh b/kreypi/output/ewarn.sh new file mode 100644 index 0000000..56d8d91 --- /dev/null +++ b/kreypi/output/ewarn.sh @@ -0,0 +1,29 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: " +Command used to output a warning message to the end user + +SYNOPSIS: warn [message] + +Example: + + if command -v curl >/dev/null; then ewarn \"Command 'curl' is not executable\"; fi + +Message prefix is set by default on: + + WARN: message + +This can be overwritten by seting EWARN_PREFIX variable on expected prefix +" + +# This is used for customization by the end-user in case different prefix is wanted. +if [ -z "$EWARN_PREFIX" ]; then + EWARN_PREFIX="WARN:" +elif [ -n "$EWARN_PREFIX" ]; then + true +else + die 255 "ewarn - EWARN_PREFIX" +fi + +ewarn() { printf "$EWARN_PREFIX %s\n' "$1" 1>&2 ;} \ No newline at end of file diff --git a/kreypi/terminators/die.sh b/kreypi/terminators/die.sh new file mode 100644 index 0000000..9aa950c --- /dev/null +++ b/kreypi/terminators/die.sh @@ -0,0 +1,148 @@ +#!/bin/sh +# Created by Jacob Hrbek in 2019 under the terms of GPL-3 (https://www.gnu.org/licenses/gpl-3.0.en.html) + +: ' +function used to terminate the program if provided error code is non-zero and output a custom message with options for translate + +SYNOPSIS: die [error_code] (message) + +This command is made to be compatible with POSIX sh and bash + +ERROR CODES: + 0 - General true + 1 - General false + 2 - Syntax error + 3 - Permission issue + 126 - Not executable + 130 - Killed by the end-user + 255 - Unexpected + ping - output ping (used for development) + fixme - Used to output fatal error about unimplemented feature + +Example: + + if ! command -v wget >/dev/null; then die 126 "Command wget is not executable"; fi + +References: + - Exit codes - http://tldp.org/LDP/abs/html/exitcodes.html#EXITCODESREF +' + +if [ -z "$DIE_PREFIX" ]; then + case $LANG in + en*) DIE_PREFIX="FATAL:" ;; + cz*) DIE_PREFIX="FATALNÍ:" ;; + sk*) DIE_PREFIX="ČOBOLO:" ;; + *) DIE_PREFIX="FATAL:" + esac +elif [ -z "$DIE_PREFIX" ]; then + true # DIE_PREFIX set by the end-user +else + # Do not use die here since it is not sourced yet + printf 'FATAL: %s\n' "Unexpected happend in DIE_PREFIX for die.sh" + exit 255 +fi + +die() { + # Capture arguments + err_code="$1" + message="$2" + + # POSIX compatibility for FUNCNAME + if [ -z "$FUNCNAME" ]; then + MYFUNCNAME="die" # INFO: This has to be changed in case function name changes! + elif [ -n "$FUNCNAME" ]; then + MYFUNCNAME="${FUNCNAME[0]}" + else + # Do not use die here since it is not sourced yet + printf 'FATAL: %s\n' "Unexpected happend in die() - FUNCNAME" + exit 255 + fi + + # HELPER: Handle the output of the command for those that are outputing only `FATAL: message` + die_output() { + if [ -n "$message" ]; then + printf "$DIE_PREFIX %s\\n" "$messge" 1>&2 + elif [ -z "$message" ]; then + die_message + else + die 255 "$MYFUNCNAME, $err_code" + fi + + exit "$err_code" + } + + case "$err_code" in + 0|true) + edebug "Function $MYFUNCNAME returned true" + return 0 # Do not terminate if error code '0' is used + ;; + 1|false) # False + die_message() { + case $LANG in + en*) printf "$DIE_PREFIX %s\\n" "Function $MYFUNCNAME returned false" ;; + *) printf "$DIE_PREFIX %s\\n" "Function $MYFUNCNAME returned false" + esac + } + + die_output + ;; + 2) # Syntax err + die_message() { + case $LANG in + en*|*) printf "$DIE_PREFIX %s\\n" "Function $MYFUNCNAME returned $err_code" + esac + } + + die_output + ;; + 3) # Permission issue + die_message() { + case $LANG in + en*|*) printf "$DIE_PREFIX %s\\n" "Unable to elevate root access $([ -n "$(id -u)" ] && printf '%s\n' "from UID '$(id -u)'")" + esac + } + + die_output + ;; + 126) # Not executable + die_message() { + case $LANG in + en*) printf "$DIE_PREFIX %s\\n" "Error code $err_code has been parsed in $MYFUNCNAME which is used for not executable" + esac + } + + die_output + ;; + 130) # Killed by user + die_message() { + case $LANG in + en*) printf "$DIE_PREFIX %s\\n" "Error code $err_code has been parsed in $MYFUNCNAME indicating that command was killed by user" + esac + } + + die_output + ;; + 255) # Unexpected + die_message() { + case $LANG in + en*) printf "$DIE_PREFIX %s\\n" "Unexpected result in '$message'" ;; + *) printf "$DIE_PREFIX %s\\n" "Unexpected result in '$message'" + esac + } + + die_output + ;; + ping) # Ping used for development + printf "$DIE_PREFIX %s\\n" "! ! ! P I N G ! ! !" + exit 1 + ;; + *) # In case wrong syntax was used + case $LANG in + en*) printf "$DIE_PREFIX %s\\n" "Wrong argument '$err_code' has been parsed in die()" + esac + + exit 2 + esac + + unset err_code message MYFUNCNAME +} \ No newline at end of file