-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotey
executable file
·277 lines (244 loc) · 7.84 KB
/
dotey
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
#!/usr/bin/env bash
# The MIT License (MIT)
# Copyright (c) 2012 Fabian Franke http://fabianfranke.de
# layout variables
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
RED="\033[0;31m"
COLORRESET="\033[0m"
SPACER=" "
# filename for the private dotfiles archive
packfilename='privdots'
# the big linker
function install() {
backupall=false
deleteall=false
skipall=false
backupdir=${PWD}/backup
# in case we want to link from a sub directory in .dotfiles, e.g. privatedots
if [ ! -z $1 ]; then
prefix=$1
fi
# we have to check if there are any linkable files at all or at strange
# and wild links will appear
if [ "`ls $prefix*.symlink | wc -l`" != 0 ]; then
echo -e "$GREEN-$COLORRESET found some .symlink files, starting symlinking"
else
echo -e "$RED-$COLORRESET found no .symlink files, nothing to do"
exit
fi
for file in $prefix*.symlink; do
# target is the planned home directory link
# purefile name is needed in case we work with a prefix
target="$HOME/.`basename ${file%.*}`"
purefilename=`basename ${file%.*}`
# if the target is already linked to our .dotfiles directory skip it
if [ "`readlink $target`" == "${PWD}/$file" ]; then
continue
fi
# we have an existing .dotfile
if [ -e "$target" ]; then
# go down the fast lane if the user has already wished to do something
# to all files
if $backupall ; then
echo -e "$SPACER$YELLOW-$COLORRESET moving $target to backup"
mv $target $backupdir/
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
elif $deleteall ; then
echo -e "$SPACER$RED-$COLORRESET deleting existing $target"
rm -r $target
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
elif $skipall ; then
echo -e "$SPACER$YELLOW-$COLORRESET skipping ${file%.*}"
else
# no general decision saved, let's ask
# lower case: decision for the current file
# upper case: decision for the current and all following file conflicts
echo "$target exists. What to do? (b)ackup, (d)elete, (s)kip. Or for this and further existing files (B)ackup, (S)kip, (D)elete"
read filedecision
case $filedecision in
b)
echo -e "$SPACER$YELLOW-$COLORRESET moving $target to .dotfiles/backup/.$purefilename"
mv $target $backupdir/
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
;;
B)
echo backup all
backupall=true
echo -e "$SPACER$YELLOW-$COLORRESET moving $target to .dotfiles/backup/.$purefilename"
mv $target $backupdir/
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
;;
d)
echo -e "$SPACER$RED-$COLORRESET deleting $target"
rm -r $target
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
;;
D)
echo deleting all
deleteall=true
echo -e "$SPACER$RED-$COLORRESET deleting $target"
rm -r $target
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
;;
s)
echo -e "$SPACER$YELLOW-$COLORRESET skipping ${file%.*}"
continue
;;
S)
echo skipping all
skipall=true
echo -e "$SPACER$YELLOW-$COLORRESET skipping ${file%.*}"
continue
;;
*)
echo -e "$RED-$COLORRESET wrong input, skipping"
continue
;;
esac
fi
# no existing .dotfile in ~, link found file
else
echo -e "$SPACER$GREEN+$COLORRESET linking $file to $target"
ln -s ${PWD}/$file $target
fi
done
}
function uninstall() {
# in case we want to link from a sub directory if .dotfiles. e.g. privatedots
if [ ! -z $1 ]; then
prefix=$1
fi
# loop over *.symlink file in user .dotfiles/$prefix directory if a link from
# the home directory points to the file, delete the symlink. When a backup is
#found restore it.
for sym in $prefix*.symlink; do
purefilename=`basename ${sym%.*}`
target="$HOME/.$purefilename"
if [ "`readlink $target`" == "${PWD}/$sym" ]; then
echo -e "$RED-$COLORRESET Deleting $target"
rm $target
if [ -e "backup/.$purefilename" ]; then
echo -e "$SPACER$GREEN+$COLORRESET restoring original file"
mv backup/.$purefilename $HOME/
fi
fi
done
}
# fetch updates from git repo and _hide_ the git output for a nicer user experience
function update() {
echo -e "$GREEN-$COLORRESET Fetching updates from github"
git pull >/dev/null
link
}
# someday todo: merge with install()
function link() {
echo -e "$GREEN-$COLORRESET Trying to link new files"
install
}
# check if a private dotfiles archive with name $packfilename is present
# decrypt, and extract
# call install() with sub directory prefix
function installprivatedots() {
echo -e "$GREEN-$COLORRESET Decrypting, unpacking and linking your private dotfiles"
if [ -e "$packfilename.des3" ]; then
openssl des3 -a -d -in $packfilename.des3 -out $packfilename.tbz2 && tar xfj $packfilename.tbz2 && rm $packfilename.tbz2 $packfilename.des3
else
echo -e "$RED-$COLORRESET ERROR: Cannot find transport container."
echo -e "$SPACER$RED-$COLORRESET It should be named $packfilename.des3"
fi
install "privatedots/"
}
# someday todo: merge with link() and install()
function linkprivatedots() {
echo -e "$GREEN-$COLORRESET Updating links from your private dotfiles"
install "privatedots/"
}
# pack ~/.dotfiles/privatedots with tar/bzip2 and encrypt with openssl
function packprivatedots() {
echo -e "$GREEN-$COLORRESET compressing and encrypting private dotfiles"
echo -e "$SPACER$YELLOW-$COLORRESET prompting for a password in a little while"
tar -jcf $packfilename.tbz2 privatedots && openssl des3 -a -in $packfilename.tbz2 -out $packfilename.des3 && rm $packfilename.tbz2
echo
if [ -e "$packfilename.des3" ]; then
echo -e "$GREEN-$COLORRESET your private dotfiles are ready at $PWD/$packfilename.des3"
echo -e "$SPACER Transfer them over to your other installations and run ./dotey installprivatedots"
else
echo -e "$RED-$COLORRESET something went terribly wrong"
fi
}
function delprivatedots() {
echo -e "$SPACER$RED-$COLORRESET unlinking and deleting your private dotfiles from this box"
echo -e "$SPACER CTRL+C to abort now"
echo -en "$RED"
for i in {1..5}; do
echo -n .
sleep 1
done
echo -e "$COLORRESET"
uninstall "privatedots/"
rm privatedots/*
}
function usage() {
cat << EOF
How to use this:
./dotey install
Symlink dotfiles to the home directory
./dotey uninstall
Remove all dotfile symlinks from home directory and restore from backup if possible
./dotey update
Fetch updates from the repository and update symlinks if needed
./dotey link
Update symlinks if needed
./dotey privatedots install
Decrypt and unpack your private dotfiles and install them at this computer
./dotey privatedots link
Link newly added private dotfiles to your home directory
./dotey privatedots pack
Encrypt and pack your private dotfiles to transfer them to another box
./dotey privatedots delete
Delete your private dotfiles from this computer
EOF
}
case $1 in
install)
install
;;
uninstall)
uninstall
;;
link)
link
;;
update)
update
;;
privatedots)
case $2 in
pack)
packprivatedots
;;
install)
installprivatedots
;;
link)
linkprivatedots
;;
delete)
deleteprivatedots
;;
*)
usage
;;
esac
;;
*)
usage
;;
esac