dotfiles/install.sh

98 lines
2.5 KiB
Bash
Executable file

#!/bin/bash
set -euo pipefail
DOTFILES_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
STOWERS_DIR="${DOTFILES_DIR}/stowers"
MODULES_DIR="${DOTFILES_DIR}/modules"
TARGET_DIR="${HOME}"
install_stower() {
if ! command -v stow &>/dev/null; then
echo "[!] GNU Stow is not installed!" >&2
return 1
fi
if [[ "$#" -lt 1 ]]; then
echo "[!] No stower given!" >&2
return 1
fi
local stower_name stower_dir
stower_name="$1"
stower_dir="${STOWERS_DIR}/${stower_name}"
if [[ ! -d "${stower_dir}" ]]; then
echo "[!] Stower (${stower_dir}) is not a directory!" >&2
return 1
fi
local COMMAND_DEPS
COMMAND_DEPS=()
if [[ -f "${stower_dir}"/config.sh ]]; then
source "${stower_dir}"/config.sh
fi
for required_cmd in "${COMMAND_DEPS[@]}"; do
if ! command -v "${required_cmd}" &>/dev/null; then
echo "[${stower_name}] Missing required command: ${required_cmd}" >&2
return 1
fi
done
stow --dotfiles \
--dir "${STOWERS_DIR}" \
--target "${TARGET_DIR}" \
--ignore "config.sh" "${stower_name}" || \
{ [[ "$(type -t fallback)" == "function" ]] && fallback "${stower_dir}" "${TARGET_DIR}"; } || \
{ echo "[!] [${stower_name}] Failed to install!" >&2; return 1; }
echo "[+] [${stower_name}] Installed!"
}
install_module() {
if [[ "$#" -lt 1 ]]; then
echo "[!] No module given!" >&2
return 1
fi
local module_dir
module_dir="${MODULES_DIR}/$1"
if [[ ! -d "${module_dir}" ]] || [[ ! -f "${module_dir}/install.sh" ]]; then
echo "[!] Module (${module_dir}) is not a module!" >&2
return 1
fi
/bin/bash "${module_dir}/install.sh"
}
STOWERS=()
MODULES=()
if [[ "$#" -gt 0 ]]; then
for stower_or_module in "$@"; do
if [[ -e "${STOWERS_DIR}/${stower_or_module}" ]]; then
STOWERS+=("${stower_or_module}")
elif [[ -e "${MODULES_DIR}/${stower_or_module}" ]]; then
MODULES+=("${stower_or_module}")
else
echo "[!] No such stower/module: ${stower_or_module}" >&2
return 1
fi
done
else
for stower in "${STOWERS_DIR}"/*/; do
stower_name="$(basename -- "${stower}")"
STOWERS+=("${stower_name}")
done
fi
for stower in "${STOWERS[@]}"; do
install_stower "${stower}" || true
done
for module in "${MODULES[@]}"; do
install_module "${module}" || true
done
echo "[+] Installed everything!"