1
0
Fork 0
This commit is contained in:
Lukas Wurzinger 2024-12-19 00:15:20 +01:00
parent 77eccec635
commit 16ea2e2d7c
No known key found for this signature in database
18 changed files with 407 additions and 15 deletions

6
plow/default.nix Normal file
View file

@ -0,0 +1,6 @@
{writeShellApplication}:
writeShellApplication {
name = "plow";
text = builtins.readFile ./plow.bash;
}

74
plow/plow.bash Executable file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s nullglob globstar
opts=$(getopt --options f:t:Fivm: --longoptions=from:,to:,force,interactive,verbose,directory-mode: --name "$0" -- "$@")
eval set -- "$opts"
from=${PLOW_FROM:-$PWD}
to=${PLOW_TO:-$HOME}
lnflags=()
mkdirflags=()
while true; do
case "$1" in
-f | --from)
from=$2
shift 2
;;
-t | --to)
to=$2
shift 2
;;
-F | --force)
lnflags+=(--force)
shift
;;
-i | --interactive)
lnflags+=(--interactive)
shift
;;
-v | --verbose)
lnflags+=(--verbose)
mkdirflags+=(--verbose)
shift
;;
-m | --directory-mode)
mkdirflags+=(--mode "$2")
shift 2
;;
--)
shift
break
;;
esac
done
from=$(realpath --strip -- "$from")
to=$(realpath --strip -- "$to")
if (( $# > 0 )); then
choices=("$@")
else
choices=()
for dir in "$from"/*/; do
choices+=("$(basename -- "$dir")")
done
fi
shopt -s dotglob
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")"
ln --symbolic "${lnflags[@]}" -- "$target" "$link"
fi
done
done