-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·121 lines (108 loc) · 2.73 KB
/
setup
File metadata and controls
executable file
·121 lines (108 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env sh
#
# Manage dotfiles with GNU Stow.
#
# Running without arguments stows all packages. The `add` subcommand
# automates adopting existing config files into a new stow package
# by creating the package directory, moving the files, and stowing.
#
set -e
DOTFILES="$(cd "$(dirname "$0")" && pwd)"
usage() {
cat <<EOF
Manage dotfiles with GNU Stow.
Usage: ./setup install all packages
./setup add <name> adopt ~/.<name>
./setup add .config <name> adopt ~/.config/<name>
./setup add .config scan ~/.config for unmanaged dirs
The add command moves config files into a new stow package and
creates symlinks back to their original locations.
EOF
}
add_package() {
package="$1"
source="$2"
if [ ! -e "$source" ]; then
echo "error: $source does not exist" >&2
return 1
fi
if [ -d "$DOTFILES/$package" ]; then
echo "skip: $package already managed" >&2
return 1
fi
relative="${source#$HOME/}"
dest="$DOTFILES/$package/$relative"
mkdir -p "$(dirname "$dest")"
mv "$source" "$dest"
cd "$DOTFILES" && stow "$package"
echo "added $package: ~/$relative"
}
# Check if a name is already managed by any stow package:
# either as a top-level package or as .config/<name> inside
# a differently-named package, for example ruby/.config/gem
is_managed() {
[ -d "$DOTFILES/$1" ] && return 0
for package in "$DOTFILES"/[!.]*/; do
[ -d "${package}.config/$1" ] && return 0
done
return 1
}
scan_dotconfig() {
for dir in "$HOME"/.config/*/; do
[ -L "$dir" ] && continue
name="$(basename "$dir")"
is_managed "$name" && continue
printf "Add %s? [y/N] " "$name"
read -r answer
case "$answer" in
[yY]*) add_package "$name" "$dir" ;;
esac
done
}
case "${1:-}" in
help|--help|-h)
usage
;;
add)
case "${2:-}" in
.config)
if [ -n "${3:-}" ]; then
add_package "$3" "$HOME/.config/$3"
else
scan_dotconfig
fi
;;
.config/*)
name="${2#.config/}"
add_package "$name" "$HOME/.config/$name"
;;
"")
usage >&2
exit 1
;;
.*)
echo "error: package name must not start with '.'" >&2
exit 1
;;
*)
add_package "$2" "$HOME/.$2"
;;
esac
;;
"")
cd "$DOTFILES"
# [!.]*/ excludes hidden directories (.git, .claude, et cetera)
# because when bash dotglob is set */ matches dotfiles as well
output="$(stow --verbose [!.]*/ 2>&1)" || { echo "$output" >&2; exit 1; }
if [ -n "$output" ]; then
echo "$output"
else
echo "all packages up to date"
fi
;;
*)
echo "unknown command: $1" >&2
usage >&2
exit 1
;;
esac