-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotfiles
executable file
·211 lines (173 loc) · 4.49 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
#!/bin/bash
################################################################################
#Copyright 2015 Robert Dolca <robert.dolca@gmail.com>
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
HERE=`dirname $0`
REPO="$HERE/files/"
function homepath {
len=`expr ${#HOME} + 1`
name=${1:$len}
echo $name | sed "s/^\///g"
}
function repopath {
len=`expr ${#REPO}`
name=${1:$len}
echo $name | sed "s/^\///g"
}
function log {
if [ $VERBOSE -eq 1 ]; then
echo "$1"
fi
}
function find_files {
# Get the base path for the search
BASE=$1
# There is a global versioned white/blacklist and there is a local one
whitelist=`cat $HERE/whitelist.txt $HOME/.dotfiles/whitelist.txt 2> /dev/null`
blacklist=`cat $HERE/blacklist.txt $HOME/.dotfiles/blacklist.txt 2> /dev/null`
# Remove empty lines
whitelist=`echo "$whitelist" | grep -v '^$'`
blacklist=`echo "$blacklist" | grep -v '^$'`
# Check if the whitelist is empty
if ! [ -n "$whitelist" ]; then
echo "Error: the whitelist can not be empty"
exit 1
fi
# Make a string with all the whitelisted files (separated by space)
search=`echo -n "$whitelist" | perl -p -e "s#\n#\" \"$BASE/#"`
search="\"$BASE/$search\""
# Check if there is anything blacklisted
if [ -n "$blacklist" ]; then
blacklist=`echo -n "$blacklist" | perl -p -e "s#/\\$##"`
ignore=`echo -n "$blacklist" | perl -p -e "s#\n#\" -o -path \"$BASE/#"`
ignore="\( -path \"$BASE$ignore\" \) -prune -o -print"
else
ignore=""
fi
eval find $search $ignore 2> /dev/null
}
function collect {
# Create the repo folder if it's not present
mkdir -p $REPO
# Create the local white/blacklist
mkdir -p "$HOME/.dotfiles"
touch "$HOME/.dotfiles/whitelist.txt"
touch "$HOME/.dotfiles/blacklist.txt"
files=`find_files $HOME`
# For each entry in the list
for homepath in $files; do
# Get the path relative to the home folder
filename=`homepath $homepath`
# Path in the home folder
repopath="$REPO/$filename"
# If it's a folder we don't need it
if ! [ -d $homepath ] && ! [ -d $repopath ]; then
# Check if the files are the same
if diff $repopath $homepath &> /dev/null; then
log "same $filename"
else
if [ -f $repopath ]; then
log "update $filename"
else
log "create $filename"
fi
# If this is a dry run we don't change anything
if [ $DRY_RUN -eq 0 ]; then
# If the folder containing the
# file does not exit create it
mkdir -p "$REPO/`dirname $filename`"
# Copy the file
cp $homepath $REPO/$filename
fi
fi
fi
done
}
function deploy {
if ! [ -d $REPO ]; then
echo "Repo not found"
fi
files=`find_files $REPO`
# For each entry in the list
for repopath in $files; do
# Get the path relative to the repo folder
filename=`repopath $repopath`
# Path in the home folder
homepath="$HOME/$filename"
# We don't touch folders
if ! [ -d $repopath ] && ! [ -d $homepath ]; then
# Check if the files are the same
if diff $repopath $homepath &> /dev/null; then
log "same $filename"
else
if [ -f $homepath ]; then
log "update $filename"
else
log "create $filename"
fi
# If this is a dry run we don't change anything
if [ $DRY_RUN -eq 0 ]; then
# If the folder containing the
# file does not exit create it
mkdir -p "`dirname $homepath`"
# Copy the file
cp $repopath $homepath
fi
fi
fi
done
}
function show_help {
echo "./dotfiles [collect|deploy] {OPTIONS}"
echo "Options"
echo -e "\t-d|--dry\tSimulate a run"
echo -e "\t-v|--verbose\tShow the name of the files copied"
}
# The 1st parameter is the command
command=$1
shift
# Default value for options
DRY_RUN=0
VERBOSE=0
while [[ $# > 0 ]]; do
key="$1"
case $key in
-d|--dry)
DRY_RUN=1
VERBOSE=1
;;
-v|--verbose)
VERBOSE=1
;;
*)
echo "Unknown option $key"
show_help
exit 1
;;
esac
shift
shift
done
case $command in
collect)
collect
;;
deploy)
deploy
;;
*)
show_help
;;
esac