2018-02-22 04:15:49 +00:00
|
|
|
#!/bin/bash
|
|
|
|
set +e
|
2018-02-23 21:37:38 +00:00
|
|
|
trap 'exit' INT
|
2018-02-22 04:15:49 +00:00
|
|
|
|
2018-02-22 15:43:19 +00:00
|
|
|
RECIPE_PATH=${HOME}/dotfiles/recipes
|
2018-04-12 18:07:33 +00:00
|
|
|
RECIPES=$(ls $RECIPE_PATH|grep -v '^_' | sort)
|
2018-02-23 15:12:24 +00:00
|
|
|
INSTALL=()
|
|
|
|
STACK=()
|
2018-02-22 15:43:19 +00:00
|
|
|
|
2020-06-21 00:04:46 +00:00
|
|
|
if test -t 1; then
|
|
|
|
ncolors=$(tput colors)
|
|
|
|
if test -n "$ncolors" && test $ncolors -ge 8; then
|
|
|
|
bold="$(tput bold)"
|
|
|
|
dim="$(tput dim)"
|
|
|
|
normal="$(tput sgr0)"
|
|
|
|
red="$(tput setaf 1)"
|
|
|
|
green="$(tput setaf 2)"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-02-22 15:43:19 +00:00
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
-D|--debug)
|
|
|
|
DEBUG=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-l|--list)
|
|
|
|
echo Available recipes:
|
|
|
|
for recipe in $RECIPES; do
|
2018-04-12 18:07:33 +00:00
|
|
|
description=$(grep '^# Description: ' "${RECIPE_PATH}/$recipe" \
|
|
|
|
| cut -d' ' -f 3-)
|
|
|
|
printf "%15s %s\n" "$recipe" "$description"
|
2018-02-22 15:43:19 +00:00
|
|
|
done
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
-A|--all)
|
|
|
|
INSTALL=($RECIPES)
|
|
|
|
shift
|
|
|
|
;;
|
2018-02-27 05:21:29 +00:00
|
|
|
-r|--restart)
|
|
|
|
RESTART_SHELL=1
|
|
|
|
shift
|
|
|
|
;;
|
2018-04-12 17:43:07 +00:00
|
|
|
-h|--help)
|
|
|
|
cat <<EOF
|
|
|
|
Usage: $(basename $0) [OPTION]... [RECIPE]...
|
|
|
|
|
|
|
|
Provision one or more dotfiles RECIPEs
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-A, --all Install all available recipes.
|
|
|
|
-D, --debug Enable debug logging, including command output for each
|
|
|
|
step.
|
|
|
|
-h, --help Display this help text and exit.
|
|
|
|
-l, --list Display all available recipes and exit.
|
|
|
|
-r, --restart Restart the shell upon completion.
|
|
|
|
|
|
|
|
If no RECIPE is provided (and the -A/--all flag is not set), the
|
|
|
|
'base' recipe will be provisioned.
|
|
|
|
|
|
|
|
EOF
|
|
|
|
exit
|
|
|
|
;;
|
2018-02-22 15:43:19 +00:00
|
|
|
*)
|
|
|
|
INSTALL+=("$1")
|
|
|
|
shift
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
INSTALL=($(for recipe in "${INSTALL[@]}"; do
|
|
|
|
echo $recipe
|
|
|
|
done |sort | uniq))
|
|
|
|
|
2018-02-22 04:15:49 +00:00
|
|
|
function _run {
|
|
|
|
local msg=$1
|
|
|
|
shift
|
|
|
|
if [ -z "$DEBUG" ]; then
|
|
|
|
echo -n "$msg..."
|
2018-05-02 21:22:50 +00:00
|
|
|
$@ >/dev/null 2>&1
|
2018-02-22 04:15:49 +00:00
|
|
|
else
|
|
|
|
echo "$msg..."
|
|
|
|
$@
|
|
|
|
fi
|
2020-06-21 00:04:46 +00:00
|
|
|
echo "${green}done.${normal}"
|
2018-02-22 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _recipe {
|
2018-02-28 05:09:33 +00:00
|
|
|
local recipe
|
|
|
|
local path
|
|
|
|
if [ -f "$1" ]; then
|
|
|
|
recipe="$(basename $1)"
|
|
|
|
path=$(dirname "$1")
|
|
|
|
elif [ -f "${RECIPE_PATH}/$1" ]; then
|
|
|
|
recipe="$1"
|
|
|
|
path="${RECIPE_PATH}"
|
|
|
|
else
|
2020-06-21 00:04:46 +00:00
|
|
|
echo "${bold}${red}Could not load recipe '$1'${normal}" >&2
|
2018-02-28 05:09:33 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2018-02-23 15:12:24 +00:00
|
|
|
STACK+=("$1")
|
2020-06-21 00:04:46 +00:00
|
|
|
pushd "$path" >/dev/null
|
|
|
|
echo "${dim}-- Recipe [${STACK[@]}]'${normal}"
|
2018-02-28 05:09:33 +00:00
|
|
|
source "./${recipe}"
|
2020-06-21 00:04:46 +00:00
|
|
|
popd >/dev/null
|
|
|
|
unset 'STACK[-1]'
|
|
|
|
echo "${dim}-- Recipe [${STACK[@]}]${normal}"
|
|
|
|
|
2018-02-22 04:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
USER=${USER:-$(whoami)}
|
|
|
|
_PLATFORM=$(uname -s | awk '{print tolower($1)}')
|
|
|
|
|
2018-02-23 15:12:24 +00:00
|
|
|
if [ -z "${INSTALL[@]}" ]; then
|
|
|
|
INSTALL=(base)
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2018-02-22 15:43:19 +00:00
|
|
|
for recipe in "${INSTALL[@]}"; do
|
2018-02-22 04:15:49 +00:00
|
|
|
_recipe $recipe
|
|
|
|
done
|
|
|
|
|
2020-06-21 00:04:46 +00:00
|
|
|
echo "${green}Finished${normal}"
|
2018-02-27 05:21:29 +00:00
|
|
|
if [ -n "$RESTART_SHELL" ]; then
|
|
|
|
echo "Restarting shell."
|
|
|
|
exec $SHELL
|
|
|
|
fi
|