neovim-confs/install.sh

228 lines
4.8 KiB
Bash
Raw Normal View History

2021-08-04 05:38:03 +02:00
#!/bin/bash
2021-03-19 02:42:44 +01:00
2021-08-03 04:23:20 +02: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-03-27 03:50:15 +01:00
2021-08-03 04:23:20 +02: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-08-03 04:23:20 +02:00
_eval_exit() {
2021-08-04 05:38:03 +02:00
status=$1
2021-08-03 04:23:20 +02:00
suc_msg=$2
err_msg=$3
case $status in
0)
prompt -s "${suc_msg}"
;;
*)
prompt -e "${err_msg}"
;;
esac
2021-08-03 04:23:20 +02:00
}
2021-08-04 05:38:03 +02: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-03 04:23:20 +02:00
_remove() {
2021-08-04 05:38:03 +02:00
prompt -i "-> Cleaning config ($HOME/.config/nvim/)"
2021-08-03 04:23:20 +02:00
# rm -rf "$HOME/.config/nvim/"
2021-08-04 05:38:03 +02: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-08-03 04:23:20 +02:00
# _update() {}
2021-08-04 05:38:03 +02: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
}
main() {
2021-08-04 05:38:03 +02:00
local argv=("$@")
local argc=${#argv[@]}
local skip=0
assert_arg() {
var=$1
index=$2
case ${var} in
h | help)
2021-08-03 04:23:20 +02:00
_usage
;;
2021-08-04 05:38:03 +02:00
i | install)
prompt -i "installing..."
2021-08-03 04:23:20 +02:00
;;
2021-08-04 05:38:03 +02:00
r | remove)
prompt -i "removing..."
2021-08-03 04:23:20 +02:00
;;
2021-08-04 05:38:03 +02:00
# p | --path
*)
prompt -w "Warning: flag '${var}' not recognized"
2021-08-03 04:23:20 +02:00
;;
esac
2021-08-04 05:38:03 +02:00
}
_skip_ahead() {
amount=$1
skip=$((skip + amount))
}
2021-08-04 05:38:03 +02: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-03 04:23:20 +02:00
}
2021-08-03 04:23:20 +02:00
init() {
if [ $# -eq 0 ]; then
prompt -e "ERROR: This script needs at least one argument"
else
2021-08-04 05:38:03 +02:00
_init_settings
2021-08-03 04:23:20 +02:00
main "${@}"
fi
}
2021-08-03 04:23:20 +02:00
init "${@}"