-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdotfiles.sh
More file actions
executable file
·314 lines (264 loc) · 7.03 KB
/
dotfiles.sh
File metadata and controls
executable file
·314 lines (264 loc) · 7.03 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/bin/bash
# script to manage dotfiles
# The MIT License (MIT)
#
# Copyright (c) 2015 Hannes Fuchs
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# get scriptdir
# from http://stackoverflow.com/a/246128
SCRIPTDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
# static vars
FILEMAP="$SCRIPTDIR/FILEMAP"
HOSTNAME=$(hostname 2>/dev/null || cat /etc/hostname)
# check if file is in filemap
function is_in_filemap {
# check for argument
if [ -z "$1" ]; then
echo "is_in_filemap needs an argument"
exit 1
fi
# create if not exists
if [ ! -f "$FILEMAP" ]; then
touch "$FILEMAP"
fi
grep -c "$1" "$FILEMAP"
}
# get the absolute path of the file
function absolute_path_of_file {
echo "$(cd "$(dirname "$1")" && pwd -P)"/"$(basename "$1")"
}
# get the absolute directory of the file
function absolute_dir_of_file {
cd "$(dirname "$1")" && pwd -P
}
# ask yes or no question -> return 1 for yes and 0 for no
function ask_yes_no {
L=0
while [ $L -eq 0 ]; do
read -rp "Type yes/no:" YN
case $YN in
[Yy]* )
L=1
A=1
;;
[Nn]* )
L=1
A=0
;;
* )
L=0
echo "Enter yes or no"
;;
esac
done
return "$A"
}
# adds a file
function add {
# check for argument
if [ -z "$1" ]; then
echo "add needs an argument"
exit 1
fi
FILE_REL=$(absolute_path_of_file "$1")
FILE=${FILE_REL/$HOME/\~}
# check if already in list
if [ "$(is_in_filemap "$FILE")" -ge 1 ] || [ -L "$FILE_REL" ]; then
echo "file $FILE already in git?"
exit 1
fi
# check if in $HOME
if [[ ! "$FILE_REL" =~ $HOME ]]; then
echo "file $FILE_REL seems to be not in $HOME"
exit 1
fi
# create a "readable" filename
FILENTMP=${FILE_REL//${HOME}\//} # only filepath in HOME
FILENTMP=${FILENTMP//\//_} # replace / with _
FILENTMP=${FILENTMP/#\./} # remove first .
echo "Adding file $1 (move, link and add to git repro)"
# move the file, symlink and add to git
mv "$FILE_REL" "$SCRIPTDIR/$FILENTMP"
ln -s "$SCRIPTDIR/$FILENTMP" "$FILE_REL"
git add "$SCRIPTDIR/$FILENTMP"
# add to mapping
echo "$FILENTMP $FILE" >> "$FILEMAP"
# add FILEMAP to git
git add "$FILEMAP"
echo "don't forget to commit the changes"
}
# deletes a file
function del {
# check for argument
if [ -z "$1" ]; then
echo "delete needs an argument"
exit 1
fi
FILE_REL=$(absolute_path_of_file "$1")
# file in scriptdir?
if [[ "$FILE_REL" =~ $SCRIPTDIR ]]; then
FILE=${FILE_REL/$SCRIPTDIR/}
elif [[ "$FILE_REL" =~ $HOME ]]; then
FILE=${FILE_REL/$HOME/\~}
else
echo "error on deleting file $1 - wrong dir"
exit 1
fi
if [ "$(is_in_filemap "$FILE")" -ge 1 ]; then
# get the line -> src and dst
LINE=$(grep "$FILE" "$FILEMAP")
SRC=$(echo "$LINE" | cut -d" " -f1)
SRC="${SCRIPTDIR}/$SRC"
DST=$(echo "$LINE" | cut -d" " -f2)
DST="${DST/#\~/$HOME}"
else
echo "error on deleting file $1 - not in filemap"
exit 1
fi
# both should exists
if [ -z "$SRC" ] || [ -z "$DST" ] || [ ! -e "$SRC" ] || [ ! -e "$DST" ]; then
echo "error on deleting file $1 -> src: $SRC dst: $DST"
exit 1
fi
# delete symlink
if [ ! -L "$DST" ]; then
echo "error on deleting file $1 -> $DST is not a link"
exit 1
fi
# maybe use unlink
rm "$DST"
# copy to orign
cp "$SRC" "$DST"
# delete from FILEMAP and cleanup
grep -v "$LINE" "$FILEMAP" > "${FILEMAP}.tmp" && mv "${FILEMAP}.tmp" "$FILEMAP"
sed -i '/^ *$/d' "$FILEMAP"
# git rm file
echo "delete file from git repro?"
ask_yes_no
RT=$?
if [ $RT -eq 1 ]; then
git rm "$SRC"
RT=$?
# getting dirty
if [ $RT -ne 0 ]; then
git reset HEAD -- "$SRC"
rm "$SRC"
fi
fi
# rm file
echo "delete file from fs?"
ask_yes_no
RT=$?
if [ $RT -eq 1 ]; then
rm "$DST"
fi
# add FILEMAP to git
git add "$FILEMAP"
}
# initalizate
function init {
echo "initializate (backup, move and link)"
if [ ! -f "$FILEMAP" ] || [ ! -r "$FILEMAP" ]; then
echo "couldn't read filemapping: $FILEMAP"
exit 1
fi
#check branch
if [ "$(git branch --no-color --no-column --list "$HOSTNAME" | wc -l )" -eq 1 ]; then
echo "found a branch for hostname ${HOSTNAME}, checkout?"
ask_yes_no
RV=$?
if [ $RV -eq 1 ]; then
git checkout "$HOSTNAME"
fi
else
echo "do you want to create a branch for ${HOSTNAME}?"
ask_yes_no
RV=$?
if [ $RV -eq 1 ]; then
git branch "$HOSTNAME"
fi
fi
BAKDIR="$SCRIPTDIR/backup/$(date +%s)/"
while read -r SRC DST
do
DST="${DST/#\~/$HOME}"
SRC="${SCRIPTDIR}/$SRC"
DSTDIR=$(absolute_dir_of_file "$DST")
# backup files, if exists
if [ -e "$DST" ] && [ ! -L "$DST" ]; then
if [ ! -d "$BAKDIR" ]; then
mkdir -p "$BAKDIR" || exit 1
fi
#if [[ ! "$FILE_REL" =~ "$HOME" ]]; then
if [[ "$DSTDIR" =~ $HOME ]]; then
TBACKDIR="${BAKDIR}${DSTDIR/$HOME/}/"
mkdir -p "$TBACKDIR" || exit 1
else
TBACKDIR="$BAKDIR"
fi
echo "File $DST exists, creating backup"
mv "$DST" "$TBACKDIR"
fi
if [ -e "$DST" ] && [ -L "$DST" ]; then
echo "$DST is already a symlink, skipping"
continue
fi
# create dir
if [ ! -e "$DSTDIR" ]; then
mkdir -p "$DSTDIR" || exit 1
elif [ -e "$DSTDIR" ] && [ ! -d "$DSTDIR" ]; then
echo "$DSTDIR exists and ist not a directory!"
exit 1
fi
# creat symlinks
ln -s "$SRC" "$DST"
done < "$FILEMAP"
}
function usage {
echo -e "usage: ${BASH_SOURCE[0]} [add|del|init] [file]\n
init initializes/bootstraps. All files in FILEMAP will be linked
to its destination. Existing files will be saved in backup
folder. Branches can be used (default is to use the hostname
as Branch-name) for different hosts.
add adds a file
The file should be in your home directory to be added. It
moves the file to the git repository (renamed) and creates
a link to its orign location. Finally it's added to the
repository.
del deletes a file
Deletes the specific file a.) from the repository b.) from
filesystem. You will be asket if you want to delete it from
the repository and the filesystem.\n
Also see the README"
}
case $1 in
a*)
add "$2"
;;
d*)
del "$2"
;;
i*)
init
;;
*)
usage
;;
esac