From 98b33cc61501ad459fd67182775ae8fdf9be61fa Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 31 Mar 2022 03:09:30 +0200 Subject: [PATCH] meta:archive: Get link targets by parsing config Instead of only archiving the ~/.config/*, now parse `install.conf.yaml` and extract the link targets dynamically. --- meta/archive | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/meta/archive b/meta/archive index 5165cd3..73b510a 100755 --- a/meta/archive +++ b/meta/archive @@ -1,9 +1,51 @@ -#!/bin/sh +#!/bin/bash + +CONFIG=meta/install.conf.yaml + +set -o noglob +# Match all source and target paths of the link segment +links=( $( + sed -n '/^- link:$/,$ p' "$CONFIG" \ + | grep -Po "(?<=path: ).*|(?<=^ ).*(?=:$)" \ + | sed "s/~/${HOME//\//\\/}/g" +) ) +set +o noglob + +# Assemble files that would be potentially overwritten by the `link` step +typeset -a to_archive +for (( i = 0; i < ${#links[@]}; i+=2 )); do + # Source and target of link + src="${links[$((i+1))]}" + tar="${links[$i]}" + + # TODO: This assumes that the globbing can only be specified at the end + if [[ ${src##*\*} ]]; then + # `glob` set to false; just add the target + to_archive+=("$tar") + else + # `glob` set to true; add globbed path portion in target + + # TODO: this breaks on files with spaces, but quotes must not be used for + # the globbing to take effect + for path in $src; do + # Keep the dirname as base + base="${src%%/*}/" + # Only the matched portion + file="${path#$base}" + + # Skip `.` and `..` when globbing hidden files through `/.*` + [[ $file != '.' && $file != '..' ]] || continue + + # Avoid potential double slashes (e.g. `//`) + to_archive+=("${tar%/}${file:+/$file}") + done + fi +done tmpd="$(mktemp -d)" -for file in .config/*; do - [ ! -e "$HOME/$file" ] || mv "$HOME/$file" "$tmpd" +for file in "${to_archive[@]}"; do + [[ ! -e "$file" ]] || mv "$file" "$tmpd" done if rmdir "$tmpd" 2>/dev/null; then