dotfiles/bin/dotfiles.sh
2022-02-03 19:33:04 +01:00

54 lines
No EOL
1.4 KiB
Bash
Executable file

#!/usr/bin/env bash
SWD="$(dirname "$(realpath "$0")")"
function rebase-fs() {
# disable null glob because empty dirs will break
# globbing and return the glob char '*'
shopt -s nullglob
local src_path=$1 dst_path=$2 file stat
[[ -d "${src_path}" ]] && stat="d"
[[ -f "${src_path}" ]] && stat="f"
echo "${src_path}" "${dst_path}" "${stat}"
case ${stat} in
d)
for file in "${src_path}"/*; do
local path="${file/#${src_path}/${dst_path}}"
rebase-fs "${file}" "${path}"
done
;;
esac
}
function mirror-fs() {
local func_f="${1:?""}" func_d="${2:-"$1"}"
local src dst typ
while read -r src dst typ; do
dst="${dst//\/dot-/\/.}"
case ${typ} in
d) "${func_d}" "${src}" "${dst}" "${typ}" ;;
f) "${func_f}" "${src}" "${dst}" "${typ}" ;;
esac
done
}
function clone-d() {
local src="${1:?""}" dst="${2:?""}" typ="${3:?""}"
case ${typ} in
d) command -p mkdir --parent "${dst}" ;;
f) command -p cp --update "${src}" "${dst}" ;;
esac
command -p mkdir --parent "${dst}"
command -p chmod --reference="${src}" "${dst}"
}
function clone-f() {
local src="${1:?""}" dst="${2:?""}" typ="${3:?""}"
if [[ ${src} = *.localhost && -e ${dst} ]]; then
printf "skipping local file %s\n" "${dst}"
return
fi
command -p cp --update "${src}" "${dst}"
}
SRC="$(realpath "${SWD}/../home")"
rebase-fs "${SRC}" "${HOME}" | mirror-fs 'clone-f' 'clone-d'