-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotfiles.sh
executable file
·177 lines (165 loc) · 3.65 KB
/
dotfiles.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
dotfiles=($(find `pwd` -mindepth 1 -maxdepth 2 -regex ".*\/\..*" \
-not -type d \
-not -name "*.DS_Store" \
-not -name "*.gitignore" \
-not -name "*.gitmodules" \
-not -name "*.git" \
-not -name "*.swp" \
-not -name "*.swo" \
-not -path "*/.git/*" \
-not -path "*/org/*"))
dotfiles+=("$(pwd)/vim/.vim")
dotfiles+=("$(pwd)/pip/.pip")
dotfiles+=("$(pwd)/tmux/.tmux")
dotfiles+=("$(pwd)/emacs/.emacs.d")
dotfiles+=($(pwd)/bin/*)
dotfiles+=("$(pwd)/ssh/rc")
dotfiles+=("$(pwd)/nvim")
dotfiles+=("$(pwd)/vscode/*.json")
home_dotsfile=($(find $HOME -maxdepth 1 -regex ".*\/\..*" \
-not -type d \
-not -name "*.DS_Store" \
-not -name "*.swp" \
-not -name "*.swo"))
home_dotsfile+=("$HOME/.vim")
home_dotsfile+=("$HOME/.emacs.d")
home_dotsfile+=($HOME/bin/*)
home_dotsfile+=("$HOME/.ssh/rc")
home_dotsfile+=("$(pwd)/nvim")
if [ "$(uname)" == 'Darwin' ]; then
home_dotsfile+="$HOME/Library/Application Support/Code/User/*.json"
elif [ "$(expr substr $(uname -s) 1 5)" == 'Linux']; then
home_dotsfile+="$HOME/.config/Code/User/*.json"
fi
install() {
backup
git submodule update --init --recursive
set
}
set() {
for file_name in "${dotfiles[@]}"; do
if [[ $file_name =~ "ssh" ]]; then
ln -svf $file_name $HOME/.ssh
elif [[ $file_name =~ "bin" ]]; then
ln -svf $file_name $HOME/bin
elif [[ $file_name =~ "nvim" ]]; then
if [ -e $HOME/.config ]; then
ln -svf $file_name $HOME/.config/nvim
else
mkdir $HOME/.config
ln -svf $file_name $HOME/.config/nvim
fi
elif [[ $file_name =~ "vscode" ]]; then
if [ "$(uname)" == 'Darwin' ]; then
ln -svf $file_name "$HOME/Library/Application Support/Code/User"
elif [ "$(expr substr $(uname -s) 1 5)" == 'Linux']; then
ln -svf $file_name "$HOME/.config/Code/User"
fi
else
ln -svf $file_name $HOME
fi
echo "Set link to ${file_name}."
done
}
unset() {
for file_name in $(list); do
unlink $file_name
echo "Unlink to ${file_name}."
done
}
delete() {
for file_name in $(list); do
rm -rf $file_name
echo "Delete to ${file_name}."
done
}
backup() {
for file_name in "${home_dotsfile[@]}"; do
cp -RH $file_name org/
echo "Backup to ${file_name}."
done
}
list() {
for file_name in "${home_dotsfile[@]}"; do
link_path=$(readlink $file_name)
if [ $(contains "${dotfiles[@]}" "$link_path") == "y" ]; then
echo "$file_name"
fi
done
}
contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)) {
if [[ "${!i}" =~ "${value}" ]]; then
echo "y"
return 0
fi
}
echo "n"
return 1
}
debug() {
echo "dotfiles"
# echo "$dotfiles"
for file_name in "${dotfiles[@]}"; do
echo $file_name
done
echo "home_dotfiles"
# echo ${home_dotsfile[@]}
for file_name in "${home_dotsfile[@]}"; do
echo $file_name
done
}
help() {
echo "Useage: $0 <install|update|delete|backup|list|help>"
echo " - install|-i: Install dotfiles."
echo " - set|-s: Set dotfiles."
echo " - unset|-u: Un set dotfiles."
echo " - delete|-d: Delete a dotfiles."
echo " - list|-l: List dotfiles."
}
case $1 in
debug)
debug
exit 0
;;
install|-i)
install
exit 0
;;
set|-s)
set
exit 0
;;
unset|-u)
unset
exit 0
;;
delete|-d)
delete
exit 0
;;
backup|-b)
backup
exit 0
;;
list|-l)
cnt=0
for file_name in $(list); do
echo "$file_name -> $(readlink $file_name)"
let cnt++
done
echo "Total lists $cnt"
exit 0
;;
help|-h)
help
exit 0
;;
*)
help
exit 1
;;
esac