-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·86 lines (74 loc) · 1.68 KB
/
install.sh
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
#!/bin/sh
#
# Install dotfiles symlinks in $HOME
#
# USAGE:
# Just run it.
#
# Run with `-n` to see a dry run of what the script will do.
set -e
############# SETUP: variables, flags #############
colorR=$(tput setaf 1)
colorG=$(tput setaf 2)
nocolor=$(tput sgr0)
dry_run=0
while getopts ":n" opt; do
case $opt in
n)
dry_run=1;;
"?")
printf "Invalid option -%s\n" "$OPTARG"
exit 64;;
":")
printf "Option -%s requires an argument\n" "$OPTARG"
exit 64;;
esac
done
############# FUNCTIONS #############
warn() {
printf "%s%s%s\n" "$colorR" "$1" "$nocolor"
}
success() {
printf "%s%s%s\n" "$colorG" "$1" "$nocolor"
}
# ARGS: (source, dest)
link() {
if [ -e "$2" ]; then
if [ "$(realpath "$2")" = "$1" ]; then
echo "$1 is already linked to $2"
else
warn "Cannot link $1 to $2: destination exists"
fi
elif [ "$dry_run" = 1 ]; then
success "[DRY RUN] Would link $1 to $2"
else
if ln -s "$1" "$2"; then
success "Linked $1 to $2"
else
warn "Failed to link $1 to $2"
fi
fi
}
# ARGS (source, base_target, prefix)
target_path() {
printf "%s/%s%s" "$2" "$3" "$(basename "$1")"
}
############# DO THE LINKING #############
if [ "$dry_run" != 1 ]; then
# ensure gnupg has expected perms to avoid warning messages
chmod 0700 ./home/gnupg
fi
for f in home/*; do
if [ "$f" != "home/config" ]; then
link "$(realpath "$f")" "$(target_path "$f" "$HOME" ".")"
fi
done
if [ "$dry_run" != 1 ]; then
mkdir -p ~/.config
fi
for f in home/config/*; do
link "$(realpath "$f")" "$(target_path "$f" "$HOME/.config")"
done
for f in home_nodot/*; do
link "$(realpath "$f")" "$(target_path "$f" "$HOME")"
done