forked from lmartinking/dotfiles-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotfiles
executable file
·257 lines (201 loc) · 5.68 KB
/
dotfiles
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
REPO_PATH="${HOME}/.dotfiles"
MAP_FILE="dotfiles.map"
function die () {
echo $@ 1>&2
exit 1
}
function print_separator {
# 80 chars
echo "================================================================================";
}
function main () {
cmd="$1"
if [[ "$cmd" != "setup" && "$cmd" != "setup-from" ]] ; then
cd $REPO_PATH 2> /dev/null || die "No repo! Please run '$0 setup'"
fi
shift
_$cmd $@
}
function map_add () {
local local_file="$1"
local repo_file="$2"
local commit_msg="Added Map: $local_file -> $repo_file"
echo "$local_file:$repo_file" >> $MAP_FILE &&
git add $MAP_FILE &&
git commit -m "$commit_msg"
}
function map_remove () {
local local_file="$1"
cp $MAP_FILE $MAP_FILE.bak &&
cat $MAP_FILE.bak | sed -e "s/.*:${local_file}$//g" -e '/^$/d' > $MAP_FILE &&
rm $MAP_FILE.bak
git add "$MAP_FILE" &&
git commit -m "Removed: $local_file"
}
function local_file_add () {
local file="$1"
local repo_file="$2"
local commit_msg="Added: $local_file -> $repo_file"
cp -l "$file" "$repo_file" &&
git add "$repo_file" &&
git commit -m "$commit_msg"
}
function file_link () {
local local_file="$1"
local repo_file="$2"
if [[ -f "$local_file" ]] ; then
mv "$local_file" "$local_file.old" || die "Could not backup $local_file"
fi
ln "$repo_file" "$local_file" &&
rm -f "$local_file.old"
}
function _ () {
echo "Usage: $0 <command> [<args>]"
exit 1
}
function _setup () {
if [[ ! -d $REPO_PATH ]] ; then
mkdir $REPO_PATH || die "Could not create dotfiles repository"
fi
[[ -d $REPO_PATH ]] || die "No repo !?"
cd $REPO_PATH && [[ ! -d .git ]] && git init .
}
function _setup-remote () {
local remote="$1"
[[ -z "$remote" ]] && die "No remote specified"
git remote add origin "$remote"
}
function _setup-from () {
local remote="$1"
[[ -z "$remote" ]] && die "No remote specified"
git clone "$remote" "$REPO_PATH" &&
cd $REPO_PATH &&
_sync
}
function read_yes_no {
local input;
while [[ ! $input =~ ^[y|Y|n|N]$ ]]; do
echo -en "Your choice: "
read input;
done
if [[ $input =~ ^[y|Y]$ ]]; then
return 0; # true
else
return 1; # false
fi
}
function read_user_input {
local question=$@;
echo -en "${question}";
read return
echo "${return}";
}
function select-directory {
local dir_list=$@;
local PS3="Type a number or 'q' to quit: "
select dir in ${dir_list}; do
if [ -n "${dir}" ]; then
echo ${dir}
fi
break
done
}
function _add () {
local file="$1"
local local_file=$( echo "$file" | sed -e "s:$HOME/:~/:g" )
local repo_file=$( echo "$file" | sed -e "s:$HOME/::g" -e "s://:/:g" -e "s:^.::" -e 's:/:-:' )
[[ ! -f "$file" ]] && die "Not a file: $file"
echo "Do you want to save the file in a subdirectory? [y|n]";
if read_yes_no; then
# build clean directory list
for f in $(cd ${REPO_PATH}; find . -maxdepth 1 -type d ! -iname ".*"); do
local dir_list="${dir_list} ${f/.\/}";
done
print_separator
# ask user for directory and add option to create new subdirectory
local dir_selected=$(select-directory $dir_list [CREATE_NEW]);
if [ "${dir_selected}" = "[CREATE_NEW]" ]; then
print_separator
while true; do
echo -en "Please enter directory name: ";
read dir_selected;
echo "Is this correct? '${dir_selected}' [y|n]";
if read_yes_no; then
break;
fi
done;
# create directory:
print_separator
echo "Creating new subdirectory in dotfiles repository...";
mkdir -p ${REPO_PATH}/${dir_selected}
fi
fi
local_file_add "$file" "./${dir_selected}/$repo_file" &&
map_add "$local_file" "./${dir_selected}/$repo_file"
}
function _remove () {
local wanted="$1"
local result=$( grep ":${wanted}$" $MAP_FILE )
if [[ -z "$result" ]] ; then
echo "Not found :-("
else
local repo_file=$( echo $result | cut -d ':' -f 2 )
echo "Removing: $repo_file"
map_remove "$repo_file" &&
git rm -f "$repo_file" &&
git commit -m "Removed: $repo_file"
fi
}
function _use () {
local wanted="$1"
if [[ "$wanted" == ":all" ]] ; then
wanted='.*'
fi
local results=$( grep ":${wanted}$" $MAP_FILE )
if [[ -z "$results" ]] ; then
echo "Not found :-("
else
for result in $results ; do
local local_file=$( echo $result | cut -d ':' -f 1 | sed -e "s:~:/home/$USER:" )
local repo_file=$( echo $result | cut -d ':' -f 2 )
echo "Using: $repo_file -> $local_file"
if [[ -f "$local_file" ]] ; then
if diff $local_file $repo_file > /dev/null ; then
echo "Local file exists, but is the same as repo file. Linking anyway."
else
local backup_file="$local_file.local"
echo "Local file already exists. Moving to $backup_file"
cp "$local_file" "$backup_file"
fi
fi
file_link "$local_file" "$repo_file"
done
fi
}
function _update () {
git commit -a -m "Update from $HOSTNAME ($(date))"
}
function _sync () {
_update
git pull origin master
git push origin master
}
function _status () {
git status
}
function _help () {
cat <<EOF
Usage: ./dotfiles <command> [<args>]
Commands:
setup Initialize new dotfiles project
setup-remote <repo-path-or-url> add new remote repository
add <file> add new file to repository
use [ :all | use all configuration files from repository
<name> ] use a particular file
sync synchronise your dotfiles
status like git status
update update your dotfile changes to your local repo
EOF
}
main $@