2023-09-16 08:07:33 +02:00
|
|
|
|
#!/bin/sh
|
|
|
|
|
#
|
|
|
|
|
# Purpose {{{
|
|
|
|
|
# This script will try to start eza
|
|
|
|
|
# 1. installed on the system if available
|
|
|
|
|
# 2. from a symlink in user's PATH
|
|
|
|
|
#
|
|
|
|
|
# 2023-09-16
|
|
|
|
|
# }}}
|
|
|
|
|
# Flags {{{
|
|
|
|
|
## Exit on error {{{
|
|
|
|
|
set -o errexit
|
|
|
|
|
## }}}
|
|
|
|
|
## Exit on unset var {{{
|
|
|
|
|
### Use "${VARNAME-}" to test a var that may not have been set
|
|
|
|
|
set -o nounset
|
|
|
|
|
## }}}
|
|
|
|
|
## Pipeline command is treated as failed {{{
|
|
|
|
|
### Not available in POSIX sh − https://github.com/koalaman/shellcheck/wiki/SC3040
|
|
|
|
|
#set -o pipefail
|
|
|
|
|
## }}}
|
|
|
|
|
## Help with debugging {{{
|
|
|
|
|
### Call the script by prefixing it with "TRACE=1 ./script.sh"
|
|
|
|
|
if [ "${TRACE-0}" -eq 1 ]; then set -o xtrace; fi
|
|
|
|
|
## }}}
|
|
|
|
|
# }}}
|
|
|
|
|
# Vars {{{
|
|
|
|
|
PROGNAME=$(basename "${0}"); readonly PROGNAME
|
|
|
|
|
PROGDIR=$(readlink --canonicalize-missing $(dirname "${0}")); readonly PROGDIR
|
|
|
|
|
ARGS="${*}"; readonly ARGS
|
|
|
|
|
readonly NBARGS="${#}"
|
|
|
|
|
[ -z "${DEBUG-}" ] && DEBUG=1
|
|
|
|
|
## Export DEBUG for sub-script
|
|
|
|
|
export DEBUG
|
|
|
|
|
|
|
|
|
|
## Default values for some vars
|
|
|
|
|
readonly EZA_SYSTEM_PATH="/bin/eza"
|
|
|
|
|
readonly EZA_USER_PATH="${HOME}/bin/eza.link"
|
|
|
|
|
|
|
|
|
|
## Colors
|
|
|
|
|
readonly PURPLE='\033[1;35m'
|
|
|
|
|
readonly RED='\033[0;31m'
|
|
|
|
|
readonly RESET='\033[0m'
|
|
|
|
|
readonly COLOR_DEBUG="${PURPLE}"
|
|
|
|
|
# }}}
|
|
|
|
|
usage() { # {{{
|
|
|
|
|
|
|
|
|
|
cat <<- HELP
|
2023-09-19 10:31:40 +02:00
|
|
|
|
usage: $PROGNAME [--debug|help|--user|--system]
|
2023-09-16 08:07:33 +02:00
|
|
|
|
|
|
|
|
|
Tiny script to start existing eza (from the system or from user's PATH)
|
|
|
|
|
|
|
|
|
|
EXAMPLES :
|
|
|
|
|
- Start eza
|
|
|
|
|
${PROGNAME}
|
|
|
|
|
|
|
|
|
|
- Set different binary for user
|
2023-09-19 10:31:40 +02:00
|
|
|
|
${PROGNAME} --user "${HOME}/bin/my.eza"
|
2023-09-16 08:07:33 +02:00
|
|
|
|
|
|
|
|
|
OPTIONS :
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--debug
|
2023-09-16 08:07:33 +02:00
|
|
|
|
Enable debug messages.
|
|
|
|
|
|
2023-09-19 10:31:40 +02:00
|
|
|
|
help
|
2023-09-16 08:07:33 +02:00
|
|
|
|
Print this help message.
|
|
|
|
|
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--user
|
2023-09-16 08:07:33 +02:00
|
|
|
|
Change the user's binary to use.
|
|
|
|
|
(default: ${EZA_USER_PATH})
|
|
|
|
|
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--system
|
2023-09-16 08:07:33 +02:00
|
|
|
|
Change the system's binary to use.
|
|
|
|
|
(default: ${EZA_SYSTEM_PATH})
|
2023-09-19 10:31:40 +02:00
|
|
|
|
|
|
|
|
|
ALL OTHER OPTIONS WILL BE PASSED AS EZA's OPTIONS
|
2023-09-16 08:07:33 +02:00
|
|
|
|
HELP
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
debug_message() { # {{{
|
|
|
|
|
|
|
|
|
|
local_debug_message="${1}"
|
|
|
|
|
|
|
|
|
|
## Print message if DEBUG is enable (=0)
|
|
|
|
|
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG − ${PROGNAME} : ${local_debug_message}"
|
|
|
|
|
|
|
|
|
|
unset local_debug_message
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
error_message() { # {{{
|
|
|
|
|
|
|
|
|
|
local_error_message="${1}"
|
|
|
|
|
local_error_code="${2}"
|
|
|
|
|
|
|
|
|
|
## Print message
|
|
|
|
|
printf '%b\n' "ERROR − ${PROGNAME} : ${RED}${local_error_message}${RESET}" >&2
|
|
|
|
|
|
|
|
|
|
unset local_error_message
|
|
|
|
|
|
|
|
|
|
exit "${local_error_code:=66}"
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
define_vars() { # {{{
|
|
|
|
|
|
|
|
|
|
# If eza_system_path wasn't defined (argument) {{{
|
|
|
|
|
if [ -z "${eza_system_path-}" ]; then
|
|
|
|
|
## Use default value
|
|
|
|
|
readonly eza_system_path="${EZA_SYSTEM_PATH}"
|
|
|
|
|
fi
|
|
|
|
|
# }}}
|
|
|
|
|
# If eza_user_path wasn't defined (argument) {{{
|
|
|
|
|
if [ -z "${eza_user_path-}" ]; then
|
|
|
|
|
## Use default value
|
|
|
|
|
readonly eza_user_path="${EZA_USER_PATH}"
|
|
|
|
|
fi
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
is_bin_present() { # {{{
|
|
|
|
|
|
|
|
|
|
local_bin_present="${1}"
|
|
|
|
|
debug_prefix="${2:-}"
|
|
|
|
|
|
|
|
|
|
## File doesn't exist by default
|
|
|
|
|
return_is_bin_present="1"
|
|
|
|
|
|
|
|
|
|
### Check if the file exists with executable perm
|
|
|
|
|
# shellcheck disable=SC2086
|
|
|
|
|
if find ${local_bin_present} -perm -a+x > /dev/null 2>&1; then
|
|
|
|
|
return_is_bin_present="0"
|
|
|
|
|
debug_message "${debug_prefix}is_bin_present − \
|
|
|
|
|
The file ${RED}${local_bin_present}${COLOR_DEBUG} exists."
|
|
|
|
|
else
|
|
|
|
|
return_is_bin_present="1"
|
|
|
|
|
debug_message "${debug_prefix}is_bin_present − \
|
|
|
|
|
The file ${RED}${local_bin_present}${COLOR_DEBUG} doesn't exist."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
unset local_bin_present="${1}"
|
|
|
|
|
unset debug_prefix
|
|
|
|
|
|
|
|
|
|
return "${return_is_bin_present}"
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
main() { # {{{
|
|
|
|
|
|
|
|
|
|
debug_message "--- MAIN BEGIN"
|
|
|
|
|
|
|
|
|
|
## Define all vars
|
|
|
|
|
define_vars
|
|
|
|
|
debug_message "| Define vars"
|
|
|
|
|
|
|
|
|
|
## If eza system is available {{{
|
|
|
|
|
### Start it
|
|
|
|
|
if is_bin_present "${eza_system_path}" "| "; then
|
2023-09-19 10:31:40 +02:00
|
|
|
|
### If EZA_OPTIONS is defined, call eza with options {{{
|
|
|
|
|
if [ -n "${EZA_OPTIONS-}" ]; then
|
|
|
|
|
debug_message "Use Eza from system (${eza_system_path}) with options (${EZA_OPTIONS})."
|
|
|
|
|
"${eza_system_path}" $(echo "${EZA_OPTIONS}") \
|
|
|
|
|
|| error_message "Error while calling eza from system path (${eza_system_path})" 11
|
|
|
|
|
### }}}
|
|
|
|
|
### If EZA_OPTIONS is NOT defined simply call it {{{
|
|
|
|
|
elif [ -z "${EZA_OPTIONS-}" ]; then
|
|
|
|
|
debug_message "Use Eza from system (${eza_system_path}) without options."
|
|
|
|
|
"${eza_system_path}" \
|
|
|
|
|
|| error_message "Error while calling eza from system path (${eza_system_path})" 12
|
|
|
|
|
### }}}
|
|
|
|
|
### If anything else happens, exit with error {{{
|
|
|
|
|
else
|
|
|
|
|
error_message "This test case is not supposed to happen ! Variable EZA_OPTIONS is defined or not." 13
|
|
|
|
|
fi
|
|
|
|
|
### }}}
|
2023-09-16 08:07:33 +02:00
|
|
|
|
debug_message "--- MAIN END"
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
## }}}
|
|
|
|
|
## If eza from user path is available {{{
|
|
|
|
|
if is_bin_present "${eza_user_path}" "| "; then
|
2023-09-19 10:31:40 +02:00
|
|
|
|
### If EZA_OPTIONS is defined, call eza with options {{{
|
|
|
|
|
if [ -n "${EZA_OPTIONS-}" ]; then
|
|
|
|
|
debug_message "Use Eza from user's path (${eza_user_path}) with options (${EZA_OPTIONS})."
|
|
|
|
|
"${eza_user_path}" $(echo "${EZA_OPTIONS}") \
|
|
|
|
|
|| error_message "Error while calling eza from system path (${eza_user_path})" 21
|
|
|
|
|
### }}}
|
|
|
|
|
### If EZA_OPTIONS is NOT defined simply call it {{{
|
|
|
|
|
elif [ -z "${EZA_OPTIONS-}" ]; then
|
|
|
|
|
debug_message "Use Eza from user's path (${eza_user_path}) without options."
|
|
|
|
|
"${eza_user_path}" \
|
|
|
|
|
|| error_message "Error while calling eza from user path (${eza_user_path})" 22
|
|
|
|
|
### }}}
|
|
|
|
|
### If anything else happens, exit with error {{{
|
|
|
|
|
else
|
|
|
|
|
error_message "This test case is not supposed to happen ! Variable EZA_OPTIONS is defined or not." 23
|
|
|
|
|
fi
|
|
|
|
|
### }}}
|
2023-09-16 08:07:33 +02:00
|
|
|
|
debug_message "--- MAIN END"
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
## }}}
|
|
|
|
|
|
|
|
|
|
debug_message "--- MAIN END"
|
|
|
|
|
}
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
# Manage arguments # {{{
|
|
|
|
|
# This code can't be in a function due to argument management
|
|
|
|
|
|
|
|
|
|
if [ ! "${NBARGS}" -eq "0" ]; then
|
|
|
|
|
|
|
|
|
|
manage_arg="0"
|
|
|
|
|
|
2023-09-19 10:31:40 +02:00
|
|
|
|
## If the first argument ask for help (h|help) {{{
|
|
|
|
|
if printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^h(elp)?$"; then
|
2023-09-16 08:07:33 +02:00
|
|
|
|
usage
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
## }}}
|
|
|
|
|
|
|
|
|
|
## If the first argument is not an option
|
|
|
|
|
if ! printf -- '%s' "${1}" | grep --quiet --extended-regexp -- "^-+";
|
|
|
|
|
then
|
|
|
|
|
## Print help message and exit
|
|
|
|
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|
|
|
|
printf '%b\n' "---"
|
|
|
|
|
usage
|
|
|
|
|
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Parse all options (start with a "-") one by one
|
|
|
|
|
while printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^-+"; do
|
|
|
|
|
|
|
|
|
|
case "${1}" in
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--debug ) ## debug
|
2023-09-16 08:07:33 +02:00
|
|
|
|
DEBUG=0
|
|
|
|
|
debug_message "--- Manage argument BEGIN"
|
|
|
|
|
;;
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--user ) ## Define eza_user_path with given arg
|
2023-09-16 08:07:33 +02:00
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
## Define var
|
|
|
|
|
readonly eza_user_path="${1}"
|
|
|
|
|
;;
|
2023-09-19 10:31:40 +02:00
|
|
|
|
--system ) ## Define eza_system_path with given arg
|
2023-09-16 08:07:33 +02:00
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
## Define var
|
|
|
|
|
readonly eza_system_path="${1}"
|
|
|
|
|
;;
|
|
|
|
|
* ) ## unknow option
|
2023-09-19 10:31:40 +02:00
|
|
|
|
debug_message "Unknown option (${1}), pass it as eza's option"
|
|
|
|
|
test -n "${EZA_OPTIONS-}" && EZA_OPTIONS="${EZA_OPTIONS} ${1}"
|
|
|
|
|
test -z "${EZA_OPTIONS-}" && EZA_OPTIONS="${1}"
|
2023-09-16 08:07:33 +02:00
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
debug_message "| ${RED}${1}${COLOR_DEBUG} option managed."
|
|
|
|
|
|
|
|
|
|
## Move to the next argument
|
|
|
|
|
shift
|
|
|
|
|
manage_arg=$((manage_arg+1))
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
debug_message "| ${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed."
|
|
|
|
|
else
|
|
|
|
|
debug_message "| No arguments/options to manage."
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
debug_message "--- Manage argument END"
|
|
|
|
|
# }}}
|
|
|
|
|
|
|
|
|
|
main
|
|
|
|
|
|
|
|
|
|
exit 255
|