2021-08-03 23:38:03 -04:00
|
|
|
#!/bin/bash
|
2021-03-18 21:42:44 -04:00
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
RED='\033[0;31m'
|
|
|
|
GREEN='\033[0;32m'
|
|
|
|
ORANGE='\033[0;33m'
|
|
|
|
BLUE='\033[0;34m'
|
|
|
|
NC='\033[0m' # No Color
|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
|
|
|
|
prompt() {
|
|
|
|
case ${1} in
|
|
|
|
"-s" | "--success")
|
|
|
|
printf "${GREEN}%s${NC}\n" "${2}"
|
|
|
|
;;
|
|
|
|
"-e" | "--error")
|
|
|
|
printf "${RED}%s${NC}\n" "${2}"
|
|
|
|
;;
|
|
|
|
"-w" | "--warning")
|
|
|
|
printf "${ORANGE}%s${NC}\n" "${2}"
|
|
|
|
;;
|
|
|
|
"-i" | "--info")
|
|
|
|
printf "${BLUE}%s${NC}\n" "${2}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
printf "${GREEN}%s${NC}\n" "${2}"
|
|
|
|
;;
|
|
|
|
esac
|
2021-07-03 00:13:42 -04:00
|
|
|
}
|
2021-03-26 22:50:15 -04:00
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
_usage() {
|
|
|
|
printf "%s" \
|
|
|
|
"Usage: sh ${0##*/} [ options ]
|
|
|
|
-h | --help => Show this help.
|
|
|
|
-i | --install => Install the config.
|
|
|
|
-r | --remove => Remove the config.
|
|
|
|
-u | --update => Update the existing config without removing existing stuff.
|
|
|
|
"
|
2021-07-09 04:54:42 -04:00
|
|
|
}
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
_eval_exit() {
|
2021-08-03 23:38:03 -04:00
|
|
|
status=$1
|
2021-08-02 22:23:20 -04:00
|
|
|
suc_msg=$2
|
|
|
|
err_msg=$3
|
|
|
|
|
|
|
|
case $status in
|
|
|
|
0)
|
|
|
|
prompt -s "${suc_msg}"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
prompt -e "${err_msg}"
|
|
|
|
;;
|
2021-07-09 04:54:42 -04:00
|
|
|
esac
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-03 23:38:03 -04:00
|
|
|
_check_dependencies() {
|
|
|
|
_error_dependencies() {
|
|
|
|
prompt -r "Error: Install ${1} before proceeding."
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
command -v git 1>/dev/null || _error_dependencies git
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
_install() {
|
|
|
|
_check_dependencies
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
_remove() {
|
2021-08-03 23:38:03 -04:00
|
|
|
prompt -i "-> Cleaning config ($HOME/.config/nvim/)"
|
2021-08-02 22:23:20 -04:00
|
|
|
# rm -rf "$HOME/.config/nvim/"
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-03 23:38:03 -04:00
|
|
|
prompt -i "-> Cleaning miscellaneous ($HOME/.local/share/nvim/)"
|
|
|
|
# rm -rf "$HOME/.local/share/nvim/"
|
|
|
|
|
|
|
|
prompt -i "-> Cleaning cache ($HOME/.cache/nvim/)"
|
|
|
|
# rm -rf "$HOME/.cache/nvim/"
|
2021-07-09 04:54:42 -04:00
|
|
|
}
|
2021-08-02 22:23:20 -04:00
|
|
|
# _update() {}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-03 23:38:03 -04:00
|
|
|
_init_settings() {
|
|
|
|
no_backup="false"
|
|
|
|
install="false"
|
|
|
|
uninstall="false"
|
|
|
|
}
|
|
|
|
|
|
|
|
_clean_arg() {
|
|
|
|
arg=$1
|
|
|
|
if [[ "$arg" == "--"* ]]; then
|
|
|
|
echo "${arg:2}"
|
|
|
|
elif [[ "$arg" == "-"* ]]; then
|
|
|
|
echo "${arg:1}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_parse_args() {
|
|
|
|
local args_func=$1
|
|
|
|
local argv=("$@")
|
|
|
|
local argc=${#argv[@]}
|
|
|
|
local skip=0
|
|
|
|
|
|
|
|
_skip_ahead() {
|
|
|
|
amount=$1
|
|
|
|
skip=$((skip + amount))
|
|
|
|
}
|
|
|
|
|
|
|
|
_clean_arg() {
|
|
|
|
arg=$1
|
|
|
|
if [[ "$arg" == "--"* ]]; then
|
|
|
|
echo "${arg:2}"
|
|
|
|
elif [[ "$arg" == "-"* ]]; then
|
|
|
|
echo "${arg:1}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
for j in "${!argv[@]}"; do
|
|
|
|
if [[ ${skip} -gt 0 ]]; then
|
|
|
|
left=$((argc - j))
|
|
|
|
while [[ ${skip} > ${left} ]]; do ((skip--)); done
|
|
|
|
skip=$((skip - 1))
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
case ${argv[j]} in
|
|
|
|
--*) # End of all options.
|
|
|
|
case ${argv[j]} in
|
|
|
|
--) # End of all options.
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
eval "${eval_args}" "$(_clean_arg "${argv[j]}")" "$j"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
tangled_args=$(_clean_arg "${argv[j]}")
|
|
|
|
for ((k = 0; k < ${#tangled_args}; k++)); do
|
|
|
|
eval "${eval_args}" "${tangled_args:$k:1}" "$j"
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
prompt -w "Warning: flag ''${argv[j]}' not recognized"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-07-09 04:54:42 -04:00
|
|
|
main() {
|
2021-08-03 23:38:03 -04:00
|
|
|
local argv=("$@")
|
|
|
|
local argc=${#argv[@]}
|
|
|
|
local skip=0
|
|
|
|
|
|
|
|
assert_arg() {
|
|
|
|
var=$1
|
|
|
|
index=$2
|
|
|
|
case ${var} in
|
|
|
|
h | help)
|
2021-08-02 22:23:20 -04:00
|
|
|
_usage
|
|
|
|
;;
|
2021-08-03 23:38:03 -04:00
|
|
|
i | install)
|
|
|
|
prompt -i "installing..."
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
2021-08-03 23:38:03 -04:00
|
|
|
r | remove)
|
|
|
|
prompt -i "removing..."
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
2021-08-03 23:38:03 -04:00
|
|
|
# p | --path
|
|
|
|
*)
|
|
|
|
prompt -w "Warning: flag '${var}' not recognized"
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
|
|
|
esac
|
2021-08-03 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_skip_ahead() {
|
|
|
|
amount=$1
|
|
|
|
skip=$((skip + amount))
|
|
|
|
}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-03 23:38:03 -04:00
|
|
|
# works for:
|
|
|
|
# 1. normal flags (e.g. -h,--help)
|
|
|
|
# 2. nested flags (e.g. -ivh)
|
|
|
|
# 3. space sperated flags and args (e.g. --option argument)
|
|
|
|
# 4. equal separated flags and args (e.g. --option=argument)
|
|
|
|
|
|
|
|
|
|
|
|
for j in "${!argv[@]}"; do
|
|
|
|
if [[ ${skip} -gt 0 ]]; then
|
|
|
|
left=$((argc - j))
|
|
|
|
while [[ ${skip} > ${left} ]]; do ((skip--)); done
|
|
|
|
skip=$((skip - 1))
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
case ${argv[j]} in
|
|
|
|
--*) # End of all options.
|
|
|
|
case ${argv[j]} in
|
|
|
|
--) # End of all options.
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
assert_arg "$(_clean_arg "${argv[j]}")" "$j"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
-*)
|
|
|
|
tangled_args=$(_clean_arg "${argv[j]}")
|
|
|
|
for ((k = 0; k < ${#tangled_args}; k++)); do
|
|
|
|
assert_arg "${tangled_args:$k:1}" "$j"
|
|
|
|
done
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
prompt -w "Warning: flag ''${argv[j]}' not recognized"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2021-08-02 22:23:20 -04:00
|
|
|
}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
init() {
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
prompt -e "ERROR: This script needs at least one argument"
|
2021-07-09 04:54:42 -04:00
|
|
|
else
|
2021-08-03 23:38:03 -04:00
|
|
|
_init_settings
|
2021-08-02 22:23:20 -04:00
|
|
|
main "${@}"
|
2021-07-09 04:54:42 -04:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
init "${@}"
|