-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmanage.sh
executable file
·134 lines (119 loc) · 2.03 KB
/
manage.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
files=(\
agignore \
bash_profile \
bashrc \
bazelrc \
bin \
colordiffrc \
conductor.js \
config \
css \
ctags \
curlrc \
gemrc \
ghci \
git_template \
gitattributes \
gitconfig \
gitignore \
gnupg \
haskeline \
hgrc \
hushlogin \
ideavimrc \
inputrc \
js \
lldbhelpers \
lldbinit \
mailcap \
mpdconf \
msmtprc \
mutt \
ncmpcpp \
npmrc \
offlineimaprc \
psqlrc \
pylintrc \
rspec \
tmux \
tmux.conf \
urlview \
vim \
vimrc \
w3m \
weechat \
xvimrc \
zshenv \
zshrc \
)
custom_path() {
for i in "${!PATHS[@]}"
do
if [[ "$1" == "$i" ]]; then
return 0
fi
done
return 1
}
new_path() {
echo "$HOME/.$1"
}
# Links the passed filename to its new location
link() {
local filename="$1"
if [[ ! -e "$filename" ]]; then
echo "$filename doesn't exist"
return
fi
target="$(new_path "$filename")"
if [[ ! -e "$target" ]]; then
echo "Linking $filename to $target"
ln -s "$PWD/$filename" "$target"
elif [[ ! "$PWD/$filename" -ef "$target" ]]; then
echo "warning: $target exists and is not linked" >&2
fi
}
# Delete the linked file
unlink() {
target="$(new_path "$1")"
if [ -e "$target" ]; then
echo "Removing $target"
rm "$target"
fi
}
# Loops through and link all files without links
install_links() {
for file in "${files[@]}"
do
link "$file"
done
}
# Function to remove all linked files
remove_links() {
for file in "${files[@]}"
do
unlink "$file"
done
}
# Fuction to print the usage and exit when there's bad input
die() {
echo "Usage ./manage.sh {install|remove|clean}"
exit 1
}
# Make sure there is 1 command line argument
if [[ $# != 1 ]]; then
die
fi
# Check whether the user is installing or removing
if [[ $1 == "install" ]]; then
install_links
# It's required for this to have these permissions
chmod 0600 ~/.msmtprc
elif [[ $1 == "remove" ]]; then
remove_links
elif [[ $1 == "clean" ]]; then
find -L "$HOME" -maxdepth 1 -type l -exec rm -i {} \;
else
die
fi