252 lines
6.0 KiB
Plaintext
252 lines
6.0 KiB
Plaintext
|
#!/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
|
|||
|
usage: $PROGNAME [-d|-h|-u|-s]
|
|||
|
|
|||
|
Tiny script to start existing eza (from the system or from user's PATH)
|
|||
|
|
|||
|
EXAMPLES :
|
|||
|
- Start eza
|
|||
|
${PROGNAME}
|
|||
|
|
|||
|
- Set different binary for user
|
|||
|
${PROGNAME} -u "${HOME}/bin/my.eza"
|
|||
|
|
|||
|
OPTIONS :
|
|||
|
-d,--debug
|
|||
|
Enable debug messages.
|
|||
|
|
|||
|
-h,--help
|
|||
|
Print this help message.
|
|||
|
|
|||
|
-u,--user
|
|||
|
Change the user's binary to use.
|
|||
|
(default: ${EZA_USER_PATH})
|
|||
|
|
|||
|
-s,--system
|
|||
|
Change the system's binary to use.
|
|||
|
(default: ${EZA_SYSTEM_PATH})
|
|||
|
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
|
|||
|
debug_message "Use Eza from system (${eza_system_path})"
|
|||
|
"${eza_system_path}" \
|
|||
|
|| error_message "Error while calling eza from system path (${eza_system_path})" 11
|
|||
|
debug_message "--- MAIN END"
|
|||
|
exit 0
|
|||
|
fi
|
|||
|
## }}}
|
|||
|
## If eza from user path is available {{{
|
|||
|
### Start it
|
|||
|
if is_bin_present "${eza_user_path}" "| "; then
|
|||
|
debug_message "Use Eza from user's path (${eza_user_path})"
|
|||
|
"${eza_user_path}" \
|
|||
|
|| error_message "Error while calling eza from user path (${eza_user_path})" 12
|
|||
|
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"
|
|||
|
|
|||
|
## If the first argument ask for help (h|help|-h|-help|-*h|-*help) {{{
|
|||
|
if printf -- '%s' "${1-}" | grep --quiet --extended-regexp -- "^-*h(elp)?$"; then
|
|||
|
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
|
|||
|
-d|--debug ) ## debug
|
|||
|
DEBUG=0
|
|||
|
debug_message "--- Manage argument BEGIN"
|
|||
|
;;
|
|||
|
-u|--user ) ## Define eza_user_path with given arg
|
|||
|
## Move to the next argument
|
|||
|
shift
|
|||
|
## Define var
|
|||
|
readonly eza_user_path="${1}"
|
|||
|
;;
|
|||
|
-s|--system ) ## Define eza_system_path with given arg
|
|||
|
## Move to the next argument
|
|||
|
shift
|
|||
|
## Define var
|
|||
|
readonly eza_system_path="${1}"
|
|||
|
;;
|
|||
|
* ) ## unknow option
|
|||
|
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
|
|||
|
printf '%b\n' "---"
|
|||
|
usage
|
|||
|
exit 1
|
|||
|
;;
|
|||
|
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
|