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
|
|
|
|
|
2021-08-04 00:17:40 -04:00
|
|
|
skip=0
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
# 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 ]
|
2021-08-04 00:09:18 -04:00
|
|
|
-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-02 22:23:20 -04:00
|
|
|
"
|
2021-07-09 04:54:42 -04:00
|
|
|
}
|
|
|
|
|
2021-08-02 22:23:20 -04:00
|
|
|
|
2021-08-04 00:09:18 -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
|
|
|
|
|
|
|
_parse_args() {
|
2021-08-04 00:09:18 -04:00
|
|
|
local func_args=$1
|
2021-08-03 23:38:03 -04:00
|
|
|
local argv=("$@")
|
2021-08-04 00:09:18 -04:00
|
|
|
# local skip=0
|
|
|
|
|
|
|
|
unset 'argv[0]' # becuase arg1 is $func_arg
|
|
|
|
for i in "${!argv[@]}"; do new_array+=( "${argv[i]}" ); done
|
|
|
|
argv=("${new_array[@]}")
|
|
|
|
unset new_array
|
|
|
|
|
2021-08-03 23:38:03 -04:00
|
|
|
local argc=${#argv[@]}
|
|
|
|
|
|
|
|
_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
|
|
|
|
;;
|
|
|
|
*)
|
2021-08-04 00:09:18 -04:00
|
|
|
# eval "${func_args}" "$(_clean_arg "${argv[j]}")" "$j"
|
|
|
|
eval "${func_args}" "${argv[j]}" "$j"
|
2021-08-03 23:38:03 -04:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
-*)
|
2021-08-04 00:09:18 -04:00
|
|
|
if [[ ${#argv[j]} -le 2 ]]; then
|
|
|
|
eval "${func_args}" "${argv[j]}" "$j"
|
|
|
|
else
|
|
|
|
tangled_args=$(_clean_arg "${argv[j]}")
|
|
|
|
for ((k = 0; k < ${#tangled_args}; k++)); do
|
|
|
|
eval "${func_args}" "-${tangled_args:$k:1}" "$j"
|
|
|
|
done
|
|
|
|
fi
|
2021-08-03 23:38:03 -04:00
|
|
|
;;
|
|
|
|
*)
|
2021-08-04 00:09:18 -04:00
|
|
|
eval "${func_args}" "${argv[j]}" "$j"
|
2021-08-03 23:38:03 -04:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2021-07-09 04:54:42 -04:00
|
|
|
main() {
|
2021-08-04 00:09:18 -04:00
|
|
|
local argvs=("$@")
|
|
|
|
local argc=${#argvs[@]}
|
2021-08-03 23:38:03 -04:00
|
|
|
|
|
|
|
assert_arg() {
|
2021-08-04 00:09:18 -04:00
|
|
|
var=$1 # flag
|
|
|
|
index=$2 # flag's index
|
2021-08-03 23:38:03 -04:00
|
|
|
case ${var} in
|
2021-08-04 00:09:18 -04:00
|
|
|
-h | --help)
|
2021-08-02 22:23:20 -04:00
|
|
|
_usage
|
|
|
|
;;
|
2021-08-04 00:09:18 -04:00
|
|
|
-i | --install)
|
2021-08-03 23:38:03 -04:00
|
|
|
prompt -i "installing..."
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
2021-08-04 00:09:18 -04:00
|
|
|
-r | --remove)
|
2021-08-03 23:38:03 -04:00
|
|
|
prompt -i "removing..."
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
2021-08-04 00:09:18 -04:00
|
|
|
-a | --action)
|
|
|
|
action=${argvs[index+1]}
|
|
|
|
prompt -i "Action to perform -> ${action}"
|
|
|
|
_skip_ahead 1
|
|
|
|
;;
|
|
|
|
-p=* | --path=*)
|
|
|
|
path="${var#*=}"
|
|
|
|
prompt -i "Path was set to -> ${path}"
|
|
|
|
;;
|
2021-08-03 23:38:03 -04:00
|
|
|
*)
|
2021-08-04 00:09:18 -04:00
|
|
|
prompt -w "Warning: unknown command '${var}'"
|
2021-08-02 22:23:20 -04:00
|
|
|
;;
|
|
|
|
esac
|
2021-08-03 23:38:03 -04:00
|
|
|
}
|
|
|
|
|
2021-08-04 00:09:18 -04:00
|
|
|
_parse_args "assert_arg" "${argvs[@]}"
|
2021-08-02 22:23:20 -04:00
|
|
|
}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-04 00:17:40 -04:00
|
|
|
init() {
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
prompt -e "ERROR: This script needs at least one argument"
|
|
|
|
else
|
|
|
|
main "${@}"
|
|
|
|
fi
|
|
|
|
}
|
2021-07-09 04:54:42 -04:00
|
|
|
|
2021-08-04 00:17:40 -04:00
|
|
|
init "${@}"
|