Update install script.

- Add code to support installation of files in subfolders of dotfiles
content.
- Add code to support patching of JSON files via jq.
- Add code to ensure target directory exists.
- Fix handling of target file paths.
- Remove install_dotfiles function as it is now unused.
master
Icedream 2018-02-17 19:09:10 +01:00
parent 501fb4cd64
commit fddd5cb144
Signed by: icedream
GPG Key ID: 1573F6D8EFE4D0CF
1 changed files with 28 additions and 13 deletions

View File

@ -1,31 +1,46 @@
#!/bin/sh -e
install_dotfiles() {
local target_dir="$1"
for d in dotfiles/*; do
cp -v "$d" "${target_dir}/$(basename "$d")"
done
}
install_files() {
local target_dir="$1"
local source_dir="${2:-.}"
local source_dir="$(readlink -f "${2:-.}")"
local target_filename_prefix="${3}"
mkdir -vp "${target_dir}"
target_dir="$(readlink -f "${target_dir}")"
if [ ! -d "${source_dir}" ]; then
echo "Source directory ${source_dir} does not exist, skipping."
return
fi
(
cd "${source_dir}"
for f in *; do
case "$f" in
^dotfiles)
install_dotfiles "${target_dir}"
for f in "${source_dir}"/*; do
case "$(basename "$f")" in
dotfiles)
install_files "${target_dir}" "$f" .
;;
*.jq)
tmpfile="$(mktemp)"
if [ -f "$f" ]; then
tmpfile="$(mktemp)"
target_file="${target_dir}/$(basename "$f" .jq)"
if [ -f "$target_file" ]; then
cat "$target_file"
else
echo "{}"
fi | jq "$(cat "$f")" > "$tmpfile"
mv -v "$tmpfile" "$target_file"
else
echo "Expected source file with .jq extension not to be a directory."
exit 1
fi
;;
*)
if [ -d "$f" ]; then
(cd "$d" && install_files "${target_dir}/$f")
install_files "${target_dir}/${target_filename_prefix}$(basename "$f")" "$f"
else
cp -v "$f" "${target_dir}"
cp -v "$f" "${target_dir}/${target_filename_prefix}$(basename "$f")"
fi
;;
esac