-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.bash
executable file
·259 lines (215 loc) · 5.63 KB
/
sync.bash
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
#!/bin/bash
# TODO: Sync by last modified date
# Text Colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
clear='\033[0m'
delete='false' # remove pre-existing files
test='false' # test run
files='files.txt' # file with paths
verbose='false' # print lots of things
while getopts 'df:vt' flag; do
case "${flag}" in
d) delete='true' ;;
f) files="${OPTARG}" ;;
v) verbose='true' ;;
t) test='true' ;;
esac
done
# File names can get wierd sometimes
# set echo >&2 to avoid pollution of function capture
log() {
echo -e >&2 "$1"
}
# Verbose is basically just debug
verbose() {
if [ "$verbose" = true ]; then
echo -e >&2 "${yellow}$1${clear}"
fi
}
# Files are identified by a / at the end
# if the file name is left unspecified cp will use its current one hence just pwd
copy() {
verbose " "
verbose "copy "
local from="$1" to="$2" name="$3"
local super=false
local type="Unknown"
local overwrite=false
local overwrite_type="unknown"
if [[ ! -e $from ]]; then
log "${red}Failed${clear}: Item does not exist, $from"
return
fi
if [[ ! -r $from ]]; then
log "Missing read permission: '$from'"
super=$( elevate_prompt )
if [[ "$super" = false ]]; then
log "${red}Failed${clear}: Missing permissions, $from"
return
fi
fi
type=$( path_type $from )
if [[ "$type" = "Unkown" ]]; then
log "${red}Failed${clear} Invalid type: $from "
return
fi
verbose "Type: $type, From:'$from', To:'$to'"
if [[ ! -d $to ]]; then
log "${red}Failed${clear}: Not a directory, $to"
return
fi
if [[ ! -w $to && "$super" = false ]]; then
log "Missing write permission: '$to'"
super=$( bool_prompt "Would you like to run as sudo?(N,y)" )
if [[ "$super" = false ]]; then
log "${red}Failed${clear}: Missing permissions, $to"
return
fi
fi
if [[ -e "$to/$name" ]]; then
overwrite=true
if [[ ! -w "$to/$name" && "$super" = false ]]; then
log "Missing write permission: '$to/$name'"
super=$( bool_prompt "Would you like to run as sudo?(N,y)" )
if [[ "$super" = false ]]; then
log "${red}Failed${clear}: Missing permissions, $to/$name"
return
fi
fi
overwrite_type=$( path_type "$to/$name" )
if [[ "$overwrite_type" = "Unkown" ]]; then
log "${red}Failed${clear} Invalid type: $to/$name "
return
fi
if [[ "$overwrite_type" != "$type" ]]; then
log "${yellow}Warn${clear}: Mismatch overwrite type $type -> $overwrite_type"
approve_mismatch=$( bool_prompt "Would you like to continue?(N,y)" )
if [[ "$approve_mismatch" = false ]]; then
log "${red}Failed${clear}: Mismatch types, $type -> $overwrite_type"
return
fi
fi
if [[ "$r_flag" = true ]]; then
if [[ "$overwrite_type" = "Directory" ]]; then
elevate_run $super "rm -r $to/$name"
if [ "$?" -ne "0" ]; then
log "${red}Failed${clear}: Could not delete pre-existing folder"
return
fi
verbose "Removed pre-existing folder: '$to/$name'"
fi
if [[ "$overwrite_type" = "File" ]]; then
elevate_run $super "rm $to/$name"
if [ "$?" -ne "0" ]; then
log "${red}Failed${clear}: Could not delete pre-existing file"
return
fi
verbose "Removed pre-existing file: '$to/$name'"
fi
fi
fi
if [[ "$type" = "Directory" ]]; then
elevate_run $super "cp -r $from $to"
if [ "$?" -ne "0" ]; then
log "${red}Failed${clear}"
return
fi
fi
if [[ "$type" = "File" ]]; then
elevate_run $super "cp $from $to"
if [ "$?" -ne "0" ]; then
log "${red}Failed${clear}"
return
fi
fi
if [[ -e "$to/$name" ]]; then
log "${green}Success${clear}: $to/$name"
fi
}
bool_prompt(){
read -p "$1" -n 1 -r </dev/tty
log " "
if [[ $REPLY =~ ^[Yy]$ ]];
then
echo true
else
echo false
fi
}
elevate_run(){
if [[ "$test" = true ]]; then
return $(log "${magenta}Execute${clear}: $2")
elif [[ "$1" = true ]]; then
return $( sudo $2 )
else
return $( $2 )
fi
}
path_type() {
if [[ -d $1 ]]; then
echo "Directory"
elif [[ -L $1 ]]; then
echo "Link"
elif [[ -f $1 ]]; then
echo "File"
else
echo "Unkown"
fi
return
}
parse_re='^([[:alnum:]+=\-_/.\\]+)[[:space:]]+\.([[:alnum:]+=\-_/.\\]+)'
parse_path_pull() {
from=$(homeSub "$1")
if [[ $from =~ "$parse_re" ]]; then
verbose "has multipath ${bash_rematch[1]} : ${bash_rematch[2]} "
from="${bash_rematch[1]%/}"
to="$PWD${bash_rematch[2]%/}"
else
to="$PWD"
fi
name=$(basename $from)
}
parse_path_push() {
to=$(homesub "$1")
if [[ $to =~ "$parse_re" ]]; then
verbose "has multipath ${bash_rematch[1]} : ${bash_rematch[2]} "
from="$PWD${bash_rematch[2]%/}"
to="${bash_rematch[1]%/}"
else
from="$pwd"
fi
name=$(basename $to)
from="$from/$name"
to=$(dirname "$to")
}
# Simple match for home identity then replace with actual env
# BASH_REMATCH is the output of =~ and groups.
homeSub() {
re='(\$HOME|~)(.*)'
if [[ $1 =~ $re ]]; then
# verbose "homesub regex match"
echo "$HOME${bash_rematch[2]}"
else
# verbose "homesub no match"
echo "$1"
fi
}
# Read input from file one line at a time.
while read -r line || [ -n "$line" ]; do
verbose " "
verbose "-------------------------------------------"
verbose "Pulling: $line"
from="" to="" name=""
parse_path_pull "$line"
verbose " "
verbose "parse_path "
verbose "From: $from"
verbose "To: $to"
verbose "Name: $name"
copy "$from" "$to" "$name"
done <"$files"