-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemcs
executable file
·231 lines (188 loc) · 6.22 KB
/
emcs
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
#!/usr/bin/env bash
#-----------------------------------------------------------------------#
# handleError:
# to be able to exit entire program from within function
trap "exit 1" TERM
export TOP_PID=$$
handleError () {
if [ $# -ne 1 ]; then
1="Unknown error"
fi
echo -e "\nError: $1\n"
kill -s TERM $TOP_PID
}
#-----------------------------------------------------------------------#
# dumpFind: check directory & dump recursive file/folder list
# $1 = root path ie, /media/4000/Audio
# $2 = file/dir ie, f or d
# $3 = label ie, files-latest
dumpFind () {
if [ $# -eq 3 ]; then
# check that directory exists
if [ -d "$1" ]; then
# save recursive file/dir list to file (ignore hidden)
# -printf '%P\n' removes the proceeding ./
if [ "$2" = "f" ]; then
find "$1" -not -path '*/\.*' -printf '%P\n' | \
sort > "$1/.emcs-$label-$3"
elif [ "$2" = "d" ]; then
find "$1" -type d -links 2 -not -path '*/\.*' \
-printf '%P\n' | sort > "$1/.emcs-$label-$3"
else
handleError "mode: file(f) or directory(d) not specified"
fi
else
handleError "'$1' is not a directory"
fi
else
handleError "Lack of args to 'dumpFind'"
fi
}
#-----------------------------------------------------------------------#
# removeF: remove file or folder
# $1 = file/folder path ie, /media/4000/Audio/Phish/...
# $2 = simulate/delete ie, 0=simulate 1=delete
removeF () {
arg=("$@")
if [ $# -eq 2 ]; then
# if file or directory exists
if [ -d "$1" ] || [ -e "$1" ]; then
# if not a simulation
if [ "$2" -eq "1" ]; then
# delete folder/file
if [ -d "$1" ]; then
rm -r "$1"
elif [ -e "$1" ]; then
rm "$1"
fi
fi
echo -e "$1" # return this value
fi
else
handleError "Lack of args to 'removeF'"
fi
}
#-----------------------------------------------------------------------#
# determineDeleted:
# $1 = root path ie, /media/4000/Audio
determineDeleted () {
dumpFind "$1" "f" "files-latest"
if [ -e "$1/.emcs-$label-files" ]; then
# exists in 'files', but not in 'files-latest'
comm -13 "$1/.emcs-$label-files-latest" "$1/.emcs-$label-files" \
> "$1/.emcs-$label-deleted"
fi
rm "$1/.emcs-$label-files-latest"
}
#-----------------------------------------------------------------------#
# removeDeleted: remove from one location what's not in another
# $1 = root path ie, /media/4000/Audio
# $2 = other path ie, /media/4001/Audio
removeDeleted () {
determineDeleted "$1"
if [ -e "$1/.emcs-$label-deleted" ] &&
[ $(stat -c%s "$1/.emcs-$label-deleted") -gt "0" ]; then
# simulate delete
count="0"
echo -e "Simulate delete from '$2':"
while read line
do
items=("$2/$line" 0)
r=`removeF "${items[@]}"`
if [[ "$r" != "" ]]; then
count=$((count+1))
echo "$count: $r"
fi
done < "$1/.emcs-$label-deleted"
# actual delete
if [ "$count" -gt "0" ]; then
read -p "Actually delete these files/folders? " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
while read line
do
items=("$2/$line" 1)
removeF "${items[@]}"
done < "$1/.emcs-$label-deleted"
fi
fi
else
echo -e "Nothing to delete from '$2'"
fi
if [ -e "$1/.emcs-$label-deleted" ]; then
rm "$1/.emcs-$label-deleted"
fi
}
#-----------------------------------------------------------------------#
# mostRecentlyUpdated: most recently updated file within folder
# $1 = path ie, /media/4000/Audio
mostRecentlyUpdated() {
find "$1" -type f -exec stat \{} --printf="%Y\n" \; \
| sort -r | head -n 1
}
#-----------------------------------------------------------------------#
# updateModified: update existing dirs by last modified date
updateModified() {
dumpFind "$dir1" "d" "dirs"
dumpFind "$dir2" "d" "dirs"
# common paths found in both
comm -12 "$dir1/.emcs-$label-dirs" "$dir2/.emcs-$label-dirs" \
> "$dir1/.emcs-$label-update"
# remove dumps
rm "$dir1/.emcs-$label-dirs"; rm "$dir2/.emcs-$label-dirs"
while read folder
do
# which folder most recently modified
dir1update=`mostRecentlyUpdated "$dir1/$folder"`
dir2update=`mostRecentlyUpdated "$dir2/$folder"`
if [ "$dir1update" = "" ]; then dir1update="0"; fi
if [ "$dir2update" = "" ]; then dir2update="0"; fi
if [ "$dir1update" -gt "$dir2update" ]; then
# dir1 is newer
echo -e "\nUpdating: $dir2/$folder"
rsync -r -t -v --exclude=".*" "$dir1/$folder/" "$dir2/$folder"
elif [ "$dir2update" -gt "$dir1update" ]; then
# dir2 is newer
echo -e "\nUpdating: $dir1/$folder"
rsync -r -t -v --exclude=".*" "$dir2/$folder/" "$dir1/$folder"
fi
done < "$dir1/.emcs-$label-update"
rm "$dir1/.emcs-$label-update"
}
#-----------------------------------------------------------------------#
# main program
# check for minimum required arguments
if [ $# -ne 3 ]; then
echo -e "\nUsage: emcs [label] [location1] [location2]\n"; exit 1
fi
# save label to be used globally
label=$1
# check first path
dir1=`echo $2 | sed -e 's/\/$//'`
if [ ! -d "$dir1" ]; then
echo -e "\nError: '$dir1' is not a directory.\n"; exit 1
fi
# check second path
dir2=`echo $3 | sed -e 's/\/$//'`
if [ ! -d "$dir2" ]; then
echo -e "\nError: '$dir2' is not a directory.\n"; exit 1
fi
echo -e "\nEvolving Music Collection Synchronization (emcs):"
echo -e "Label: $label"
echo -e "Dir1: $dir1"
echo -e "Dir2: $dir2"
echo -e "\nChecking for intentionally deleted files..."
# remove intentionally deleted files
removeDeleted "$dir1" "$dir2"
removeDeleted "$dir2" "$dir1"
echo -e "\nUpdating modified files..."
updateModified
# ignore hidden files & folders: --exclude=".*"
echo -e "\nSyncrhonizing to '$dir2'"
rsync -r -t -v --size-only --exclude=".*" "$dir1/" "$dir2"
echo -e "\nSyncrhonizing to '$dir1'"
rsync -r -t -v --size-only --exclude=".*" "$dir2/" "$dir1"
echo -e "\nSaving updated file list..."
dumpFind "$dir1" "f" "files"
cp -f "$dir1/.emcs-$label-files" "$dir2/.emcs-$label-files"
echo -e "\nFinished."