clean dead links

This commit is contained in:
Lukas Wurzinger 2025-01-05 15:22:27 +01:00
parent d45aacbead
commit b7e9b356d8
No known key found for this signature in database
3 changed files with 51 additions and 2 deletions

2
.gitignore vendored
View file

@ -2,3 +2,5 @@
.devenv/
.pre-commit-config.yaml
.plowcache

View file

@ -39,6 +39,7 @@
pkgs.lib.mkIf (devenvRootFileContent != "") devenvRootFileContent;
env.PLOW_FROM = "./home";
env.PLOW_CACHE = "./.plowcache";
imports = [
./devenv.nix

View file

@ -4,9 +4,30 @@ set -o errexit
set -o nounset
set -o pipefail
progname="$0"
error() {
local line
for line in "$@"; do
echo "$progname: $line" 1>&2
done
exit 1
}
shopt -s nullglob globstar
opts=$(getopt --options f:t:Fivm: --longoptions=from:,to:,force,interactive,verbose,directory-mode: --name "$0" -- "$@")
if [[ ! -v PLOW_CACHE ]]; then
PLOW_CACHE=.plowcache
fi
opts=$(
getopt \
--options f:t:Fivm: \
--longoptions from:,to:,force,interactive,verbose,directory-mode: \
--name "$0" \
-- "$@"
)
eval set -- "$opts"
@ -14,6 +35,7 @@ from=${PLOW_FROM:-$PWD}
to=${PLOW_TO:-$HOME}
lnflags=()
mkdirflags=()
rmflags=()
while true; do
case "$1" in
-f | --from)
@ -26,15 +48,18 @@ while true; do
;;
-F | --force)
lnflags+=(--force)
rmflags+=(--force)
shift
;;
-i | --interactive)
lnflags+=(--interactive)
rmflags+=(--interactive)
shift
;;
-v | --verbose)
lnflags+=(--verbose)
mkdirflags+=(--verbose)
rmflags+=(--verbose)
shift
;;
-m | --directory-mode)
@ -62,13 +87,34 @@ fi
shopt -s dotglob
cache=()
if [[ -n "$PLOW_CACHE" ]]; then
if [[ -e "$PLOW_CACHE" ]]; then
while IFS= read -r link; do
cache+=("$link")
done < "$PLOW_CACHE"
fi
: > "$PLOW_CACHE"
fi
for choice in "${choices[@]}"; do
prefix=$from/$choice
for target in "$prefix"/**/*; do
if [[ -f "$target" ]]; then
link=$to${target#"$prefix"}
mkdir --parents "${mkdirflags[@]}" -- "$(dirname -- "$link")"
parent=$(dirname -- "$link")
mkdir --parents "${mkdirflags[@]}" -- "$parent"
ln --symbolic "${lnflags[@]}" -- "$target" "$link"
if [[ -n "$PLOW_CACHE" ]]; then
echo "$link" >> "$PLOW_CACHE"
fi
fi
done
done
for link in "${cache[@]}"; do
if [[ -L "$link" && ! -f "$link" ]]; then
rm "${rmflags[@]}" -- "$link"
fi
done