-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.sh
executable file
·88 lines (74 loc) · 1.37 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
#!/bin/bash
files=(\
bin \
config \
gitconfig \
tmux.conf \
urlview \
)
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"
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
elif [[ $1 == "remove" ]]; then
remove_links
elif [[ $1 == "clean" ]]; then
find -L "$HOME" -maxdepth 1 -type l -exec rm -i {} \;
else
die
fi